class Peddler::API
Wraps an Amazon Selling Partner API
(SP-API)
Attributes
access_token[R]
@return [String]
endpoint[R]
@return [Peddler::Endpoint]
Public Class Methods
new(aws_region, access_token)
click to toggle source
@param [String] aws_region @param [String] access_token
# File lib/peddler/api.rb, line 23 def initialize(aws_region, access_token) @endpoint = Endpoint.find(aws_region) @access_token = access_token @sandbox = false end
Public Instance Methods
cannot_sandbox!()
click to toggle source
@raise [CannotSandbox] if in a sandbox environment
# File lib/peddler/api.rb, line 47 def cannot_sandbox! raise CannotSandbox, "cannot run in a sandbox" if sandbox? end
endpoint_uri()
click to toggle source
@return [URI::HTTPS]
# File lib/peddler/api.rb, line 30 def endpoint_uri sandbox? ? endpoint.sandbox : endpoint.production end
http()
click to toggle source
@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]
# File lib/peddler/api.rb, line 59 def http @http ||= HTTP.headers( "Host" => endpoint_uri.host, "User-Agent" => user_agent, "X-Amz-Access-Token" => access_token, "X-Amz-Date" => timestamp, ) end
meter(rate_limit)
click to toggle source
Throttles with a rate limit and retries when the API
returns a 429
@param [Float] rate_limit The delay in seconds before retrying @return [self]
# File lib/peddler/api.rb, line 72 def meter(rate_limit) # HTTP v6.0 will implement retriable. Until then, point to their GitHub repo, or it's a no-op. # https://github.com/httprb/http/pull/790 delay = sandbox? ? 0.2 : 1.0 / rate_limit retriable(delay: delay, retry_statuses: [429]) if @http.respond_to?(:retriable) self end
must_sandbox!()
click to toggle source
@raise [MustSandbox] unless in a sandbox environment
# File lib/peddler/api.rb, line 52 def must_sandbox! raise MustSandbox, "must run in a sandbox" unless sandbox? end
sandbox()
click to toggle source
@see developer-docs.amazon.com/sp-api/docs/the-selling-partner-api-sandbox @return [self]
# File lib/peddler/api.rb, line 36 def sandbox @sandbox = true self end
sandbox?()
click to toggle source
@return [Boolean]
# File lib/peddler/api.rb, line 42 def sandbox? @sandbox end
Private Instance Methods
timestamp()
click to toggle source
# File lib/peddler/api.rb, line 134 def timestamp Time.now.utc.strftime("%Y%m%dT%H%M%SZ") end
user_agent()
click to toggle source
# File lib/peddler/api.rb, line 130 def user_agent "Peddler/#{Peddler::VERSION} (Language=Ruby; #{Socket.gethostname})" end