Class: Peddler::API

Inherits:
Object
  • Object
show all
Defined in:
lib/peddler/api.rb

Overview

Wraps an Amazon Selling Partner API (SP-API)

Direct Known Subclasses

Peddler::APIs::AmazonWarehousingAndDistribution20240509, Peddler::APIs::AplusContent20201101, Peddler::APIs::ApplicationIntegrations20240401, Peddler::APIs::ApplicationManagement20231130, Peddler::APIs::CatalogItems20201201, Peddler::APIs::CatalogItems20220401, Peddler::APIs::CatalogItemsV0, Peddler::APIs::DataKiosk20231115, Peddler::APIs::EasyShip20220323, Peddler::APIs::FBAInboundEligibilityV1, Peddler::APIs::FBAInventoryV1, Peddler::APIs::Feeds20210630, Peddler::APIs::Finances20240619, Peddler::APIs::FinancesV0, Peddler::APIs::FulfillmentInbound20240320, Peddler::APIs::FulfillmentInboundV0, Peddler::APIs::FulfillmentOutbound20200701, Peddler::APIs::Invoices20240619, Peddler::APIs::ListingsItems20200901, Peddler::APIs::ListingsItems20210801, Peddler::APIs::ListingsRestrictions20210801, Peddler::APIs::MerchantFulfillmentV0, Peddler::APIs::MessagingV1, Peddler::APIs::NotificationsV1, Peddler::APIs::OrdersV0, Peddler::APIs::ProductFeesV0, Peddler::APIs::ProductPricing20220501, Peddler::APIs::ProductPricingV0, Peddler::APIs::ProductTypeDefinitions20200901, Peddler::APIs::Replenishment20221107, Peddler::APIs::Reports20210630, Peddler::APIs::SalesV1, Peddler::APIs::SellersV1, Peddler::APIs::ServicesV1, Peddler::APIs::ShipmentInvoicingV0, Peddler::APIs::ShippingV1, Peddler::APIs::ShippingV2, Peddler::APIs::SolicitationsV1, Peddler::APIs::SupplySources20200701, Peddler::APIs::Tokens20210301, Peddler::APIs::Uploads20201101, Peddler::APIs::VendorDirectFulfillmentInventoryV1, Peddler::APIs::VendorDirectFulfillmentOrders20211228, Peddler::APIs::VendorDirectFulfillmentOrdersV1, Peddler::APIs::VendorDirectFulfillmentPaymentsV1, Peddler::APIs::VendorDirectFulfillmentSandboxTestData20211028, Peddler::APIs::VendorDirectFulfillmentShipping20211228, Peddler::APIs::VendorDirectFulfillmentShippingV1, Peddler::APIs::VendorDirectFulfillmentTransactions20211228, Peddler::APIs::VendorDirectFulfillmentTransactionsV1, Peddler::APIs::VendorInvoicesV1, Peddler::APIs::VendorOrdersV1, Peddler::APIs::VendorShipmentsV1, Peddler::APIs::VendorTransactionStatusV1

Defined Under Namespace

Classes: CannotSandbox, MustSandbox

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aws_region, access_token) ⇒ API

Returns a new instance of API.

Parameters:

  • aws_region (String)
  • access_token (String)


30
31
32
33
34
# File 'lib/peddler/api.rb', line 30

def initialize(aws_region, access_token)
  @endpoint = Endpoint.find(aws_region)
  @access_token = access_token
  @sandbox = false
end

Class Attribute Details

.parser#call

Returns:

  • (#call)


19
20
21
# File 'lib/peddler/api.rb', line 19

def parser
  @parser
end

Instance Attribute Details

#access_tokenString (readonly)

Returns:

  • (String)


26
27
28
# File 'lib/peddler/api.rb', line 26

def access_token
  @access_token
end

#endpointPeddler::Endpoint (readonly)

Returns:



23
24
25
# File 'lib/peddler/api.rb', line 23

def endpoint
  @endpoint
end

#parser#call

Returns:

  • (#call)


131
132
133
# File 'lib/peddler/api.rb', line 131

def parser
  @parser || self.class.parser
end

Instance Method Details

#endpoint_uriURI::HTTPS

Returns:

  • (URI::HTTPS)


37
38
39
# File 'lib/peddler/api.rb', line 37

def endpoint_uri
  sandbox? ? endpoint.sandbox : endpoint.production
end

#httpHTTP::Client



58
59
60
61
62
63
64
65
# File 'lib/peddler/api.rb', line 58

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) ⇒ self

Throttles with a rate limit and retries when the API returns a 429

Parameters:

  • rate_limit (Float)

    The delay in seconds before retrying

Returns:

  • (self)


71
72
73
74
75
76
77
78
# File 'lib/peddler/api.rb', line 71

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

#retriable(**options) ⇒ self

Retries requests if they fail due to socket or 5xx errors

Returns:

  • (self)


98
99
100
101
102
103
# File 'lib/peddler/api.rb', line 98

[:via, :use, :retriable].each do |method|
  define_method(method) do |*args, &block|
    @http = http.send(method, *args, &block)
    self
  end
end

#sandboxself

Switches to the SP-API sandbox to make test calls



45
46
47
48
# File 'lib/peddler/api.rb', line 45

def sandbox
  @sandbox = true
  self
end

#sandbox?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/peddler/api.rb', line 51

def sandbox?
  @sandbox
end

#use(*features) ⇒ self

Turn on HTTP features

Parameters:

  • features

Returns:

  • (self)


98
99
100
101
102
103
# File 'lib/peddler/api.rb', line 98

[:via, :use, :retriable].each do |method|
  define_method(method) do |*args, &block|
    @http = http.send(method, *args, &block)
    self
  end
end

#via(*proxy) ⇒ self Also known as: through

Make a request through an HTTP proxy

Parameters:

  • proxy (Array)

Returns:

  • (self)

Raises:

  • (HTTP::Request::Error)

    if HTTP proxy is invalid



98
99
100
101
102
103
# File 'lib/peddler/api.rb', line 98

[:via, :use, :retriable].each do |method|
  define_method(method) do |*args, &block|
    @http = http.send(method, *args, &block)
    self
  end
end