Skip to content

Commit 02fe10c

Browse files
samuelpxmatiassalles99AaronDDM
authored
Including headers in response #2 (fix tests/CI) (#534)
Co-authored-by: Matias Salles <matiassalles99@gmail.com> Co-authored-by: Aaron de Mello <aaron.d@nylas.com>
1 parent 5195572 commit 02fe10c

File tree

4 files changed

+64
-22
lines changed

4 files changed

+64
-22
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
### [Unreleased]
4+
* Added access to response headers
5+
36
### [6.6.0]
47
* Added support for `single_level` query parameter in Folders API for Microsoft accounts
58
* Added support for `include_hidden_folders` query parameter in folders list endpoint for Microsoft accounts to control whether hidden folders are included in the response

lib/nylas/handler/http_client.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def execute(method:, path:, timeout:, headers: {}, query: {}, payload: nil, api_
4141
content_type = response.headers["content-type"].downcase
4242
end
4343

44-
parsed_response = parse_json_evaluate_error(result.code.to_i, response.body, path, content_type)
44+
parsed_response = parse_json_evaluate_error(result.code.to_i, response.body, path, content_type,
45+
response.headers)
4546
# Include headers in the response
4647
parsed_response[:headers] = response.headers unless parsed_response.nil?
4748
parsed_response
@@ -311,37 +312,34 @@ def handle_response(http, get_request, path, &block)
311312
end
312313

313314
# Parses the response from the Nylas API and evaluates for errors.
314-
def parse_json_evaluate_error(http_code, response, path, content_type = nil)
315+
def parse_json_evaluate_error(http_code, response, path, content_type = nil, headers = nil)
315316
begin
316317
response = parse_response(response) if content_type == "application/json"
317318
rescue Nylas::JsonParseError
318-
handle_failed_response(http_code, response, path)
319+
handle_failed_response(http_code, response, path, headers)
319320
raise
320321
end
321322

322-
handle_failed_response(http_code, response, path)
323+
handle_failed_response(http_code, response, path, headers)
323324
response
324325
end
325326

326327
# Handles failed responses from the Nylas API.
327-
def handle_failed_response(http_code, response, path)
328+
def handle_failed_response(http_code, response, path, headers = nil)
328329
return if HTTP_SUCCESS_CODES.include?(http_code)
329330

330331
case response
331332
when Hash
332-
raise error_hash_to_exception(response, http_code, path)
333+
raise error_hash_to_exception(response, http_code, path, headers)
333334
else
334335
raise NylasApiError.parse_error_response(response, http_code)
335336
end
336337
end
337338

338339
# Converts error hashes to exceptions.
339-
def error_hash_to_exception(response, status_code, path)
340+
def error_hash_to_exception(response, status_code, path, headers = nil)
340341
return if !response || !response.key?(:error)
341342

342-
# Safely get headers without risking KeyError
343-
headers = response.key?(:headers) ? response[:headers] : nil
344-
345343
if %W[#{api_uri}/v3/connect/token #{api_uri}/v3/connect/revoke].include?(path)
346344
NylasOAuthError.new(response[:error], response[:error_description], response[:error_uri],
347345
response[:error_code], status_code)

spec/nylas/handler/http_client_spec.rb

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,35 @@ class TestHttpClient
318318
expect(response).to eq(response_json.merge(headers: mock_headers))
319319
end
320320

321+
it "returns an error with headers" do
322+
response_json = {
323+
foo: "bar",
324+
error: {
325+
type: "api_error",
326+
message: "An unexpected error occurred",
327+
provider_error: "This is the provider error"
328+
}
329+
}
330+
request_params = { method: :get, path: "https://test.api.nylas.com/foo", timeout: 30 }
331+
mock_headers = {
332+
"content-type" => "application/json",
333+
"x-request-id" => "123",
334+
"some-header" => "value"
335+
}
336+
mock_response = instance_double("HTTParty::Response",
337+
body: response_json.to_json,
338+
headers: mock_headers,
339+
code: 429)
340+
341+
allow(HTTParty).to receive(:get).and_return(mock_response)
342+
343+
expect do
344+
http_client.send(:execute, **request_params)
345+
end.to raise_error(Nylas::NylasApiError) { |error|
346+
expect(error.headers).to eq(mock_headers)
347+
}
348+
end
349+
321350
it "raises a timeout error" do
322351
request_params = { method: :get, path: "https://test.api.nylas.com/foo", timeout: 30 }
323352
allow(HTTParty).to receive(:get).and_raise(Net::OpenTimeout)
@@ -462,22 +491,23 @@ class TestHttpClient
462491
type: "api_error",
463492
message: "An unexpected error occurred",
464493
provider_error: "This is the provider error"
465-
},
466-
headers: {
467-
"x-request-id": "request-id-from-headers",
468-
"x-ratelimit-limit": "100",
469-
"x-ratelimit-remaining": "99"
470494
}
471495
}
496+
headers = {
497+
"x-request-id": "request-id-from-headers",
498+
"x-ratelimit-limit": "100",
499+
"x-ratelimit-remaining": "99"
500+
}
472501

473-
err_obj = http_client.send(:error_hash_to_exception, response, 400, "https://test.api.nylas.com/foo")
502+
err_obj = http_client.send(:error_hash_to_exception, response, 400, "https://test.api.nylas.com/foo",
503+
headers)
474504

475505
expect(err_obj).to be_a(Nylas::NylasApiError)
476506
expect(err_obj.message).to eq("An unexpected error occurred")
477507
expect(err_obj.request_id).to eq("request-id")
478508
expect(err_obj.provider_error).to eq("This is the provider error")
479509
expect(err_obj.type).to eq("api_error")
480-
expect(err_obj.headers).to eq(response[:headers])
510+
expect(err_obj.headers).to eq(headers)
481511
end
482512
end
483513

@@ -555,11 +585,17 @@ class TestHttpClient
555585
provider_error: "This is the provider error"
556586
}
557587
}
588+
headers = {
589+
"x-request-id": "request-id-from-headers",
590+
"x-ratelimit-limit": "100"
591+
}
558592

559593
expect do
560594
http_client.send(:parse_json_evaluate_error, 400, response.to_json,
561-
"https://test.api.nylas.com/foo", "application/json")
562-
end.to raise_error(Nylas::NylasApiError)
595+
"https://test.api.nylas.com/foo", "application/json", headers)
596+
end.to raise_error(Nylas::NylasApiError) { |error|
597+
expect(error.headers).to eq(headers)
598+
}
563599
end
564600

565601
it "raises a NylasApiError for a non-JSON response" do

spec/spec_helper.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# frozen_string_literal: true
22

33
require "simplecov"
4+
require "simplecov-cobertura"
5+
46
SimpleCov.start do
57
add_filter "/spec/"
6-
end
78

8-
require "simplecov-cobertura"
9-
SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter
9+
# Use multiple formatters to ensure coverage data is available
10+
formatter SimpleCov::Formatter::MultiFormatter.new([
11+
SimpleCov::Formatter::HTMLFormatter,
12+
SimpleCov::Formatter::CoberturaFormatter
13+
])
14+
end
1015

1116
require "nylas"
1217
require "support/nylas_helpers"

0 commit comments

Comments
 (0)