Class: Peddler::API

Inherits:
Object
  • Object
show all
Defined in:
lib/peddler/api.rb,
lib/peddler/api/sales_v1.rb,
lib/peddler/api/orders_v0.rb,
lib/peddler/api/sellers_v1.rb,
lib/peddler/api/finances_v0.rb,
lib/peddler/api/services_v1.rb,
lib/peddler/api/shipping_v1.rb,
lib/peddler/api/shipping_v2.rb,
lib/peddler/api/messaging_v1.rb,
lib/peddler/api/product_fees_v0.rb,
lib/peddler/api/catalog_items_v0.rb,
lib/peddler/api/fba_inventory_v1.rb,
lib/peddler/api/feeds_2021_06_30.rb,
lib/peddler/api/notifications_v1.rb,
lib/peddler/api/solicitations_v1.rb,
lib/peddler/api/vendor_orders_v1.rb,
lib/peddler/api/tokens_2021_03_01.rb,
lib/peddler/api/product_pricing_v0.rb,
lib/peddler/api/reports_2021_06_30.rb,
lib/peddler/api/uploads_2020_11_01.rb,
lib/peddler/api/vendor_invoices_v1.rb,
lib/peddler/api/finances_2024_06_19.rb,
lib/peddler/api/invoices_2024_06_19.rb,
lib/peddler/api/vendor_shipments_v1.rb,
lib/peddler/api/easy_ship_2022_03_23.rb,
lib/peddler/api/data_kiosk_2023_11_15.rb,
lib/peddler/api/shipment_invoicing_v0.rb,
lib/peddler/api/fulfillment_inbound_v0.rb,
lib/peddler/api/merchant_fulfillment_v0.rb,
lib/peddler/api/aplus_content_2020_11_01.rb,
lib/peddler/api/catalog_items_2020_12_01.rb,
lib/peddler/api/catalog_items_2022_04_01.rb,
lib/peddler/api/replenishment_2022_11_07.rb,
lib/peddler/api/listings_items_2020_09_01.rb,
lib/peddler/api/listings_items_2021_08_01.rb,
lib/peddler/api/supply_sources_2020_07_01.rb,
lib/peddler/api/fba_inbound_eligibility_v1.rb,
lib/peddler/api/product_pricing_2022_05_01.rb,
lib/peddler/api/vendor_transaction_status_v1.rb,
lib/peddler/api/fulfillment_inbound_2024_03_20.rb,
lib/peddler/api/fulfillment_outbound_2020_07_01.rb,
lib/peddler/api/listings_restrictions_2021_08_01.rb,
lib/peddler/api/application_management_2023_11_30.rb,
lib/peddler/api/application_integrations_2024_04_01.rb,
lib/peddler/api/product_type_definitions_2020_09_01.rb,
lib/peddler/api/vendor_direct_fulfillment_orders_v1.rb,
lib/peddler/api/vendor_direct_fulfillment_payments_v1.rb,
lib/peddler/api/vendor_direct_fulfillment_shipping_v1.rb,
lib/peddler/api/vendor_direct_fulfillment_inventory_v1.rb,
lib/peddler/api/vendor_direct_fulfillment_transactions_v1.rb,
lib/peddler/api/vendor_direct_fulfillment_orders_2021_12_28.rb,
lib/peddler/api/vendor_direct_fulfillment_shipping_2021_12_28.rb,
lib/peddler/api/amazon_warehousing_and_distribution_2024_05_09.rb,
lib/peddler/api/vendor_direct_fulfillment_transactions_2021_12_28.rb,
lib/peddler/api/vendor_direct_fulfillment_sandbox_test_data_2021_10_28.rb

Overview

Wraps an Amazon Selling Partner API (SP-API)

Defined Under Namespace

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

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)


23
24
25
26
27
# 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

Instance Attribute Details

#access_tokenString (readonly)

Returns:

  • (String)


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

def access_token
  @access_token
end

#endpointPeddler::Endpoint (readonly)

Returns:



16
17
18
# File 'lib/peddler/api.rb', line 16

def endpoint
  @endpoint
end

Instance Method Details

#cannot_sandbox!Object

Raises:



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

def cannot_sandbox!
  raise CannotSandbox, "cannot run in a sandbox" if sandbox?
end

#endpoint_uriURI::HTTPS

Returns:

  • (URI::HTTPS)


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

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

#httpHTTP::Client



59
60
61
62
63
64
65
66
# 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) ⇒ 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)


72
73
74
75
76
77
78
79
# 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!Object

Raises:



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

def must_sandbox!
  raise MustSandbox, "must run in a sandbox" unless sandbox?
end

#retriable(**options) ⇒ self

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

Returns:

  • (self)


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

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

#sandboxself



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

def sandbox
  @sandbox = true
  self
end

#sandbox?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/peddler/api.rb', line 42

def sandbox?
  @sandbox
end

#use(*features) ⇒ self

Turn on [HTTP](github.com/httprb/http) features

Parameters:

  • features

Returns:

  • (self)


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

[: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



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

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