class Peddler::API
Wraps an Amazon Selling Partner API
(SP-API)
Attributes
@return [#call]
@return [String]
@return [Peddler::Endpoint]
Number of retries if throttled (default: 0)
@return [Integer]
Public Class Methods
Source
# File lib/peddler/api.rb, line 36 def initialize(aws_region, access_token, retries: 0) @endpoint = Endpoint.find(aws_region) @access_token = access_token @retries = retries @sandbox = false end
@param [String] aws_region The AWS region to use for the endpoint @param [String] access_token
The access token for authentication @param [Integer] retries The number of retries if throttled (default: 0)
Public Instance Methods
Source
# File lib/peddler/api.rb, line 44 def endpoint_uri sandbox? ? endpoint.sandbox : endpoint.production end
@return [URI::HTTPS]
Source
# File lib/peddler/api.rb, line 65 def http @http ||= HTTP.headers( "Host" => endpoint_uri.host, "User-Agent" => user_agent, "X-Amz-Access-Token" => access_token, "X-Amz-Date" => timestamp, ) end
@see developer-docs.amazon.com/sp-api/docs/include-a-user-agent-header-in-all-requests @see developer-docs.amazon.com/amazon-shipping/docs/connecting-to-the-selling-partner-api#step-3-add-headers-to-the-uri @return [HTTP::Client]
Source
# File lib/peddler/api.rb, line 78 def meter(requests_per_second) return self if retries.zero? delay = sandbox? ? 0.2 : 1.0 / requests_per_second retriable(delay:, tries: retries + 1, retry_statuses: [429]) self end
Throttles with a rate limit and retries when the API
returns a 429
@param [Float] requests_per_second @return [self]
Source
# File lib/peddler/api.rb, line 132 def parser @parser || self.class.parser end
@!attribute parser @return [#call]
Source
# File lib/peddler/api.rb, line 52 def sandbox @sandbox = true self end
Switches to the SP-API sandbox to make test calls
@see developer-docs.amazon.com/sp-api/docs/the-selling-partner-api-sandbox @return [self]
Private Instance Methods
Source
# File lib/peddler/api.rb, line 138 def cannot_sandbox! raise CannotSandbox, "cannot run in a sandbox" if sandbox? end
Source
# File lib/peddler/api.rb, line 142 def must_sandbox! raise MustSandbox, "must run in a sandbox" unless sandbox? end
Source
# File lib/peddler/api.rb, line 155 def percent_encode(component) URI.encode_uri_component(component) end
Encodes URL path components
Source
# File lib/peddler/api.rb, line 160 def stringify_array(val) val.is_a?(Array) ? val.join(",") : val end
Converts an array to a comma-separated string, or returns the value as-is if not an array
Source
# File lib/peddler/api.rb, line 150 def timestamp Time.now.utc.strftime("%Y%m%dT%H%M%SZ") end
Source
# File lib/peddler/api.rb, line 146 def user_agent "Peddler/#{Peddler::VERSION} (Language=Ruby; #{Socket.gethostname})" end