peddler 3.0.0.beta1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +108 -110
  3. data/lib/peddler/api.rb +56 -18
  4. data/lib/peddler/{api → apis}/amazon_warehousing_and_distribution_2024_05_09.rb +38 -26
  5. data/lib/peddler/{api → apis}/aplus_content_2020_11_01.rb +97 -59
  6. data/lib/peddler/apis/application_integrations_2024_04_01.rb +56 -0
  7. data/lib/peddler/{api → apis}/application_management_2023_11_30.rb +14 -4
  8. data/lib/peddler/{api → apis}/catalog_items_2020_12_01.rb +34 -23
  9. data/lib/peddler/{api → apis}/catalog_items_2022_04_01.rb +39 -29
  10. data/lib/peddler/{api → apis}/catalog_items_v0.rb +43 -30
  11. data/lib/peddler/{api → apis}/data_kiosk_2023_11_15.rb +47 -31
  12. data/lib/peddler/apis/easy_ship_2022_03_23.rb +126 -0
  13. data/lib/peddler/{api → apis}/fba_inbound_eligibility_v1.rb +15 -7
  14. data/lib/peddler/{api → apis}/fba_inventory_v1.rb +45 -29
  15. data/lib/peddler/{api → apis}/feeds_2021_06_30.rb +54 -33
  16. data/lib/peddler/apis/finances_2024_06_19.rb +50 -0
  17. data/lib/peddler/{api → apis}/finances_v0.rb +50 -36
  18. data/lib/peddler/{api → apis}/fulfillment_inbound_2024_03_20.rb +384 -255
  19. data/lib/peddler/{api → apis}/fulfillment_inbound_v0.rb +138 -93
  20. data/lib/peddler/{api → apis}/fulfillment_outbound_2020_07_01.rb +105 -68
  21. data/lib/peddler/apis/invoices_2024_06_19.rb +180 -0
  22. data/lib/peddler/apis/listings_items_2020_09_01.rb +95 -0
  23. data/lib/peddler/apis/listings_items_2021_08_01.rb +136 -0
  24. data/lib/peddler/{api → apis}/listings_restrictions_2021_08_01.rb +21 -11
  25. data/lib/peddler/apis/merchant_fulfillment_v0.rb +80 -0
  26. data/lib/peddler/{api → apis}/messaging_v1.rb +107 -75
  27. data/lib/peddler/{api → apis}/notifications_v1.rb +63 -32
  28. data/lib/peddler/{api → apis}/orders_v0.rb +99 -84
  29. data/lib/peddler/apis/product_fees_v0.rb +77 -0
  30. data/lib/peddler/{api → apis}/product_pricing_2022_05_01.rb +24 -13
  31. data/lib/peddler/{api → apis}/product_pricing_v0.rb +70 -52
  32. data/lib/peddler/{api → apis}/product_type_definitions_2020_09_01.rb +32 -21
  33. data/lib/peddler/{api → apis}/replenishment_2022_11_07.rb +25 -13
  34. data/lib/peddler/{api → apis}/reports_2021_06_30.rb +71 -47
  35. data/lib/peddler/{api → apis}/sales_v1.rb +22 -14
  36. data/lib/peddler/{api → apis}/sellers_v1.rb +19 -7
  37. data/lib/peddler/{api → apis}/services_v1.rb +156 -116
  38. data/lib/peddler/apis/shipment_invoicing_v0.rb +59 -0
  39. data/lib/peddler/apis/shipping_v1.rb +132 -0
  40. data/lib/peddler/{api → apis}/shipping_v2.rb +150 -98
  41. data/lib/peddler/{api → apis}/solicitations_v1.rb +21 -11
  42. data/lib/peddler/apis/supply_sources_2020_07_01.rb +100 -0
  43. data/lib/peddler/{api → apis}/tokens_2021_03_01.rb +15 -7
  44. data/lib/peddler/{api → apis}/uploads_2020_11_01.rb +18 -8
  45. data/lib/peddler/{api → apis}/vendor_direct_fulfillment_inventory_v1.rb +14 -6
  46. data/lib/peddler/{api → apis}/vendor_direct_fulfillment_orders_2021_12_28.rb +33 -21
  47. data/lib/peddler/{api → apis}/vendor_direct_fulfillment_orders_v1.rb +33 -21
  48. data/lib/peddler/{api → apis}/vendor_direct_fulfillment_payments_v1.rb +13 -5
  49. data/lib/peddler/apis/vendor_direct_fulfillment_sandbox_test_data_2021_10_28.rb +45 -0
  50. data/lib/peddler/{api → apis}/vendor_direct_fulfillment_shipping_2021_12_28.rb +86 -60
  51. data/lib/peddler/apis/vendor_direct_fulfillment_shipping_v1.rb +189 -0
  52. data/lib/peddler/{api → apis}/vendor_direct_fulfillment_transactions_2021_12_28.rb +14 -6
  53. data/lib/peddler/{api → apis}/vendor_direct_fulfillment_transactions_v1.rb +14 -6
  54. data/lib/peddler/apis/vendor_invoices_v1.rb +30 -0
  55. data/lib/peddler/{api → apis}/vendor_orders_v1.rb +59 -48
  56. data/lib/peddler/apis/vendor_shipments_v1.rb +132 -0
  57. data/lib/peddler/{api → apis}/vendor_transaction_status_v1.rb +14 -5
  58. data/lib/peddler/endpoint.rb +51 -0
  59. data/lib/peddler/error.rb +21 -11
  60. data/lib/peddler/helpers/feeds_2021_06_30.rb +26 -0
  61. data/lib/peddler/json_feed_document.rb +31 -0
  62. data/lib/peddler/marketplace.rb +93 -0
  63. data/lib/peddler/response.rb +46 -0
  64. data/lib/peddler/token.rb +70 -0
  65. data/lib/peddler/version.rb +1 -1
  66. data/lib/peddler.rb +55 -52
  67. metadata +64 -57
  68. data/lib/peddler/access_token.rb +0 -76
  69. data/lib/peddler/api/easy_ship_2022_03_23.rb +0 -108
  70. data/lib/peddler/api/listings_items_2020_09_01.rb +0 -84
  71. data/lib/peddler/api/listings_items_2021_08_01.rb +0 -154
  72. data/lib/peddler/api/merchant_fulfillment_v0.rb +0 -64
  73. data/lib/peddler/api/product_fees_v0.rb +0 -69
  74. data/lib/peddler/api/shipment_invoicing_v0.rb +0 -46
  75. data/lib/peddler/api/shipping_v1.rb +0 -107
  76. data/lib/peddler/api/supply_sources_2020_07_01.rb +0 -82
  77. data/lib/peddler/api/vendor_direct_fulfillment_sandbox_test_data_2021_10_28.rb +0 -35
  78. data/lib/peddler/api/vendor_direct_fulfillment_shipping_v1.rb +0 -165
  79. data/lib/peddler/api/vendor_invoices_v1.rb +0 -22
  80. data/lib/peddler/api/vendor_shipments_v1.rb +0 -120
  81. data/lib/peddler/region.rb +0 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f132e3d74dc1f70f5f0d84f84cc75e6d4e9184bd2ed4162e2e805a766341afed
4
- data.tar.gz: 65c57dc08f0d1e1b23f87969ec2657d7592ea4590c65d3c18b7e9c9ac612ed71
3
+ metadata.gz: 989f5bec7dce426405b687e65c4677fe8b359b355d815df799a048ef1907809f
4
+ data.tar.gz: eec2f1236bf285bf00c7d32e1482ae2fadaadeb2db9f0bc0a9beb64cf3ec7072
5
5
  SHA512:
6
- metadata.gz: cc5e82f24bd72cebd6d226e713d22e792944e043489da3a7f79245fd5242f1430cef84201643e9ebd5a25d9b5a10c9bbf5ae88e70f27a4d52a7b077482f9760a
7
- data.tar.gz: 80a16515b74499a28513058b41eab6a912bcb21249d7748cedd74db9efd7e3ccc2316e896f78b9406c2e1f7d078b667ad73a62a980054cd0982135a9b219fd0f
6
+ metadata.gz: 361ce5ea21116bbc09efb9590d080db5275f35dbc6717ccd318c291eb32e4b122b24d06ad4f78c9c693298d8e6e70fc81f3d7a128b843b7949579a601683bf91
7
+ data.tar.gz: 956ad0c6b2b7d1a490dffbf846a4c4415e1323510ac52c0eebdb2b42835f5a2f14354f2e0d16fa19099d0823ed47f09218255e98973735e7ccfa1579f08ccefa
data/README.md CHANGED
@@ -4,9 +4,16 @@
4
4
  [![Maintainability](https://api.codeclimate.com/v1/badges/281e6176048f3c0a1ed3/maintainability)][maintainability]
5
5
  [![Test Coverage](https://api.codeclimate.com/v1/badges/281e6176048f3c0a1ed3/test_coverage)][test-coverage]
6
6
 
7
+ > **_NOTE:_** This README explains the main branch, which may contain unreleased code. For the README of the latest release, please refer to [this link][latest-release-readme].
8
+
7
9
  **Peddler** is a Ruby interface to the [Amazon Selling Partner API (SP-API)][docs-overview]. The SP-API enables Amazon sellers and vendors to programmatically access their data on orders, shipments, payments, and more.
8
10
 
9
- To begin using the Amazon SP-API, you must [register as a developer][register-as-developer] and [register your application][register-application]. Once registered, [obtain your Login with Amazon (LWA) credentials on Amazon][view-credentials]. You'll use these to access data for your own seller account or retrieve data on behalf of others.
11
+ Peddler is automatically generated from the Open API models provided by Amazon.
12
+
13
+ To begin using the Amazon SP-API, you must [register as a developer][register-as-developer] and [register your application][register-application]. Once registered, [obtain your Login with Amazon (LWA) credentials][view-credentials] to access your own or other selling partners' data.
14
+
15
+ - [API models][api-models]
16
+ - [API samples][api-samples]
10
17
 
11
18
  ![Peddler](https://github.com/hakanensari/peddler/blob/main/images/peddler.jpg?raw=true)
12
19
 
@@ -15,7 +22,7 @@ To begin using the Amazon SP-API, you must [register as a developer][register-as
15
22
  Add this line to your Gemfile.
16
23
 
17
24
  ```ruby
18
- gem "peddler", "3.0.0.beta1"
25
+ gem "peddler", "~> 3.0"
19
26
  ```
20
27
 
21
28
  And then execute:
@@ -39,53 +46,65 @@ Require the library.
39
46
  require "peddler"
40
47
  ```
41
48
 
42
- ### Authentication
49
+ ### Authorization
43
50
 
44
- A seller or vendor will [provide a refresh token][authorization] to access their data on Amazon. You'll use this to generate temporary access tokens to authenticate individual API requests. Here’s how you can request one in Peddler.
51
+ A seller or vendor [provides you a refresh token][authorization] to access their data on Amazon.
45
52
 
46
53
  ```ruby
47
- access_token = Peddler::AccessToken.request(
54
+ refresh_token = Peddled::Token.request(
55
+ code: "<AUTHORIZATION_CODE>"
56
+ ).parse["refresh_token"]
57
+ ```
58
+
59
+ You use this to generate a temporary access token to authenticate individual API requests.
60
+
61
+ ```ruby
62
+ access_token = Peddler::Token.request(
48
63
  refresh_token: "<REFRESH_TOKEN>",
49
- )
64
+ ).parse["access_token"]
50
65
  ```
51
66
 
52
- You can also request a token for grantless operations.
67
+ Similarly, you can request a token for grantless operations.
53
68
 
54
69
  ```ruby
55
- access_token = Peddler::AccessToken.request(
70
+ access_token = Peddler::Token.request(
56
71
  scope: "sellingpartnerapi::notifications",
57
- )
72
+ ).parse["access_token"]
58
73
  ```
59
74
 
60
- Access tokens are valid for one hour. To optimize performance, cache the token and reuse it across calls instead of generating a new one each time.
61
-
62
- If you haven’t set your LWA credentials as environment variables, you can pass them directly when requesting an access token:
75
+ If you haven’t set your LWA credentials as environment variables, pass them directly when requesting the token.
63
76
 
64
77
  ```ruby
65
- access_token = Peddler::AccessToken.request(
78
+ access_token = Peddler::Token.request(
66
79
  client_id: "<YOUR_CLIENT_ID>",
67
80
  client_secret: "<YOUR_CLIENT_SECRET>",
68
81
  refresh_token: "<REFRESH_TOKEN>",
69
- )
82
+ ).parse["access_token"]
70
83
  ```
71
84
 
72
- ### Throttling
85
+ Access tokens are valid for one hour. To optimize performance, cache the token and reuse across calls.
73
86
 
74
- Amazon’s Selling Partner API (SP-API) imposes standard rate limits on most operations. Peddler respects these limits and automatically backs off when throttled to ensure compliance with Amazon’s policies.
75
-
76
- **Note:** This functionality requires version 6 of the underlying [HTTP library][httprb]. As of writing, this is not released yet. To use rate limiting, point to the main branch of their GitHub repo:
87
+ In Rails, if you're storing a refresh token in a model representing a selling partner, implement a method there to retrieve and cache access tokens:
77
88
 
78
89
  ```ruby
79
- gem "http", github: "httprb/http"
90
+ class Seller
91
+ "..."
92
+
93
+ def access_token
94
+ Rails.cache.fetch("#{cache_key}/access_key", expires_in: 1.hour) do
95
+ Peddler::Token.request(
96
+ refresh_token:,
97
+ ).parse["access_token"]
98
+ end
99
+ end
100
+ end
80
101
  ```
81
102
 
82
- If you have custom rate limits (e.g., Amazon has granted you higher quotas), you can override the standard rate limit using the rate_limit method.
103
+ ### Rate limiting
83
104
 
84
- ```ruby
85
- @api.rate_limit(5).get_item_offers_batch(...)
86
- ```
105
+ Amazon’s Selling Partner API (SP-API) imposes [rate limits][rate-limits] on most operations. Peddler respects these limits and automatically backs off when throttled. To override the default rate limit, pass a `:rate_limit` argument when running an operation.
87
106
 
88
- This sets a custom rate limit of 5 requests per second for the requested operation.
107
+ **Note:** This functionality requires version 6 of the underlying [HTTP library][httprb]. As of writing, this is not released yet. To use rate limiting, point to their main branch on GitHub.
89
108
 
90
109
  ### The APIs
91
110
 
@@ -95,42 +114,37 @@ Peddler provides a class for each API version under an eponymous namespace. Belo
95
114
 
96
115
  Provides programmatic access to Amazon's catalog data, such as item titles, descriptions, and other product details.
97
116
 
98
- **Example:**
99
-
100
117
  ```ruby
101
- client = Peddler::API::CatalogItems20220401.new("<AWS_REGION>", "<ACCESS_TOKEN>")
102
- response = client.get_catalog_item(
118
+ api = Peddler.catalog_items_2022_04_01(aws_region, access_token)
119
+ response = api.get_catalog_item(
103
120
  marketplaceIds: ["ATVPDKIKX0DER"],
104
121
  asin: "B08N5WRWNW"
105
122
  )
106
- items = response.parse.dig("items")
123
+ items = response.dig("items")
107
124
  ```
108
125
 
109
126
  #### Orders API (v0)
110
127
 
111
128
  Allows you to retrieve order information, including order details, buyer information, and order items.
112
129
 
113
- **Example:**
114
-
115
130
  ```ruby
116
- client = Peddler::API::OrdersV0.new("<AWS_REGION>", "<ACCESS_TOKEN>")
117
- response = client.get_orders(
131
+ api = Peddler.orders_v0(aws_region, access_token)
132
+ response = api.get_orders(
118
133
  marketplaceIds: ["ATVPDKIKX0DER"],
119
134
  createdAfter: "2023-01-01T00:00:00Z"
120
135
  )
121
- orders = response.parse.dig("orders")
136
+ response.dig("orders")
122
137
  ```
123
138
 
124
139
  #### Feeds API (2021-06-30)
125
140
 
126
141
  Enables you to upload data to Amazon for updating listings, prices, inventory, and more.
127
142
 
128
- **Example:**
129
-
130
143
  ```ruby
131
- client = Peddler::API::Feeds20210630.new("<AWS_REGION>", "<ACCESS_TOKEN>")
144
+ api = Peddler.feeds_2021_06_30(aws_region, access_token)
145
+
132
146
  # Create a feed document to get an upload URL
133
- response = client.create_feed_document(
147
+ response = api.create_feed_document(
134
148
  contentType: "text/xml; charset=UTF-8"
135
149
  )
136
150
  feed_document_id = response.parse["feedDocumentId"]
@@ -138,41 +152,37 @@ upload_url = response.parse["url"]
138
152
 
139
153
  # Upload the feed content to the provided URL
140
154
  feed_content = File.read("inventory_update.xml")
141
- client.upload_feed_document(upload_url, feed_content)
155
+ api.upload_feed_document(upload_url, feed_content, "text/xml; charset=UTF-8")
142
156
 
143
157
  # Create the feed
144
- response = client.create_feed(
158
+ response = api.create_feed(
145
159
  feedType: "POST_INVENTORY_AVAILABILITY_DATA",
146
160
  marketplaceIds: ["ATVPDKIKX0DER"],
147
161
  inputFeedDocumentId: feed_document_id
148
162
  )
149
- feed_id = response.parse["feedId"]
163
+ response.parse["feedId"]
150
164
  ```
151
165
 
152
166
  #### Reports API (2021-06-30)
153
167
 
154
168
  Allows you to request and download various reports, such as order and inventory reports.
155
169
 
156
- **Example:**
157
-
158
170
  ```ruby
159
- client = Peddler::API::Reports20210630.new("<AWS_REGION>", "<ACCESS_TOKEN>")
160
- response = client.create_report(
171
+ api = Peddler.reports_2021_06_30(aws_region, access_token)
172
+ response = api.create_report(
161
173
  reportType: "GET_FLAT_FILE_OPEN_LISTINGS_DATA",
162
174
  marketplaceIds: ["ATVPDKIKX0DER"]
163
175
  )
164
- report_id = response.parse["reportId"]
176
+ response.parse["reportId"]
165
177
  ```
166
178
 
167
179
  #### Listings Items API (2021-08-01)
168
180
 
169
181
  Enables you to manage your product listings on Amazon, including creating, updating, and deleting listings.
170
182
 
171
- **Example:**
172
-
173
183
  ```ruby
174
- client = Peddler::API::ListingsItems20210801.new("<AWS_REGION>", "<ACCESS_TOKEN>")
175
- response = client.put_listings_item(
184
+ api = Peddler.listings_items_2021_08_01(aws_region, access_token)
185
+ response = api.put_listings_item(
176
186
  "<SELLER_ID>",
177
187
  "SKU123",
178
188
  "ATVPDKIKX0DER",
@@ -187,19 +197,17 @@ response = client.put_listings_item(
187
197
  }
188
198
  }
189
199
  )
190
- result = response.parse
200
+ response.parse
191
201
  ```
192
202
 
193
203
  #### Notifications API (v1)
194
204
 
195
205
  Allows you to subscribe to notifications for specific events like order status updates or feed processing statuses.
196
206
 
197
- **Example:**
198
-
199
207
  ```ruby
200
- client = Peddler::API::NotificationsV1.new("<AWS_REGION>", "<ACCESS_TOKEN>")
208
+ api = Peddler.notifications_v1(aws_region, access_token)
201
209
  # Create a destination
202
- response = client.create_destination(
210
+ response = api.create_destination(
203
211
  name: "MySQSQueue",
204
212
  resourceSpecification: {
205
213
  sqs: {
@@ -210,7 +218,7 @@ response = client.create_destination(
210
218
  destination_id = response.parse["destinationId"]
211
219
 
212
220
  # Create a subscription
213
- response = client.create_subscription(
221
+ response = api.create_subscription(
214
222
  notificationType: "ANY_OFFER_CHANGED",
215
223
  destinationId: destination_id
216
224
  )
@@ -221,11 +229,9 @@ subscription = response.parse
221
229
 
222
230
  Provides information about fees that may be charged for selling products on Amazon.
223
231
 
224
- **Example:**
225
-
226
232
  ```ruby
227
- client = Peddler::API::ProductFeesV0.new("<AWS_REGION>", "<ACCESS_TOKEN>")
228
- response = client.get_my_fees_estimate_for_sku(
233
+ api = Peddler.product_fees_v0(aws_region, access_token)
234
+ response = api.get_my_fees_estimate_for_sku(
229
235
  sellerId: "<YOUR_SELLER_ID>",
230
236
  sku: "SKU123",
231
237
  body: {
@@ -248,11 +254,9 @@ fees_estimate = response.parse
248
254
 
249
255
  Allows you to create and manage fulfillment orders using Amazon's fulfillment network.
250
256
 
251
- **Example:**
252
-
253
257
  ```ruby
254
- client = Peddler::API::FulfillmentOutbound20200701.new("<AWS_REGION>", "<ACCESS_TOKEN>")
255
- response = client.create_fulfillment_order(
258
+ api = Peddler.fulfillment_outbound_2020_07_01(aws_region, access_token)
259
+ response = api.create_fulfillment_order(
256
260
  body: {
257
261
  sellerFulfillmentOrderId: "ORDER123",
258
262
  displayableOrderId: "ORDER123",
@@ -276,18 +280,16 @@ response = client.create_fulfillment_order(
276
280
  ]
277
281
  }
278
282
  )
279
- result = response.parse
283
+ response.parse
280
284
  ```
281
285
 
282
286
  #### Merchant Fulfillment API (v0)
283
287
 
284
288
  Allows you to create shipping labels for orders using Amazon's negotiated shipping rates.
285
289
 
286
- **Example:**
287
-
288
290
  ```ruby
289
- client = Peddler::API::MerchantFulfillmentV0.new("<AWS_REGION>", "<ACCESS_TOKEN>")
290
- response = client.get_eligible_shipping_services(
291
+ api = Peddler.merchant_fulfillment_v0(aws_region, access_token)
292
+ response = api.get_eligible_shipping_services(
291
293
  body: {
292
294
  shipmentRequestDetails: {
293
295
  amazonOrderId: "ORDER123",
@@ -329,11 +331,9 @@ shipping_services = response.parse["shippingServiceList"]
329
331
 
330
332
  Allows vendors to retrieve purchase orders and order details from Amazon.
331
333
 
332
- **Example:**
333
-
334
334
  ```ruby
335
- client = Peddler::API::VendorOrders20211228.new("<AWS_REGION>", "<ACCESS_TOKEN>")
336
- response = client.get_purchase_orders(
335
+ api = Peddler.vendor_orders_2021_12_28(aws_region, access_token)
336
+ response = api.get_purchase_orders(
337
337
  shipToPartyId: "<PARTY_ID>",
338
338
  limit: 10,
339
339
  createdAfter: "2023-01-01T00:00:00Z"
@@ -345,40 +345,34 @@ purchase_orders = response.parse["purchaseOrders"]
345
345
 
346
346
  Enables vendors to manage shipping labels and shipping information for direct fulfillment orders.
347
347
 
348
- **Example:**
349
-
350
348
  ```ruby
351
- client = Peddler::API::VendorDirectFulfillmentShipping20211228.new("<AWS_REGION>", "<ACCESS_TOKEN>")
352
- response = client.get_packing_slip(
349
+ api = Peddler.vendor_direct_fulfillment_shipping_2021_12_28(aws_region, access_token)
350
+ response = api.get_packing_slip(
353
351
  purchaseOrderNumber: "PO123456789"
354
352
  )
355
- packing_slip = response.parse
353
+ response.parse
356
354
  ```
357
355
 
358
356
  #### Vendor Direct Fulfillment Orders API (2021-12-28)
359
357
 
360
358
  Allows vendors to receive orders for direct fulfillment and provide shipment confirmations.
361
359
 
362
- **Example:**
363
-
364
360
  ```ruby
365
- client = Peddler::API::VendorDirectFulfillmentOrders20211228.new("<AWS_REGION>", "<ACCESS_TOKEN>")
366
- response = client.get_orders(
361
+ api = Peddler.vendor_direct_fulfillment_orders_2021_12_28(aws_region, access_token)
362
+ response = api.get_orders(
367
363
  createdAfter: "2023-01-01T00:00:00Z",
368
364
  limit: 10
369
365
  )
370
- orders = response.parse["orders"]
366
+ response.parse["orders"]
371
367
  ```
372
368
 
373
369
  #### Vendor Direct Fulfillment Inventory API (2021-12-28)
374
370
 
375
371
  Enables vendors to update inventory levels for direct fulfillment items.
376
372
 
377
- **Example:**
378
-
379
373
  ```ruby
380
- client = Peddler::API::VendorDirectFulfillmentInventory20211228.new("<AWS_REGION>", "<ACCESS_TOKEN>")
381
- response = client.submit_inventory_update(
374
+ api = Peddler.vendor_direct_fulfillment_inventory_2021_12_28(aws_region, access_token)
375
+ response = api.submit_inventory_update(
382
376
  body: {
383
377
  inventory: [
384
378
  {
@@ -399,18 +393,16 @@ response = client.submit_inventory_update(
399
393
  ]
400
394
  }
401
395
  )
402
- result = response.parse
396
+ response.parse
403
397
  ```
404
398
 
405
399
  #### Shipping API (v2)
406
400
 
407
401
  Provides functionalities for purchasing shipping labels and tracking shipments.
408
402
 
409
- **Example:**
410
-
411
403
  ```ruby
412
- client = Peddler::API::ShippingV2.new("<AWS_REGION>", "<ACCESS_TOKEN>")
413
- response = client.purchase_shipment(
404
+ api = Peddler.shipping_v2(aws_region, access_token)
405
+ response = api.purchase_shipment(
414
406
  body: {
415
407
  clientReferenceId: "CLIENT_REF_123",
416
408
  shipTo: {
@@ -446,18 +438,16 @@ response = client.purchase_shipment(
446
438
  serviceType: "Standard"
447
439
  }
448
440
  )
449
- shipment = response.parse
441
+ response.parse
450
442
  ```
451
443
 
452
444
  #### Token API (2021-03-01)
453
445
 
454
446
  Allows you to create restricted data tokens to access personally identifiable information (PII) in specific API calls.
455
447
 
456
- **Example:**
457
-
458
448
  ```ruby
459
- client = Peddler::API::Tokens20210301.new("<AWS_REGION>", "<ACCESS_TOKEN>")
460
- response = client.create_restricted_data_token(
449
+ api = Peddler.tokens_2021_03_01(aws_region, access_token)
450
+ response = api.create_restricted_data_token(
461
451
  body: {
462
452
  restrictedResources: [
463
453
  {
@@ -471,46 +461,54 @@ response = client.create_restricted_data_token(
471
461
  restricted_data_token = response.parse["restrictedDataToken"]
472
462
 
473
463
  # Use the token in subsequent API calls
474
- orders_client = Peddler::API::Orders20211201.new("<AWS_REGION>", restricted_data_token)
475
- response = orders_client.get_order(
464
+ orders_api = Peddler.orders_2021_12_01("<AWS_REGION>", restricted_data_token)
465
+ response = orders_api.get_order(
476
466
  orderId: "123-1234567-1234567"
477
467
  )
478
- order_details = response.parse
468
+ response.parse
479
469
  ```
480
470
 
481
471
  #### Finances API (v0)
482
472
 
483
473
  Provides information about financial events for your seller account, such as order payments, refunds, and fees.
484
474
 
485
- **Example:**
486
-
487
475
  ```ruby
488
- client = Peddler::API::FinancesV0.new
489
- response = client.list_financial_events(
476
+ api = Peddler.finances_v0(aws_region, access_token)
477
+ response = api.list_financial_events(
490
478
  postedAfter: "2023-01-01T00:00:00Z",
491
479
  maxResultsPerPage: 100
492
480
  )
493
- financial_events = response.parse["FinancialEvents"]
481
+ response.parse
494
482
  ```
495
483
 
496
484
  #### Sellers API (V1)
497
485
 
498
486
  Provides information about seller's marketplaces and participation status.
499
487
 
500
- **Example:**
501
-
502
488
  ```ruby
503
- client = Peddler::API::SellersV1.new
504
- response = client.get_marketplace_participations
505
- participations = response.parse["payload"]
489
+ api = Peddler.sellers_v1(aws_region, access_token)
490
+ response = api.get_marketplace_participations
491
+ response.parse
506
492
  ```
507
493
 
494
+ ## TODO
495
+
496
+ - Code generate models to parse payload. Consider using `dry-struct`.
497
+ - Code generate the APIs section—descriptions and code examples—in this README here.
498
+ - Schedule code generation with GitHub Actions. Push new gem when models change.
499
+ - Review and consider applying [these patches][patches].
500
+
508
501
  [build]: https://github.com/hakanensari/peddler/actions
509
502
  [maintainability]: https://codeclimate.com/github/hakanensari/peddler/maintainability
510
503
  [test-coverage]: https://codeclimate.com/github/hakanensari/peddler/test_coverage
504
+ [latest-release-readme]: https://github.com/lineofflight/peddler/tree/v3.0.0
511
505
  [docs-overview]: https://developer.amazonservices.com/sp-api-docs/overview
512
506
  [register-as-developer]: https://developer-docs.amazon.com/sp-api/docs/registering-as-a-developer
513
507
  [register-application]: https://developer-docs.amazon.com/sp-api/docs/registering-your-application
508
+ [api-models]: https://github.com/amzn/selling-partner-api-models
509
+ [api-samples]: https://github.com/amzn/selling-partner-api-samples
514
510
  [view-credentials]: https://developer-docs.amazon.com/sp-api/docs/viewing-your-application-information-and-credentials
515
511
  [authorization]: https://developer-docs.amazon.com/sp-api/docs/authorizing-selling-partner-api-applications
512
+ [rate-limits]: https://developer-docs.amazon.com/sp-api/docs/usage-plans-and-rate-limits
516
513
  [httprb]: https://github.com/httprb/http
514
+ [patches]: https://github.com/bizon/selling-partner-api-sdk/tree/master/codegen/patches
data/lib/peddler/api.rb CHANGED
@@ -2,14 +2,25 @@
2
2
 
3
3
  require "http"
4
4
 
5
+ require "peddler/endpoint"
5
6
  require "peddler/error"
6
- require "peddler/region"
7
+ require "peddler/marketplace"
8
+ require "peddler/response"
9
+ require "peddler/version"
7
10
 
8
11
  module Peddler
9
12
  # Wraps an Amazon Selling Partner API (SP-API)
10
13
  class API
11
- # @return [Peddler::Region]
12
- attr_reader :region
14
+ class CannotSandbox < StandardError; end
15
+ class MustSandbox < StandardError; end
16
+
17
+ class << self
18
+ # @return [#call]
19
+ attr_accessor :parser
20
+ end
21
+
22
+ # @return [Peddler::Endpoint]
23
+ attr_reader :endpoint
13
24
 
14
25
  # @return [String]
15
26
  attr_reader :access_token
@@ -17,41 +28,52 @@ module Peddler
17
28
  # @param [String] aws_region
18
29
  # @param [String] access_token
19
30
  def initialize(aws_region, access_token)
20
- @region = Region.new(aws_region)
31
+ @endpoint = Endpoint.find(aws_region)
21
32
  @access_token = access_token
33
+ @sandbox = false
22
34
  end
23
35
 
24
- def endpoint
25
- @endpoint ||= region.endpoint
36
+ # @return [URI::HTTPS]
37
+ def endpoint_uri
38
+ sandbox? ? endpoint.sandbox : endpoint.production
26
39
  end
27
40
 
41
+ # Switches to the SP-API sandbox to make test calls
42
+ #
28
43
  # @see https://developer-docs.amazon.com/sp-api/docs/the-selling-partner-api-sandbox
29
44
  # @return [self]
30
45
  def sandbox
31
- @endpoint = endpoint.sub(/(?:sandbox.)?sellingpartnerapi/, "sandbox.sellingpartnerapi")
46
+ @sandbox = true
32
47
  self
33
48
  end
34
49
 
50
+ # @return [Boolean]
51
+ def sandbox?
52
+ @sandbox
53
+ end
54
+
35
55
  # @see https://developer-docs.amazon.com/sp-api/docs/include-a-user-agent-header-in-all-requests
36
56
  # @see https://developer-docs.amazon.com/amazon-shipping/docs/connecting-to-the-selling-partner-api#step-3-add-headers-to-the-uri
37
57
  # @return [HTTP::Client]
38
58
  def http
39
59
  @http ||= HTTP.headers(
40
- "Host" => endpoint_host,
60
+ "Host" => endpoint_uri.host,
41
61
  "User-Agent" => user_agent,
42
62
  "X-Amz-Access-Token" => access_token,
43
63
  "X-Amz-Date" => timestamp,
44
64
  )
45
65
  end
46
66
 
47
- # Retries with a rate limit when the API returns a 429
67
+ # Throttles with a rate limit and retries when the API returns a 429
48
68
  #
49
- # @param [Float] delay The delay in seconds before retrying
69
+ # @param [Float] rate_limit The delay in seconds before retrying
50
70
  # @return [self]
51
- def rate_limit(rate)
52
- # HTTP v6.0 will implement retriable
71
+ def meter(rate_limit)
72
+ # HTTP v6.0 will implement retriable. Until then, point to their GitHub repo, or it's a no-op.
53
73
  # https://github.com/httprb/http/pull/790
54
- retriable(delay: 1.0 / rate, retry_statuses: [429]) if @http.respond_to?(:retriable)
74
+ delay = sandbox? ? 0.2 : 1.0 / rate_limit
75
+ retriable(delay: delay, retry_statuses: [429])
76
+
55
77
  self
56
78
  end
57
79
 
@@ -75,7 +97,7 @@ module Peddler
75
97
  # @return [self]
76
98
  [:via, :use, :retriable].each do |method|
77
99
  define_method(method) do |*args, &block|
78
- @http = http.send(method, *args, &block)
100
+ @http = http.send(method, *args, &block) if http.respond_to?(method)
79
101
  self
80
102
  end
81
103
  end
@@ -87,21 +109,37 @@ module Peddler
87
109
  options[:json] = options.delete(:body)
88
110
  end
89
111
 
90
- response = http.send(method, [endpoint, path].join, **options)
112
+ uri = endpoint_uri.tap do |uri|
113
+ uri.path = path
114
+ end
115
+
116
+ response = http.send(method, uri, **options)
91
117
 
92
118
  if response.status.client_error?
93
119
  error = Error.build(response)
94
120
  raise error if error
95
121
  end
96
122
 
97
- response
123
+ Response.decorate(response, parser:)
98
124
  end
99
125
  end
100
126
 
127
+ attr_writer :parser
128
+
129
+ # @!attribute parser
130
+ # @return [#call]
131
+ def parser
132
+ @parser || self.class.parser
133
+ end
134
+
101
135
  private
102
136
 
103
- def endpoint_host
104
- URI.parse(endpoint).host
137
+ def cannot_sandbox!
138
+ raise CannotSandbox, "cannot run in a sandbox" if sandbox?
139
+ end
140
+
141
+ def must_sandbox!
142
+ raise MustSandbox, "must run in a sandbox" unless sandbox?
105
143
  end
106
144
 
107
145
  def user_agent