Skip to content

Commit

Permalink
- Use body for POST/PUT/PATCH and query for GET/DELETE requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nwithan8 committed Jul 29, 2024
1 parent d0f99e8 commit 3dde03d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/easypost/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
25 changes: 17 additions & 8 deletions lib/easypost/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions spec/support/fixture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 3dde03d

Please sign in to comment.