lib/nylas/http_client.rb in nylas-5.2.0 vs lib/nylas/http_client.rb in nylas-5.3.0
- old
+ new
@@ -3,11 +3,11 @@
module Nylas
require "yajl"
# Plain HTTP client that can be used to interact with the Nylas API sans any type casting.
class HttpClient # rubocop:disable Metrics/ClassLength
- HTTP_SUCCESS_CODES = [200, 302].freeze
+ HTTP_SUCCESS_CODES = [200, 201, 302].freeze
HTTP_CODE_TO_EXCEPTIONS = {
400 => InvalidRequest,
401 => UnauthorizedRequest,
402 => MessageRejected,
@@ -90,11 +90,16 @@
if response.headers && response.headers[:content_type]
content_type = response.headers[:content_type].downcase
end
- response = parse_response(response) if content_type == "application/json"
+ begin
+ response = parse_response(response) if content_type == "application/json"
+ rescue Nylas::JsonParseError
+ handle_failed_response(result: result, response: response)
+ raise
+ end
handle_failed_response(result: result, response: response)
response
end
end
@@ -137,11 +142,11 @@
@default_headers ||= {
"X-Nylas-API-Wrapper" => "ruby",
"X-Nylas-Client-Id" => @app_id,
"Nylas-API-Version" => SUPPORTED_API_VERSION,
"User-Agent" => "Nylas Ruby SDK #{Nylas::VERSION} - #{RUBY_VERSION}",
- "Content-types" => "application/json"
+ "Content-type" => "application/json"
}
end
def parse_response(response)
return response if response.is_a?(Enumerable)
@@ -177,15 +182,15 @@
def handle_anticipated_failure_mode(http_code:, response:)
return if HTTP_SUCCESS_CODES.include?(http_code)
exception = HTTP_CODE_TO_EXCEPTIONS.fetch(http_code, APIError)
- parsed_response = parse_response(response)
+ raise exception.new(http_code, response) unless response.is_a?(Hash)
raise exception.new(
- parsed_response[:type],
- parsed_response[:message],
- parsed_response.fetch(:server_error, nil)
+ response[:type],
+ response[:message],
+ response.fetch(:server_error, nil)
)
end
def add_query_params_to_url(url, query)
unless query.empty?