Class: Peddler::API
- Inherits:
-
Object
show all
- Defined in:
- lib/peddler/api.rb,
sig/peddler/api.rbs
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::DeliveryByAmazon20220701, Peddler::APIs::EasyShip20220323, Peddler::APIs::ExternalFulfillmentInventory20240911, Peddler::APIs::ExternalFulfillmentReturns20240911, Peddler::APIs::ExternalFulfillmentShipments20240911, 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::Orders20260101, 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::Transfers20240601, 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
Instance Method Summary
collapse
-
#cannot_sandbox! ⇒ void
-
#delete(path, rate_limit: nil, parser: nil, **options) ⇒ Response
-
#endpoint_uri ⇒ URI::HTTPS
-
#get(path, rate_limit: nil, parser: nil, **options) ⇒ Response
-
#http(rate_limit: nil) ⇒ HTTP::Session
-
#initialize(aws_region, access_token, retries: 0, http: HTTP::Client.new) ⇒ API
constructor
-
#must_sandbox! ⇒ void
-
#patch(path, rate_limit: nil, parser: nil, **options) ⇒ Response
-
#percent_encode(component) ⇒ String
-
#post(path, rate_limit: nil, parser: nil, **options) ⇒ Response
-
#put(path, rate_limit: nil, parser: nil, **options) ⇒ Response
-
#request(method, path, rate_limit: nil, parser: nil, **options) ⇒ Response
-
#sandbox ⇒ self
Switches to the SP-API sandbox to make test calls.
-
#sandbox? ⇒ Boolean
-
#stringify_array(val) ⇒ String?
-
#timestamp ⇒ String
-
#user_agent ⇒ String
Constructor Details
#initialize(aws_region, access_token, retries: 0, http: HTTP::Client.new) ⇒ API
Returns a new instance of API.
27
28
29
30
31
32
33
|
# File 'lib/peddler/api.rb', line 27
def initialize(aws_region, access_token, retries: 0, http: HTTP::Client.new)
@endpoint = Endpoint.find(aws_region)
@access_token = access_token
@retries = retries
@http = http
@sandbox = false
end
|
Instance Attribute Details
#access_token ⇒ String
16
17
18
|
# File 'lib/peddler/api.rb', line 16
def access_token
@access_token
end
|
13
14
15
|
# File 'lib/peddler/api.rb', line 13
def endpoint
@endpoint
end
|
#retries ⇒ Integer
Number of retries if throttled (default: 0)
21
22
23
|
# File 'lib/peddler/api.rb', line 21
def retries
@retries
end
|
Instance Method Details
#cannot_sandbox! ⇒ void
This method returns an undefined value.
102
103
104
|
# File 'lib/peddler/api.rb', line 102
def cannot_sandbox!
raise CannotSandbox, "cannot run in a sandbox" if sandbox?
end
|
#delete(path, rate_limit: nil, parser: nil, **options) ⇒ Response
85
86
87
|
# File 'lib/peddler/api.rb', line 85
def delete(path, rate_limit: nil, parser: nil, **options)
request(:delete, path, rate_limit:, parser:, **options)
end
|
#endpoint_uri ⇒ URI::HTTPS
36
37
38
|
# File 'lib/peddler/api.rb', line 36
def endpoint_uri
sandbox? ? endpoint.sandbox : endpoint.production
end
|
#get(path, rate_limit: nil, parser: nil, **options) ⇒ Response
73
74
75
|
# File 'lib/peddler/api.rb', line 73
def get(path, rate_limit: nil, parser: nil, **options)
request(:get, path, rate_limit:, parser:, **options)
end
|
#http(rate_limit: nil) ⇒ HTTP::Session
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/peddler/api.rb', line 57
def http(rate_limit: nil)
client = @http.(
"Host" => endpoint_uri.host,
"User-Agent" => user_agent,
"X-Amz-Access-Token" => access_token,
"X-Amz-Date" => timestamp,
)
return client if retries.zero? || rate_limit.nil?
delay = sandbox? ? 0.2 : 1.0 / rate_limit
client.retriable(delay:, tries: retries + 1, retry_statuses: [429])
end
|
#must_sandbox! ⇒ void
This method returns an undefined value.
106
107
108
|
# File 'lib/peddler/api.rb', line 106
def must_sandbox!
raise MustSandbox, "must run in a sandbox" unless sandbox?
end
|
#patch(path, rate_limit: nil, parser: nil, **options) ⇒ Response
89
90
91
|
# File 'lib/peddler/api.rb', line 89
def patch(path, rate_limit: nil, parser: nil, **options)
request(:patch, path, rate_limit:, parser:, **options)
end
|
#percent_encode(component) ⇒ String
118
119
120
|
# File 'lib/peddler/api.rb', line 118
def percent_encode(component)
URI.encode_uri_component(component)
end
|
#post(path, rate_limit: nil, parser: nil, **options) ⇒ Response
77
78
79
|
# File 'lib/peddler/api.rb', line 77
def post(path, rate_limit: nil, parser: nil, **options)
request(:post, path, rate_limit:, parser:, **options)
end
|
#put(path, rate_limit: nil, parser: nil, **options) ⇒ Response
81
82
83
|
# File 'lib/peddler/api.rb', line 81
def put(path, rate_limit: nil, parser: nil, **options)
request(:put, path, rate_limit:, parser:, **options)
end
|
#request(method, path, rate_limit: nil, parser: nil, **options) ⇒ Response
93
94
95
96
97
98
99
100
|
# File 'lib/peddler/api.rb', line 93
def request(method, path, rate_limit: nil, parser: nil, **options)
options[:json] = options.delete(:body) if options[:body] && !options[:body].is_a?(String)
uri = endpoint_uri.tap { |u| u.path = path }
http_response = http(rate_limit:).send(method, uri, **options)
Response.wrap(http_response, parser:)
end
|
#sandbox ⇒ self
Switches to the SP-API sandbox to make test calls
44
45
46
47
|
# File 'lib/peddler/api.rb', line 44
def sandbox
@sandbox = true
self
end
|
#sandbox? ⇒ Boolean
50
51
52
|
# File 'lib/peddler/api.rb', line 50
def sandbox?
@sandbox
end
|
#stringify_array(val) ⇒ String?
122
123
124
|
# File 'lib/peddler/api.rb', line 122
def stringify_array(val)
val.is_a?(Array) ? val.join(",") : val
end
|
#timestamp ⇒ String
114
115
116
|
# File 'lib/peddler/api.rb', line 114
def timestamp
Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
end
|
#user_agent ⇒ String
110
111
112
|
# File 'lib/peddler/api.rb', line 110
def user_agent
"Peddler/#{Peddler::VERSION} (Language=Ruby; #{Socket.gethostname})"
end
|