lib/nylas/http_client.rb in nylas-5.0.0 vs lib/nylas/http_client.rb in nylas-5.1.0
- old
+ new
@@ -102,12 +102,12 @@
# rubocop:enable Metrics/MethodLength
inform_on :execute, level: :debug,
also_log: { result: true, values: %i[method url path headers query payload] }
def build_request(method:, path: nil, headers: {}, query: {}, payload: nil, timeout: nil)
- headers[:params] = query
url ||= url_for_path(path)
+ url = add_query_params_to_url(url, query)
resulting_headers = default_headers.merge(headers)
{ method: method, url: url, payload: payload, headers: resulting_headers, timeout: timeout }
end
# Syntactical sugar for making GET requests via the API.
@@ -176,12 +176,41 @@
raise UnexpectedResponse, result.msg if result.is_a?(Net::HTTPClientError)
end
def handle_anticipated_failure_mode(http_code:, response:)
return if http_code == 200
- return unless response.is_a?(Hash)
exception = HTTP_CODE_TO_EXCEPTIONS.fetch(http_code, APIError)
- raise exception.new(response[:type], response[:message], response.fetch(:server_error, nil))
+ parsed_response = parse_response(response)
+
+ raise exception.new(
+ parsed_response[:type],
+ parsed_response[:message],
+ parsed_response.fetch(:server_error, nil)
+ )
+ end
+
+ def add_query_params_to_url(url, query)
+ unless query.empty?
+ uri = URI.parse(url)
+ query = custom_params(query)
+ params = URI.decode_www_form(uri.query || "") + query.to_a
+ uri.query = URI.encode_www_form(params)
+ url = uri.to_s
+ end
+
+ url
+ end
+
+ def custom_params(query)
+ # Convert hash to "<key>:<value>" form for metadata_pair query
+ if query.key?(:metadata_pair)
+ pairs = query[:metadata_pair].map do |key, value|
+ "#{key}:#{value}"
+ end
+ query[:metadata_pair] = pairs
+ end
+
+ query
end
end
end