diff --git a/lib/easypost/client.rb b/lib/easypost/client.rb index d7854e5..522cf12 100644 --- a/lib/easypost/client.rb +++ b/lib/easypost/client.rb @@ -74,17 +74,17 @@ def initialize(api_key:, read_timeout: 60, open_timeout: 30, api_base: 'https:// # # @param method [Symbol] the HTTP Verb (get, method, put, post, etc.) # @param endpoint [String] URI path of the resource - # @param body [Object] (nil) object to be dumped to JSON + # @param params [Object] (nil) object to be used as the request parameters # @param api_version [String] the version of API to hit # @raise [EasyPost::Error] if the response has a non-2xx status code # @return [Hash] JSON object parsed from the response body def make_request( method, endpoint, - body = nil, + params = nil, api_version = EasyPost::InternalUtilities::Constants::API_VERSION ) - response = @http_client.request(method, endpoint, nil, body, api_version) + response = @http_client.request(method, endpoint, nil, params, api_version) potential_error = EasyPost::Errors::ApiError.handle_api_error(response) raise potential_error unless potential_error.nil? diff --git a/lib/easypost/http_client.rb b/lib/easypost/http_client.rb index 4263bb6..40d0b6c 100644 --- a/lib/easypost/http_client.rb +++ b/lib/easypost/http_client.rb @@ -14,26 +14,35 @@ def request( method, path, headers = nil, - body = nil, + params = nil, api_version = EasyPost::InternalUtilities::Constants::API_VERSION ) # Remove leading slash from path. path = path[1..] if path[0] == '/' - uri = URI.parse("#{@base_url}/#{api_version}/#{path}") headers = @config[:headers].merge(headers || {}) - serialized_body = JSON.dump(EasyPost::InternalUtilities.objects_to_ids(body)) if body open_timeout = @config[:open_timeout] read_timeout = @config[:read_timeout] request_timestamp = Time.now request_uuid = SecureRandom.uuid + uri = URI.parse("#{@base_url}/#{api_version}/#{path}") + serialized_body = nil + + if params + if [:get, :delete].include?(method) + uri.query = URI.encode_www_form(params) + else + serialized_body = JSON.dump(EasyPost::InternalUtilities.objects_to_ids(params)) + end + end + if EasyPost::Hooks.any_subscribers?(:request) request_context = EasyPost::Hooks::RequestContext.new( method: method, path: uri.to_s, headers: headers, - request_body: body, + request_body: serialized_body, request_timestamp: request_timestamp, request_uuid: request_uuid, ) @@ -66,10 +75,10 @@ def request( # client_response_object attribute if response.is_a?(Net::HTTPResponse) response_body = begin - JSON.parse(response.body) - rescue JSON::ParseError - response.body - end + JSON.parse(response.body) + rescue JSON::ParseError + response.body + end response_context.merge!( { http_status: response.code.to_i, diff --git a/spec/support/fixture.rb b/spec/support/fixture.rb index 007f8b7..c0d0069 100644 --- a/spec/support/fixture.rb +++ b/spec/support/fixture.rb @@ -96,7 +96,7 @@ def self.one_call_buy_shipment # If you need to re-record cassettes, increment the date below and ensure it is one day in the future, # USPS only does "next-day" pickups including Saturday but not Sunday or Holidays. def self.basic_pickup - pickup_date = '2024-01-24' + pickup_date = '2024-07-31' pickup_data = read_fixture_data['pickups']['basic'] pickup_data['min_datetime'] = pickup_date @@ -144,11 +144,11 @@ def self.rma_form_options end def self.planned_ship_date - '2024-07-16' + '2024-07-31' end def self.desired_delivery_date - '2024-07-16' + '2024-07-31' end # This fixture will require you to append a `tracking_code` key with the shipment's tracking code,