Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Use body for POST/PUT/PATCH and query for GET/DELETE requests #310

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
nwithan8 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading