Skip to content

Commit 11e72bd

Browse files
committed
Adding responde headers to Ruby SDK
1 parent 696d2c4 commit 11e72bd

File tree

4 files changed

+59
-33
lines changed

4 files changed

+59
-33
lines changed

lib/nylas/handler/api_operations.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ module Get
1515
#
1616
# @param path [String] Destination path for the call.
1717
# @param query_params [Hash, {}] Query params to pass to the call.
18-
# @return [Array([Hash, Array], String)] Nylas data object and API Request ID.
18+
# @return [Array([Hash, Array], String, Hash)] Nylas data object, API Request ID, and response headers.
1919
def get(path:, query_params: {})
2020
response = get_raw(path: path, query_params: query_params)
2121

22-
[response[:data], response[:request_id]]
22+
[response[:data], response[:request_id], response[:headers]]
2323
end
2424

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

33-
[response[:data], response[:request_id], response[:next_cursor]]
33+
[response[:data], response[:request_id], response[:next_cursor], response[:headers]]
3434
end
3535

3636
private
@@ -63,7 +63,7 @@ module Post
6363
# @param query_params [Hash, {}] Query params to pass to the call.
6464
# @param request_body [Hash, nil] Request body to pass to the call.
6565
# @param headers [Hash, {}] Additional HTTP headers to include in the payload.
66-
# @return Nylas data object and API Request ID.
66+
# @return [Array(Hash, String, Hash)] Nylas data object, API Request ID, and response headers.
6767
def post(path:, query_params: {}, request_body: nil, headers: {})
6868
response = execute(
6969
method: :post,
@@ -75,7 +75,7 @@ def post(path:, query_params: {}, request_body: nil, headers: {})
7575
timeout: timeout
7676
)
7777

78-
[response[:data], response[:request_id]]
78+
[response[:data], response[:request_id], response[:headers]]
7979
end
8080
end
8181

lib/nylas/handler/http_client.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ def execute(method:, path:, timeout:, headers: {}, query: {}, payload: nil, api_
4040
content_type = response.headers[:content_type].downcase
4141
end
4242

43-
parse_json_evaluate_error(result.code.to_i, response, path, content_type)
43+
parsed_response = parse_json_evaluate_error(result.code.to_i, response, path, content_type)
44+
# Include headers in the response
45+
parsed_response[:headers] = response.headers unless parsed_response.nil?
46+
parsed_response
4447
end
4548
rescue RestClient::Exceptions::OpenTimeout, RestClient::Exceptions::ReadTimeout
4649
raise Nylas::NylasSdkTimeoutError.new(request[:path], timeout)

spec/nylas/handler/api_operations_spec.rb

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,28 @@ def initialize(api_key, api_uri, timeout)
3232

3333
describe Nylas::ApiOperations::Get do
3434
describe "#get" do
35-
it "returns a response" do
35+
it "returns response data, request_id and headers" do
3636
path = "#{api_uri}/path"
3737
query_params = { foo: "bar" }
38+
mock_headers = { "X-Request-Id" => "123", "Content-Type" => "application/json" }
39+
response_with_headers = mock_response.merge(headers: mock_headers)
40+
3841
allow(api_operations).to receive(:execute).with(
3942
method: :get,
4043
path: path,
4144
query: query_params,
4245
payload: nil,
4346
api_key: api_key,
4447
timeout: timeout
45-
).and_return(mock_response)
48+
).and_return(response_with_headers)
4649

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

49-
expect(response).to eq([mock_response[:data], mock_response[:request_id]])
52+
expect(response).to eq([
53+
mock_response[:data],
54+
mock_response[:request_id],
55+
mock_headers
56+
])
5057
end
5158

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

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

65-
expect(response).to eq([mock_response[:data], mock_response[:request_id]])
72+
expect(response).to eq([mock_response[:data], mock_response[:request_id], nil])
6673
end
6774
end
6875

@@ -76,11 +83,15 @@ def initialize(api_key, api_uri, timeout)
7683
foo: "bar"
7784
}
7885
],
79-
next_cursor: "mock_cursor"
86+
next_cursor: "mock_cursor",
87+
headers: {
88+
"X-Request-Id" => "123",
89+
"Content-Type" => "application/json"
90+
}
8091
}
8192
end
8293

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

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

97-
expect(response).to eq([list_response[:data], list_response[:request_id],
98-
list_response[:next_cursor]])
108+
expect(response).to eq([
109+
list_response[:data],
110+
list_response[:request_id],
111+
list_response[:next_cursor],
112+
list_response[:headers]
113+
])
99114
end
100115

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

114129
expect(response).to eq([list_response[:data], list_response[:request_id],
115-
list_response[:next_cursor]])
130+
list_response[:next_cursor], list_response[:headers]])
116131
end
117132
end
118133
end
119134

120135
describe Nylas::ApiOperations::Post do
121136
describe "#post" do
122-
it "returns a response" do
137+
it "returns response with headers" do
123138
path = "#{api_uri}/path"
124-
query_params = { foo: "bar" }
125-
request_body = { foo: "bar" }
126-
headers = { "Content-Type" => "application/json" }
139+
mock_headers = { "X-Request-Id" => "123", "Content-Type" => "application/json" }
140+
response_with_headers = mock_response.merge(headers: mock_headers)
141+
127142
allow(api_operations).to receive(:execute).with(
128143
method: :post,
129144
path: path,
130-
query: query_params,
131-
payload: request_body,
132-
headers: headers,
145+
query: {},
146+
payload: nil,
147+
headers: {},
133148
api_key: api_key,
134149
timeout: timeout
135-
).and_return(mock_response)
150+
).and_return(response_with_headers)
136151

137-
response = api_operations.send(:post, path: path, query_params: query_params,
138-
request_body: request_body, headers: headers)
152+
response = api_operations.send(:post, path: path)
139153

140-
expect(response).to eq([mock_response[:data], mock_response[:request_id]])
154+
expect(response).to eq([
155+
mock_response[:data],
156+
mock_response[:request_id],
157+
mock_headers
158+
])
141159
end
142160

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

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

157-
expect(response).to eq([mock_response[:data], mock_response[:request_id]])
175+
expect(response).to eq([mock_response[:data], mock_response[:request_id], nil])
158176
end
159177
end
160178
end

spec/nylas/handler/http_client_spec.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,25 @@ class TestHttpClient
155155
describe "#execute" do
156156
let(:mock_request) { instance_double("request", redirection_history: nil) }
157157

158-
it "returns the response" do
158+
it "returns the response with headers" do
159159
response_json = {
160160
foo: "bar"
161161
}
162162
request_params = { method: :get, path: "https://test.api.nylas.com/foo", timeout: 30 }
163-
mock_http_res = instance_double("response", to_hash: {}, code: 200,
164-
headers: { content_type: "application/json" })
163+
mock_headers = {
164+
content_type: "application/json",
165+
x_request_id: "123",
166+
some_header: "value"
167+
}
168+
mock_http_res = instance_double("response", to_hash: {}, code: 200, headers: mock_headers)
165169
mock_response = RestClient::Response.create(response_json.to_json, mock_http_res, mock_request)
166-
mock_response.headers[:content_type] = "application/json"
170+
mock_response.headers.merge!(mock_headers)
171+
167172
allow(RestClient::Request).to receive(:execute).and_yield(mock_response, mock_request, mock_http_res)
168173

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

171-
expect(response).to eq(response_json)
176+
expect(response).to eq(response_json.merge(headers: mock_headers))
172177
end
173178

174179
it "raises a timeout error" do

0 commit comments

Comments
 (0)