From eeb47e8bfb3872e85adb808009c68f6de52a9dfc Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Thu, 8 Feb 2024 19:43:06 +0400 Subject: [PATCH] Fix `next_cursor` field omitted for list responses (#461) This PR propagates the next_cursor field for all list methods. Previously, all list methods only returned a request_id and the data object, like a find response would. --- CHANGELOG.md | 3 ++ lib/nylas/handler/api_operations.rb | 38 ++++++++++++---- lib/nylas/resources/calendars.rb | 4 +- lib/nylas/resources/contacts.rb | 8 ++-- lib/nylas/resources/drafts.rb | 4 +- lib/nylas/resources/events.rb | 4 +- lib/nylas/resources/folders.rb | 4 +- lib/nylas/resources/messages.rb | 6 +-- lib/nylas/resources/threads.rb | 4 +- spec/nylas/handler/api_operations_spec.rb | 53 +++++++++++++++++++++-- spec/nylas/resources/calendars_spec.rb | 10 +++-- spec/nylas/resources/connectors_spec.rb | 6 ++- spec/nylas/resources/contacts_spec.rb | 14 +++--- spec/nylas/resources/credentials_spec.rb | 6 ++- spec/nylas/resources/drafts_spec.rb | 10 +++-- spec/nylas/resources/events_spec.rb | 7 ++- spec/nylas/resources/folders_spec.rb | 7 ++- spec/nylas/resources/messages_spec.rb | 10 +++-- spec/nylas/resources/threads_spec.rb | 10 +++-- 19 files changed, 150 insertions(+), 58 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf6ad3c5..385ec061 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/nylas/handler/api_operations.rb b/lib/nylas/handler/api_operations.rb index e3ed406f..eacd3476 100644 --- a/lib/nylas/handler/api_operations.rb +++ b/lib/nylas/handler/api_operations.rb @@ -11,13 +11,37 @@ 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, @@ -25,8 +49,6 @@ def get(path:, query_params: {}) api_key: api_key, timeout: timeout ) - - [response[:data], response[:request_id]] end end @@ -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: {}) @@ -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: {}) @@ -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: {}) diff --git a/lib/nylas/resources/calendars.rb b/lib/nylas/resources/calendars.rb index 89eff31a..3913703b 100644 --- a/lib/nylas/resources/calendars.rb +++ b/lib/nylas/resources/calendars.rb @@ -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 ) diff --git a/lib/nylas/resources/contacts.rb b/lib/nylas/resources/contacts.rb index b2a220a0..b3dab1c2 100644 --- a/lib/nylas/resources/contacts.rb +++ b/lib/nylas/resources/contacts.rb @@ -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 ) @@ -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 ) diff --git a/lib/nylas/resources/drafts.rb b/lib/nylas/resources/drafts.rb index 6bbebfa2..72693788 100644 --- a/lib/nylas/resources/drafts.rb +++ b/lib/nylas/resources/drafts.rb @@ -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 ) diff --git a/lib/nylas/resources/events.rb b/lib/nylas/resources/events.rb index f405e9c6..063d41b3 100644 --- a/lib/nylas/resources/events.rb +++ b/lib/nylas/resources/events.rb @@ -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 ) diff --git a/lib/nylas/resources/folders.rb b/lib/nylas/resources/folders.rb index 2c214210..963a7d33 100644 --- a/lib/nylas/resources/folders.rb +++ b/lib/nylas/resources/folders.rb @@ -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 diff --git a/lib/nylas/resources/messages.rb b/lib/nylas/resources/messages.rb index 5d4536b3..9530c6e0 100644 --- a/lib/nylas/resources/messages.rb +++ b/lib/nylas/resources/messages.rb @@ -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 ) @@ -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" diff --git a/lib/nylas/resources/threads.rb b/lib/nylas/resources/threads.rb index cfcea5d1..7172f9ae 100644 --- a/lib/nylas/resources/threads.rb +++ b/lib/nylas/resources/threads.rb @@ -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 ) diff --git a/spec/nylas/handler/api_operations_spec.rb b/spec/nylas/handler/api_operations_spec.rb index 462642e2..b16cabe8 100644 --- a/spec/nylas/handler/api_operations_spec.rb +++ b/spec/nylas/handler/api_operations_spec.rb @@ -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 { @@ -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 diff --git a/spec/nylas/resources/calendars_spec.rb b/spec/nylas/resources/calendars_spec.rb index 52c213bd..4b0c4e0a 100644 --- a/spec/nylas/resources/calendars_spec.rb +++ b/spec/nylas/resources/calendars_spec.rb @@ -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) @@ -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) diff --git a/spec/nylas/resources/connectors_spec.rb b/spec/nylas/resources/connectors_spec.rb index 5b38168a..32d3bac2 100644 --- a/spec/nylas/resources/connectors_spec.rb +++ b/spec/nylas/resources/connectors_spec.rb @@ -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) @@ -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) diff --git a/spec/nylas/resources/contacts_spec.rb b/spec/nylas/resources/contacts_spec.rb index 1ca0fc53..5f273b68 100644 --- a/spec/nylas/resources/contacts_spec.rb +++ b/spec/nylas/resources/contacts_spec.rb @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/spec/nylas/resources/credentials_spec.rb b/spec/nylas/resources/credentials_spec.rb index 3152738a..1ca3cc44 100644 --- a/spec/nylas/resources/credentials_spec.rb +++ b/spec/nylas/resources/credentials_spec.rb @@ -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) @@ -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) diff --git a/spec/nylas/resources/drafts_spec.rb b/spec/nylas/resources/drafts_spec.rb index 71f779f5..f2b7eb80 100644 --- a/spec/nylas/resources/drafts_spec.rb +++ b/spec/nylas/resources/drafts_spec.rb @@ -32,11 +32,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}/drafts" - list_response = [[response[0]], response[1]] - allow(drafts).to receive(:get) + allow(drafts).to receive(:get_list) .with(path: path, query_params: nil) .and_return(list_response) @@ -49,8 +52,7 @@ identifier = "abc-123-grant-id" query_params = { foo: "bar" } path = "#{api_uri}/v3/grants/#{identifier}/drafts" - list_response = [[response[0]], response[1]] - allow(drafts).to receive(:get) + allow(drafts).to receive(:get_list) .with(path: path, query_params: query_params) .and_return(list_response) diff --git a/spec/nylas/resources/events_spec.rb b/spec/nylas/resources/events_spec.rb index facdddce..073472f1 100644 --- a/spec/nylas/resources/events_spec.rb +++ b/spec/nylas/resources/events_spec.rb @@ -54,12 +54,15 @@ 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" query_params = { calendar_id: "5d3qmne77v32r8l4phyuksl2x" } path = "#{api_uri}/v3/grants/#{identifier}/events" - list_response = [[response[0]], response[1]] - allow(events).to receive(:get) + allow(events).to receive(:get_list) .with(path: path, query_params: query_params) .and_return(list_response) diff --git a/spec/nylas/resources/folders_spec.rb b/spec/nylas/resources/folders_spec.rb index d746f98c..8c6e40c8 100644 --- a/spec/nylas/resources/folders_spec.rb +++ b/spec/nylas/resources/folders_spec.rb @@ -19,11 +19,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}/folders" - list_response = [[response[0]], response[1]] - allow(folders).to receive(:get) + allow(folders).to receive(:get_list) .with(path: path) .and_return(list_response) diff --git a/spec/nylas/resources/messages_spec.rb b/spec/nylas/resources/messages_spec.rb index 3aed9230..b0aefa13 100644 --- a/spec/nylas/resources/messages_spec.rb +++ b/spec/nylas/resources/messages_spec.rb @@ -32,11 +32,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}/messages" - list_response = [[response[0]], response[1]] - allow(messages).to receive(:get) + allow(messages).to receive(:get_list) .with(path: path, query_params: nil) .and_return(list_response) @@ -49,8 +52,7 @@ identifier = "abc-123-grant-id" query_params = { foo: "bar" } path = "#{api_uri}/v3/grants/#{identifier}/messages" - list_response = [[response[0]], response[1]] - allow(messages).to receive(:get) + allow(messages).to receive(:get_list) .with(path: path, query_params: query_params) .and_return(list_response) diff --git a/spec/nylas/resources/threads_spec.rb b/spec/nylas/resources/threads_spec.rb index ba4f65da..e95960d3 100644 --- a/spec/nylas/resources/threads_spec.rb +++ b/spec/nylas/resources/threads_spec.rb @@ -54,11 +54,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}/threads" - list_response = [[response[0]], response[1]] - allow(threads).to receive(:get) + allow(threads).to receive(:get_list) .with(path: path, query_params: nil) .and_return(list_response) @@ -71,8 +74,7 @@ identifier = "abc-123-grant-id" query_params = { foo: "bar" } path = "#{api_uri}/v3/grants/#{identifier}/threads" - list_response = [[response[0]], response[1]] - allow(threads).to receive(:get) + allow(threads).to receive(:get_list) .with(path: path, query_params: query_params) .and_return(list_response)