lib/nylas/http_client.rb in nylas-4.6.3 vs lib/nylas/http_client.rb in nylas-4.6.4
- old
+ new
@@ -5,20 +5,24 @@
# Plain HTTP client that can be used to interact with the Nylas API sans any type casting.
class HttpClient # rubocop:disable Metrics/ClassLength
HTTP_CODE_TO_EXCEPTIONS = {
400 => InvalidRequest,
+ 401 => UnauthorizedRequest,
402 => MessageRejected,
403 => AccessDenied,
404 => ResourceNotFound,
405 => MethodNotAllowed,
+ 410 => ResourceRemoved,
+ 418 => TeapotError,
422 => MailProviderError,
429 => SendingQuotaExceeded,
500 => InternalError,
501 => EndpointNotYetImplemented,
502 => BadGateway,
- 503 => ServiceUnavailable
+ 503 => ServiceUnavailable,
+ 504 => RequestTimedOut
}.freeze
ENDPOINT_TIMEOUTS = {
"/oauth/authorize" => 345,
"/messages/search" => 350,
@@ -80,11 +84,18 @@
query: query,
payload: payload,
timeout: timeout
)
rest_client_execute(**request) do |response, _request, result|
- response = parse_response(response)
+ content_type = nil
+
+ if response.headers && response.headers[:content_type]
+ content_type = response.headers[:content_type].downcase
+ end
+
+ response = parse_response(response) if content_type == "application/json"
+
handle_failed_response(result: result, response: response)
response
end
end
# rubocop:enable Metrics/MethodLength
@@ -134,9 +145,11 @@
def parse_response(response)
return response if response.is_a?(Enumerable)
json = StringIO.new(response)
Yajl::Parser.new(symbolize_names: true).parse(json)
+ rescue Yajl::ParseError
+ raise Nylas::JsonParseError
end
inform_on :parse_response, level: :debug, also_log: { result: true }
def url_for_path(path)
protocol, domain = api_server.split("//")