Skip to content

Commit

Permalink
Merge branch 'main' into TW-2543-Ruby-SDK---url_auth_builder-not-work…
Browse files Browse the repository at this point in the history
…ing-properly
  • Loading branch information
mrashed-dev authored Feb 8, 2024
2 parents 1378047 + eeb47e8 commit db13ed0
Show file tree
Hide file tree
Showing 19 changed files with 150 additions and 58 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### Unreleased
* Fixed a bug where the `next_cursor` field was omitted for list responses

### 6.0.0 / 2024-02-05
* **BREAKING CHANGE**: Ruby SDK v6 supports the Nylas API v3 exclusively, dropping support for any endpoints that are not available in v3.
* **BREAKING CHANGE**: Officially support minimum Ruby v3
Expand Down
38 changes: 30 additions & 8 deletions lib/nylas/handler/api_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,44 @@ module Get
protected

include HttpClient
# Performs a GET call to the Nylas API.
# Performs a GET call to the Nylas API for a single item response.
#
# @param path [String] Destination path for the call.
# @param query_params [Hash, {}] Query params to pass to the call.
# @return Nylas data object and API Request ID.
# @return [Array([Hash, Array], String)] Nylas data object and API Request ID.
def get(path:, query_params: {})
response = execute(
response = get_raw(path: path, query_params: query_params)

[response[:data], response[:request_id]]
end

# Performs a GET call to the Nylas API for a list response.
#
# @param path [String] Destination path for the call.
# @param query_params [Hash, {}] Query params to pass to the call.
# @return [Array(Array(Hash), String, String)] Nylas data array, API Request ID, and next cursor.
def get_list(path:, query_params: {})
response = get_raw(path: path, query_params: query_params)

[response[:data], response[:request_id], response[:next_cursor]]
end

private

# Performs a GET call to the Nylas API.
#
# @param path [String] Destination path for the call.
# @param query_params [Hash, {}] Query params to pass to the call.
# @return [Hash] The JSON response from the Nylas API.
def get_raw(path:, query_params: {})
execute(
method: :get,
path: path,
query: query_params,
payload: nil,
api_key: api_key,
timeout: timeout
)

[response[:data], response[:request_id]]
end
end

Expand All @@ -39,7 +61,7 @@ module Post
#
# @param path [String] Destination path for the call.
# @param query_params [Hash, {}] Query params to pass to the call.
# @param request_body [String, Hash, nil] Request body to pass to the call.
# @param request_body [Hash, nil] Request body to pass to the call.
# @param headers [Hash, {}] Additional HTTP headers to include in the payload.
# @return Nylas data object and API Request ID.
def post(path:, query_params: {}, request_body: nil, headers: {})
Expand All @@ -66,7 +88,7 @@ module Put
#
# @param path [String] Destination path for the call.
# @param query_params [Hash, {}] Query params to pass to the call.
# @param request_body [String, Hash, nil] Request body to pass to the call.
# @param request_body [Hash, nil] Request body to pass to the call.
# @param headers [Hash, {}] Additional HTTP headers to include in the payload.
# @return Nylas data object and API Request ID.
def put(path:, query_params: {}, request_body: nil, headers: {})
Expand All @@ -93,7 +115,7 @@ module Patch
#
# @param path [String] Destination path for the call.
# @param query_params [Hash, {}] Query params to pass to the call.
# @param request_body [String, Hash, nil] Request body to pass to the call.
# @param request_body [Hash, nil] Request body to pass to the call.
# @param headers [Hash, {}] Additional HTTP headers to include in the payload.
# @return Nylas data object and API Request ID.
def patch(path:, query_params: {}, request_body: nil, headers: {})
Expand Down
4 changes: 2 additions & 2 deletions lib/nylas/resources/calendars.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class Calendars < Resource
#
# @param identifier [String] Grant ID or email account to query.
# @param query_params [Hash, nil] Query params to pass to the request.
# @return [Array(Array(Hash), String)] The list of calendars and API Request ID.
# @return [Array(Array(Hash), String, String)] The list of calendars, API Request ID, and next cursor.
def list(identifier:, query_params: nil)
get(
get_list(
path: "#{api_uri}/v3/grants/#{identifier}/calendars",
query_params: query_params
)
Expand Down
8 changes: 4 additions & 4 deletions lib/nylas/resources/contacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class Contacts < Resource
#
# @param identifier [String] Grant ID or email account to query.
# @param query_params [Hash, nil] Query params to pass to the request.
# @return [Array(Array(Hash), String)] The list of contacts and API Request ID.
# @return [Array(Array(Hash), String, String)] The list of contacts, API Request ID, and next cursor.
def list(identifier:, query_params: nil)
get(
get_list(
path: "#{api_uri}/v3/grants/#{identifier}/contacts",
query_params: query_params
)
Expand Down Expand Up @@ -78,9 +78,9 @@ def destroy(identifier:, contact_id:)
#
# @param identifier [String] Grant ID or email account to query.
# @param query_params [Hash, nil] Query params to pass to the request.
# @return [Array(Array(Hash), String)] The list of contact groups and API Request ID.
# @return [Array(Array(Hash), String, String)] The list of contact groups and API Request ID.
def list_groups(identifier:, query_params: nil)
get(
get_list(
path: "#{api_uri}/v3/grants/#{identifier}/contacts/groups",
query_params: query_params
)
Expand Down
4 changes: 2 additions & 2 deletions lib/nylas/resources/drafts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class Drafts < Resource
#
# @param identifier [String] Grant ID or email account to query.
# @param query_params [Hash, nil] Query params to pass to the request.
# @return [Array(Array(Hash), String)] The list of drafts and API Request ID.
# @return [Array(Array(Hash), String, String)] The list of drafts, API Request ID, and next cursor.
def list(identifier:, query_params: nil)
get(
get_list(
path: "#{api_uri}/v3/grants/#{identifier}/drafts",
query_params: query_params
)
Expand Down
4 changes: 2 additions & 2 deletions lib/nylas/resources/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class Events < Resource
#
# @param identifier [String] Grant ID or email account to query.
# @param query_params [Hash] Query params to pass to the request.
# @return [Array(Array(Hash), String)] The list of events and API Request ID.
# @return [Array(Array(Hash), String, String)] The list of events, API Request ID, and next cursor.
def list(identifier:, query_params:)
get(
get_list(
path: "#{api_uri}/v3/grants/#{identifier}/events",
query_params: query_params
)
Expand Down
4 changes: 2 additions & 2 deletions lib/nylas/resources/folders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class Folders < Resource
# Return all folders.
#
# @param identifier [String] Grant ID or email account to query.
# @return [Array(Array(Hash), String)] The list of folders and API Request ID.
# @return [Array(Array(Hash), String, String)] The list of folders, API Request ID, and next cursor.
def list(identifier:)
get(
get_list(
path: "#{api_uri}/v3/grants/#{identifier}/folders"
)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/nylas/resources/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def initialize(sdk_instance)
#
# @param identifier [String] Grant ID or email account to query.
# @param query_params [Hash, nil] Query params to pass to the request.
# @return [Array(Array(Hash), String)] The list of messages and API Request ID.
# @return [Array(Array(Hash), String, String)] The list of messages, API Request ID, and next cursor.
def list(identifier:, query_params: nil)
get(
get_list(
path: "#{api_uri}/v3/grants/#{identifier}/messages",
query_params: query_params
)
Expand Down Expand Up @@ -94,7 +94,7 @@ def send(identifier:, request_body:)
# Retrieve your scheduled messages.
#
# @param identifier [String] Grant ID or email account from which to find the scheduled message from.
# @return [Array(Hash, String)] The list of scheduled messages and the API Request ID.
# @return [Array(Array(Hash), String)] The list of scheduled messages and the API Request ID.
def list_scheduled_messages(identifier:)
get(
path: "#{api_uri}/v3/grants/#{identifier}/messages/schedules"
Expand Down
4 changes: 2 additions & 2 deletions lib/nylas/resources/threads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class Threads < Resource
#
# @param identifier [String] Grant ID or email account to query.
# @param query_params [Hash] Query params to pass to the request.
# @return [Array(Array(Hash), String)] The list of threads and API Request ID.
# @return [Array(Array(Hash), String, String)] The list of threads, API Request ID, and next cursor.
def list(identifier:, query_params: nil)
get(
get_list(
path: "#{api_uri}/v3/grants/#{identifier}/threads",
query_params: query_params
)
Expand Down
53 changes: 50 additions & 3 deletions spec/nylas/handler/api_operations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ def initialize(api_key, api_uri, timeout)
end

describe Nylas::ApiOperations do
let(:api_key) { "api-key-123" }
let(:api_uri) { "https://test.api.nylas.com" }
let(:timeout) { 60 }
let(:api_operations) { APIOperations.new(api_key, api_uri, timeout) }
let(:mock_response) do
{
Expand Down Expand Up @@ -68,6 +65,56 @@ def initialize(api_key, api_uri, timeout)
expect(response).to eq([mock_response[:data], mock_response[:request_id]])
end
end

describe "#get_list" do
let(:list_response) do
{
request_id: "mock_request_id",
data: [
{
id: "mock_id",
foo: "bar"
}
],
next_cursor: "mock_cursor"
}
end

it "returns a list response" do
path = "#{api_uri}/path"
query_params = { foo: "bar" }
allow(api_operations).to receive(:execute).with(
method: :get,
path: path,
query: query_params,
payload: nil,
api_key: api_key,
timeout: timeout
).and_return(list_response)

response = api_operations.send(:get_list, path: path, query_params: query_params)

expect(response).to eq([list_response[:data], list_response[:request_id],
list_response[:next_cursor]])
end

it "returns a list response with default query_params" do
path = "#{api_uri}/path"
allow(api_operations).to receive(:execute).with(
method: :get,
path: path,
query: {},
payload: nil,
api_key: api_key,
timeout: timeout
).and_return(list_response)

response = api_operations.send(:get_list, path: path)

expect(response).to eq([list_response[:data], list_response[:request_id],
list_response[:next_cursor]])
end
end
end

describe Nylas::ApiOperations::Post do
Expand Down
10 changes: 6 additions & 4 deletions spec/nylas/resources/calendars_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
end

describe "#list" do
let(:list_response) do
[[response[0]], response[1], "mock_next_cursor"]
end

it "calls the get method with the correct parameters" do
identifier = "abc-123-grant-id"
path = "#{api_uri}/v3/grants/#{identifier}/calendars"
list_response = [[response[0]], response[1]]
allow(calendars).to receive(:get)
allow(calendars).to receive(:get_list)
.with(path: path, query_params: nil)
.and_return(list_response)

Expand All @@ -38,8 +41,7 @@
identifier = "abc-123-grant-id"
query_params = { foo: "bar" }
path = "#{api_uri}/v3/grants/#{identifier}/calendars"
list_response = [[response[0]], response[1]]
allow(calendars).to receive(:get)
allow(calendars).to receive(:get_list)
.with(path: path, query_params: query_params)
.and_return(list_response)

Expand Down
6 changes: 4 additions & 2 deletions spec/nylas/resources/connectors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
end

describe "#list" do
let(:list_response) do
[[response[0]], response[1]]
end

it "calls the get method with the correct parameters" do
path = "#{api_uri}/v3/connectors"
list_response = [[response[0]], response[1]]
allow(connectors).to receive(:get)
.with(path: path, query_params: nil)
.and_return(list_response)
Expand All @@ -32,7 +35,6 @@
it "calls the get method with the correct parameters and query params" do
path = "#{api_uri}/v3/connectors"
query_params = { foo: "bar" }
list_response = [[response[0]], response[1]]
allow(connectors).to receive(:get)
.with(path: path, query_params: query_params)
.and_return(list_response)
Expand Down
14 changes: 8 additions & 6 deletions spec/nylas/resources/contacts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@
end

describe "#list" do
let(:list_response) do
[[response[0]], response[1], "mock_next_cursor"]
end

it "calls the get method with the correct parameters" do
identifier = "abc-123-grant-id"
path = "#{api_uri}/v3/grants/#{identifier}/contacts"
list_response = [[response[0]], response[1]]
allow(contacts).to receive(:get)
allow(contacts).to receive(:get_list)
.with(path: path, query_params: nil)
.and_return(list_response)

Expand All @@ -57,8 +60,7 @@
identifier = "abc-123-grant-id"
query_params = { foo: "bar" }
path = "#{api_uri}/v3/grants/#{identifier}/contacts"
list_response = [[response[0]], response[1]]
allow(contacts).to receive(:get)
allow(contacts).to receive(:get_list)
.with(path: path, query_params: query_params)
.and_return(list_response)

Expand Down Expand Up @@ -157,7 +159,7 @@
it "calls the get method with the correct parameters" do
identifier = "abc-123-grant-id"
path = "#{api_uri}/v3/grants/#{identifier}/contacts/groups"
allow(contacts).to receive(:get)
allow(contacts).to receive(:get_list)
.with(path: path, query_params: nil)

contacts.list_groups(identifier: identifier, query_params: nil)
Expand All @@ -167,7 +169,7 @@
identifier = "abc-123-grant-id"
query_params = { foo: "bar" }
path = "#{api_uri}/v3/grants/#{identifier}/contacts/groups"
allow(contacts).to receive(:get)
allow(contacts).to receive(:get_list)
.with(path: path, query_params: query_params)

contacts.list_groups(identifier: identifier, query_params: query_params)
Expand Down
6 changes: 4 additions & 2 deletions spec/nylas/resources/credentials_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
end

describe "#list" do
let(:list_response) do
[[response[0]], response[1]]
end

it "calls the get method with the correct parameters" do
provider = "google"
path = "#{api_uri}/v3/connectors/#{provider}/creds"
list_response = [[response[0]], response[1]]
allow(credentials).to receive(:get)
.with(path: path, query_params: nil)
.and_return(list_response)
Expand All @@ -29,7 +32,6 @@
provider = "google"
path = "#{api_uri}/v3/connectors/#{provider}/creds"
query_params = { foo: "bar" }
list_response = [[response[0]], response[1]]
allow(credentials).to receive(:get)
.with(path: path, query_params: query_params)
.and_return(list_response)
Expand Down
Loading

0 comments on commit db13ed0

Please sign in to comment.