Class: Peddler::API
- Inherits:
-
Object
- Object
- Peddler::API
- 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::CustomerFeedback20240601, Peddler::APIs::DataKiosk20231115, Peddler::APIs::EasyShip20220323, Peddler::APIs::FBAInboundEligibilityV1, Peddler::APIs::FBAInventoryV1, Peddler::APIs::Feeds20210630, Peddler::APIs::Finances20240601, 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::SellerWallet20240301, 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::Vehicles20241101, 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
Instance Attribute Summary collapse
- #access_token ⇒ String readonly
- #endpoint ⇒ Peddler::Endpoint readonly
-
#retries ⇒ Integer
readonly
Number of retries if throttled (default: 0).
Instance Method Summary collapse
- #endpoint_uri ⇒ URI::HTTPS
- #http ⇒ HTTP::Client
-
#initialize(aws_region, access_token, retries: 0) ⇒ API
constructor
A new instance of API.
-
#meter(requests_per_second) ⇒ self
Throttles with a rate limit and retries when the API returns a 429.
-
#retriable(**options) ⇒ self
Retries requests if they fail due to socket or ‘5xx` errors.
-
#sandbox ⇒ self
Switches to the SP-API sandbox to make test calls.
- #sandbox? ⇒ Boolean
-
#typed ⇒ self
Enables typed response parsing.
- #typed? ⇒ Boolean
-
#use(*features) ⇒ self
Turn on [HTTP](github.com/httprb/http) features.
-
#via(*proxy) ⇒ self
(also: #through)
Make a request through an HTTP proxy.
Constructor Details
#initialize(aws_region, access_token, retries: 0) ⇒ API
Returns a new instance of API.
33 34 35 36 37 38 |
# File 'lib/peddler/api.rb', line 33 def initialize(aws_region, access_token, retries: 0) @endpoint = Endpoint.find(aws_region) @access_token = access_token @retries = retries @sandbox = false end |
Instance Attribute Details
#access_token ⇒ String (readonly)
23 24 25 |
# File 'lib/peddler/api.rb', line 23 def access_token @access_token end |
#endpoint ⇒ Peddler::Endpoint (readonly)
20 21 22 |
# File 'lib/peddler/api.rb', line 20 def endpoint @endpoint end |
#retries ⇒ Integer (readonly)
Number of retries if throttled (default: 0)
28 29 30 |
# File 'lib/peddler/api.rb', line 28 def retries @retries end |
Instance Method Details
#endpoint_uri ⇒ URI::HTTPS
41 42 43 |
# File 'lib/peddler/api.rb', line 41 def endpoint_uri sandbox? ? endpoint.sandbox : endpoint.production end |
#http ⇒ HTTP::Client
76 77 78 79 80 81 82 83 |
# File 'lib/peddler/api.rb', line 76 def http @http ||= HTTP.headers( "Host" => endpoint_uri.host, "User-Agent" => user_agent, "X-Amz-Access-Token" => access_token, "X-Amz-Date" => , ) end |
#meter(requests_per_second) ⇒ self
Throttles with a rate limit and retries when the API returns a 429
89 90 91 92 93 94 95 96 |
# File 'lib/peddler/api.rb', line 89 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 |
#retriable(**options) ⇒ self
Retries requests if they fail due to socket or ‘5xx` errors
116 117 118 119 120 121 |
# File 'lib/peddler/api.rb', line 116 [:via, :use, :retriable].each do |method| define_method(method) do |*args, **kwargs, &block| @http = http.send(method, *args, **kwargs, &block) self end end |
#sandbox ⇒ self
Switches to the SP-API sandbox to make test calls
63 64 65 66 |
# File 'lib/peddler/api.rb', line 63 def sandbox @sandbox = true self end |
#sandbox? ⇒ Boolean
69 70 71 |
# File 'lib/peddler/api.rb', line 69 def sandbox? @sandbox end |
#typed ⇒ self
Enables typed response parsing
47 48 49 50 51 52 |
# File 'lib/peddler/api.rb', line 47 def typed load_types @typed = true self end |
#typed? ⇒ Boolean
55 56 57 |
# File 'lib/peddler/api.rb', line 55 def typed? !!@typed end |
#use(*features) ⇒ self
Turn on [HTTP](github.com/httprb/http) features
116 117 118 119 120 121 |
# File 'lib/peddler/api.rb', line 116 [:via, :use, :retriable].each do |method| define_method(method) do |*args, **kwargs, &block| @http = http.send(method, *args, **kwargs, &block) self end end |
#via(*proxy) ⇒ self Also known as: through
Make a request through an HTTP proxy
116 117 118 119 120 121 |
# File 'lib/peddler/api.rb', line 116 [:via, :use, :retriable].each do |method| define_method(method) do |*args, **kwargs, &block| @http = http.send(method, *args, **kwargs, &block) self end end |