Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/nylas/handler/api_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ module Get
#
# @param path [String] Destination path for the call.
# @param query_params [Hash, {}] Query params to pass to the call.
# @return [Array([Hash, Array], String)] Nylas data object and API Request ID.
# @return [Array([Hash, Array], String, Hash)] Nylas data object, API Request ID, and response headers.
def get(path:, query_params: {})
response = get_raw(path: path, query_params: query_params)

[response[:data], response[:request_id]]
[response[:data], response[:request_id], response[:headers]]
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.
# @return [Array<Array<Hash>, String, String, Hash>]
# Nylas data array, API Request ID, next cursor, and response headers.response headers.
def get_list(path:, query_params: {})
response = get_raw(path: path, query_params: query_params)

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

private
Expand Down Expand Up @@ -63,7 +64,7 @@ module Post
# @param query_params [Hash, {}] Query params 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.
# @return [Array(Hash, String, Hash)] Nylas data object, API Request ID, and response headers.
def post(path:, query_params: {}, request_body: nil, headers: {})
response = execute(
method: :post,
Expand All @@ -75,7 +76,7 @@ def post(path:, query_params: {}, request_body: nil, headers: {})
timeout: timeout
)

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

Expand Down
5 changes: 4 additions & 1 deletion lib/nylas/handler/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ def execute(method:, path:, timeout:, headers: {}, query: {}, payload: nil, api_
content_type = response.headers[:content_type].downcase
end

parse_json_evaluate_error(result.code.to_i, response, path, content_type)
parsed_response = parse_json_evaluate_error(result.code.to_i, response, path, content_type)
# Include headers in the response
parsed_response[:headers] = response.headers unless parsed_response.nil?
parsed_response
end
rescue RestClient::Exceptions::OpenTimeout, RestClient::Exceptions::ReadTimeout
raise Nylas::NylasSdkTimeoutError.new(request[:path], timeout)
Expand Down
60 changes: 39 additions & 21 deletions spec/nylas/handler/api_operations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,28 @@ def initialize(api_key, api_uri, timeout)

describe Nylas::ApiOperations::Get do
describe "#get" do
it "returns a response" do
it "returns response data, request_id and headers" do
path = "#{api_uri}/path"
query_params = { foo: "bar" }
mock_headers = { "X-Request-Id" => "123", "Content-Type" => "application/json" }
response_with_headers = mock_response.merge(headers: mock_headers)

allow(api_operations).to receive(:execute).with(
method: :get,
path: path,
query: query_params,
payload: nil,
api_key: api_key,
timeout: timeout
).and_return(mock_response)
).and_return(response_with_headers)

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

expect(response).to eq([mock_response[:data], mock_response[:request_id]])
expect(response).to eq([
mock_response[:data],
mock_response[:request_id],
mock_headers
])
end

it "returns a response with default query_params" do
Expand All @@ -62,7 +69,7 @@ def initialize(api_key, api_uri, timeout)

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

expect(response).to eq([mock_response[:data], mock_response[:request_id]])
expect(response).to eq([mock_response[:data], mock_response[:request_id], nil])
end
end

Expand All @@ -76,11 +83,15 @@ def initialize(api_key, api_uri, timeout)
foo: "bar"
}
],
next_cursor: "mock_cursor"
next_cursor: "mock_cursor",
headers: {
"X-Request-Id" => "123",
"Content-Type" => "application/json"
}
}
end

it "returns a list response" do
it "returns list response with headers" do
path = "#{api_uri}/path"
query_params = { foo: "bar" }
allow(api_operations).to receive(:execute).with(
Expand All @@ -94,8 +105,12 @@ def initialize(api_key, api_uri, timeout)

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]])
expect(response).to eq([
list_response[:data],
list_response[:request_id],
list_response[:next_cursor],
list_response[:headers]
])
end

it "returns a list response with default query_params" do
Expand All @@ -112,32 +127,35 @@ def initialize(api_key, api_uri, timeout)
response = api_operations.send(:get_list, path: path)

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

describe Nylas::ApiOperations::Post do
describe "#post" do
it "returns a response" do
it "returns response with headers" do
path = "#{api_uri}/path"
query_params = { foo: "bar" }
request_body = { foo: "bar" }
headers = { "Content-Type" => "application/json" }
mock_headers = { "X-Request-Id" => "123", "Content-Type" => "application/json" }
response_with_headers = mock_response.merge(headers: mock_headers)

allow(api_operations).to receive(:execute).with(
method: :post,
path: path,
query: query_params,
payload: request_body,
headers: headers,
query: {},
payload: nil,
headers: {},
api_key: api_key,
timeout: timeout
).and_return(mock_response)
).and_return(response_with_headers)

response = api_operations.send(:post, path: path, query_params: query_params,
request_body: request_body, headers: headers)
response = api_operations.send(:post, path: path)

expect(response).to eq([mock_response[:data], mock_response[:request_id]])
expect(response).to eq([
mock_response[:data],
mock_response[:request_id],
mock_headers
])
end

it "returns a response with default query_params, request_body, and headers" do
Expand All @@ -154,7 +172,7 @@ def initialize(api_key, api_uri, timeout)

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

expect(response).to eq([mock_response[:data], mock_response[:request_id]])
expect(response).to eq([mock_response[:data], mock_response[:request_id], nil])
end
end
end
Expand Down
15 changes: 10 additions & 5 deletions spec/nylas/handler/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,25 @@ class TestHttpClient
describe "#execute" do
let(:mock_request) { instance_double("request", redirection_history: nil) }

it "returns the response" do
it "returns the response with headers" do
response_json = {
foo: "bar"
}
request_params = { method: :get, path: "https://test.api.nylas.com/foo", timeout: 30 }
mock_http_res = instance_double("response", to_hash: {}, code: 200,
headers: { content_type: "application/json" })
mock_headers = {
content_type: "application/json",
x_request_id: "123",
some_header: "value"
}
mock_http_res = instance_double("response", to_hash: {}, code: 200, headers: mock_headers)
mock_response = RestClient::Response.create(response_json.to_json, mock_http_res, mock_request)
mock_response.headers[:content_type] = "application/json"
mock_response.headers.merge!(mock_headers)

allow(RestClient::Request).to receive(:execute).and_yield(mock_response, mock_request, mock_http_res)

response = http_client.send(:execute, **request_params)

expect(response).to eq(response_json)
expect(response).to eq(response_json.merge(headers: mock_headers))
end

it "raises a timeout error" do
Expand Down