Skip to content

Commit

Permalink
More intuitive #rate_limit
Browse files Browse the repository at this point in the history
  • Loading branch information
hakanensari committed Sep 12, 2024
1 parent 9a09088 commit 0e58e2c
Show file tree
Hide file tree
Showing 44 changed files with 237 additions and 247 deletions.
16 changes: 3 additions & 13 deletions bin/generate-code
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module Generator
operation.merge(
"path" => path,
"method" => method.upcase,
"retriable_delay" => extract_retriable_delay(operation),
"rate_limit" => extract_rate_limit(operation),
"parameters" => parameters,
"body_param" => parameters&.find { |p| p["in"] == "body" },
"query_params" => parameters&.select { |p| p["in"] == "query" },
Expand Down Expand Up @@ -222,25 +222,15 @@ module Generator
end.join("\n")
end

def extract_retriable_delay(operation)
def extract_rate_limit(operation)
description = operation["description"]
return unless description

# Match rate limit from tables with or without "Plan type" column
# Format 1: | Plan type | Rate (requests per second) | Burst |
# Format 2: | Rate (requests per second) | Burst |
table_match = description.match(/Burst \|\n\|(?: *---- *\|){2,3}\n(?:\|[^|]*){0,1}\| (\S+) \|[^|]*\|/)
return unless table_match

rate_limit = table_match[1].to_f
retriable_delay = 1.0 / rate_limit

# Round to nearest integer if more than 3 decimal places
if (retriable_delay * 1000).round != (retriable_delay * 1000)
retriable_delay.round.to_f
else
retriable_delay
end
table_match[1].to_f if table_match
end
end

Expand Down
4 changes: 2 additions & 2 deletions bin/templates/api.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ module Peddler
<% method_args << "body:" if operation["body_param"] %>
<% method_args << "params:" if operation["query_params"]&.any? %>
<% if operation["retriable_delay"] %>
rate_limit(<%= operation["retriable_delay"] %>).<%= http_method %>(<%= method_args.join(", ") %>)
<% if operation["rate_limit"] %>
rate_limit(<%= operation["rate_limit"] %>).<%= http_method %>(<%= method_args.join(", ") %>)
<% else %>
<%= http_method %>(<%= method_args.join(", ") %>)
<% end %>
Expand Down
6 changes: 3 additions & 3 deletions lib/peddler/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def http
#
# @param [Float] delay The delay in seconds before retrying
# @return [self]
def rate_limit(delay)
# TODO: Remove when HTTP 6.0 is released
@http = @http.retriable(delay: delay, retry_statuses: [429]) if @http.respond_to?(:retriable)
def rate_limit(rate)
# TODO: Remove guard when HTTP 6.0 is released
retriable(delay: 1.0 / rate, retry_statuses: [429]) if @http.respond_to?(:retriable)
self
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_inbound_shipment(shipment_id, sku_quantities: nil)
"skuQuantities" => sku_quantities,
}.compact

rate_limit(0.5).get(path, params:)
rate_limit(2.0).get(path, params:)
end

# Retrieves a summary of all the inbound AWD shipments associated with a merchant, with the ability to apply
Expand Down Expand Up @@ -73,7 +73,7 @@ def list_inventory(sku: nil, sort_order: nil, details: nil, next_token: nil, max
"maxResults" => max_results,
}.compact

rate_limit(0.5).get(path, params:)
rate_limit(2.0).get(path, params:)
end
end
end
Expand Down
20 changes: 10 additions & 10 deletions lib/peddler/api/aplus_content_2020_11_01.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def search_content_documents(marketplace_id, page_token: nil)
"pageToken" => page_token,
}.compact

rate_limit(0.1).get(path, params:)
rate_limit(10.0).get(path, params:)
end

# Creates a new A+ Content document.
Expand All @@ -44,7 +44,7 @@ def create_content_document(marketplace_id, post_content_document_request)
"marketplaceId" => marketplace_id,
}.compact

rate_limit(0.1).post(path, body:, params:)
rate_limit(10.0).post(path, body:, params:)
end

# Returns an A+ Content document, if available.
Expand All @@ -62,7 +62,7 @@ def get_content_document(content_reference_key, marketplace_id, included_data_se
"includedDataSet" => included_data_set,
}.compact

rate_limit(0.1).get(path, params:)
rate_limit(10.0).get(path, params:)
end

# Updates an existing A+ Content document.
Expand All @@ -80,7 +80,7 @@ def update_content_document(content_reference_key, marketplace_id, post_content_
"marketplaceId" => marketplace_id,
}.compact

rate_limit(0.1).post(path, body:, params:)
rate_limit(10.0).post(path, body:, params:)
end

# Returns a list of ASINs related to the specified A+ Content document, if available. If you do not include the
Expand Down Expand Up @@ -109,7 +109,7 @@ def list_content_document_asin_relations(content_reference_key, marketplace_id,
"pageToken" => page_token,
}.compact

rate_limit(0.1).get(path, params:)
rate_limit(10.0).get(path, params:)
end

# Replaces all ASINs related to the specified A+ Content document, if available. This may add or remove ASINs,
Expand All @@ -130,7 +130,7 @@ def post_content_document_asin_relations(content_reference_key, marketplace_id,
"marketplaceId" => marketplace_id,
}.compact

rate_limit(0.1).post(path, body:, params:)
rate_limit(10.0).post(path, body:, params:)
end

# Checks if the A+ Content document is valid for use on a set of ASINs.
Expand All @@ -147,7 +147,7 @@ def validate_content_document_asin_relations(marketplace_id, post_content_docume
"asinSet" => asin_set,
}.compact

rate_limit(0.1).post(path, body:, params:)
rate_limit(10.0).post(path, body:, params:)
end

# Searches for A+ Content publishing records, if available.
Expand All @@ -168,7 +168,7 @@ def search_content_publish_records(marketplace_id, asin, page_token: nil)
"pageToken" => page_token,
}.compact

rate_limit(0.1).get(path, params:)
rate_limit(10.0).get(path, params:)
end

# Submits an A+ Content document for review, approval, and publishing.
Expand All @@ -184,7 +184,7 @@ def post_content_document_approval_submission(content_reference_key, marketplace
"marketplaceId" => marketplace_id,
}.compact

rate_limit(0.1).post(path, params:)
rate_limit(10.0).post(path, params:)
end

# Submits a request to suspend visible A+ Content. This neither deletes the content document nor the ASIN
Expand All @@ -201,7 +201,7 @@ def post_content_document_suspend_submission(content_reference_key, marketplace_
"marketplaceId" => marketplace_id,
}.compact

rate_limit(0.1).post(path, params:)
rate_limit(10.0).post(path, params:)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/peddler/api/application_management_2023_11_30.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ApplicationManagement20231130 < API
def rotate_application_client_secret
path = "/applications/2023-11-30/clientSecret"

rate_limit(60.0).post(path)
rate_limit(0.0167).post(path)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/peddler/api/catalog_items_2020_12_01.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def search_catalog_items(keywords, marketplace_ids, included_data: nil, brand_na
"locale" => locale,
}.compact

rate_limit(0.5).get(path, params:)
rate_limit(2.0).get(path, params:)
end

# Retrieves details for an item in the Amazon catalog.
Expand All @@ -63,7 +63,7 @@ def get_catalog_item(asin, marketplace_ids, included_data: nil, locale: nil)
"locale" => locale,
}.compact

rate_limit(0.5).get(path, params:)
rate_limit(2.0).get(path, params:)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/peddler/api/catalog_items_2022_04_01.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def search_catalog_items(
"keywordsLocale" => keywords_locale,
}.compact

rate_limit(0.5).get(path, params:)
rate_limit(2.0).get(path, params:)
end

# Retrieves details for an item in the Amazon catalog.
Expand All @@ -76,7 +76,7 @@ def get_catalog_item(asin, marketplace_ids, included_data: nil, locale: nil)
"locale" => locale,
}.compact

rate_limit(0.5).get(path, params:)
rate_limit(2.0).get(path, params:)
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/peddler/api/data_kiosk_2023_11_15.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_queries(processing_statuses: nil, page_size: nil, created_since: nil, cr
"paginationToken" => pagination_token,
}.compact

rate_limit(45.0).get(path, params:)
rate_limit(0.0222).get(path, params:)
end

# Creates a Data Kiosk query request.
Expand All @@ -50,7 +50,7 @@ def create_query(body)
path = "/dataKiosk/2023-11-15/queries"
body = body

rate_limit(60.0).post(path, body:)
rate_limit(0.0167).post(path, body:)
end

# Cancels the query specified by the `queryId` parameter. Only queries with a non-terminal `processingStatus`
Expand All @@ -64,7 +64,7 @@ def create_query(body)
def cancel_query(query_id)
path = "/dataKiosk/2023-11-15/queries/#{query_id}"

rate_limit(45.0).delete(path)
rate_limit(0.0222).delete(path)
end

# Returns query details for the query specified by the `queryId` parameter. See the `createQuery` operation for
Expand All @@ -75,7 +75,7 @@ def cancel_query(query_id)
def get_query(query_id)
path = "/dataKiosk/2023-11-15/queries/#{query_id}"

rate_limit(0.5).get(path)
rate_limit(2.0).get(path)
end

# Returns the information required for retrieving a Data Kiosk document's contents. See the `createQuery`
Expand All @@ -86,7 +86,7 @@ def get_query(query_id)
def get_document(document_id)
path = "/dataKiosk/2023-11-15/documents/#{document_id}"

rate_limit(60.0).get(path)
rate_limit(0.0167).get(path)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/peddler/api/fba_inventory_v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_inventory_summaries(granularity_type, granularity_id, marketplace_ids, d
"marketplaceIds" => marketplace_ids,
}.compact

rate_limit(0.5).get(path, params:)
rate_limit(2.0).get(path, params:)
end

# Requests that Amazon create product-details in the Sandbox Inventory in the sandbox environment. This is a
Expand Down
12 changes: 6 additions & 6 deletions lib/peddler/api/feeds_2021_06_30.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_feeds(feed_types: nil, marketplace_ids: nil, page_size: nil, processing_
"nextToken" => next_token,
}.compact

rate_limit(45.0).get(path, params:)
rate_limit(0.0222).get(path, params:)
end

# Creates a feed. Upload the contents of the feed document before calling this operation.
Expand All @@ -50,7 +50,7 @@ def create_feed(body)
path = "/feeds/2021-06-30/feeds"
body = body

rate_limit(120.0).post(path, body:)
rate_limit(0.0083).post(path, body:)
end

# Cancels the feed that you specify. Only feeds with `processingStatus=IN_QUEUE` can be cancelled. Cancelled feeds
Expand All @@ -64,7 +64,7 @@ def create_feed(body)
def cancel_feed(feed_id)
path = "/feeds/2021-06-30/feeds/#{feed_id}"

rate_limit(0.5).delete(path)
rate_limit(2.0).delete(path)
end

# Returns feed details (including the `resultDocumentId`, if available) for the feed that you specify.
Expand All @@ -75,7 +75,7 @@ def cancel_feed(feed_id)
def get_feed(feed_id)
path = "/feeds/2021-06-30/feeds/#{feed_id}"

rate_limit(0.5).get(path)
rate_limit(2.0).get(path)
end

# Creates a feed document for the feed type that you specify. This operation returns a presigned URL for uploading
Expand All @@ -89,7 +89,7 @@ def create_feed_document(body)
path = "/feeds/2021-06-30/documents"
body = body

rate_limit(2.0).post(path, body:)
rate_limit(0.5).post(path, body:)
end

# Returns the information required for retrieving a feed document's contents.
Expand All @@ -99,7 +99,7 @@ def create_feed_document(body)
def get_feed_document(feed_document_id)
path = "/feeds/2021-06-30/documents/#{feed_document_id}"

rate_limit(45.0).get(path)
rate_limit(0.0222).get(path)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/peddler/api/finances_v0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def list_financial_event_groups(max_results_per_page: nil, financial_event_group
"NextToken" => next_token,
}.compact

rate_limit(2.0).get(path, params:)
rate_limit(0.5).get(path, params:)
end

# Returns all financial events for the specified financial event group. It may take up to 48 hours for orders to
Expand Down Expand Up @@ -68,7 +68,7 @@ def list_financial_events_by_group_id(event_group_id, max_results_per_page: nil,
"NextToken" => next_token,
}.compact

rate_limit(2.0).get(path, params:)
rate_limit(0.5).get(path, params:)
end

# Returns all financial events for the specified order. It may take up to 48 hours for orders to appear in your
Expand All @@ -86,7 +86,7 @@ def list_financial_events_by_order_id(order_id, max_results_per_page: nil, next_
"NextToken" => next_token,
}.compact

rate_limit(2.0).get(path, params:)
rate_limit(0.5).get(path, params:)
end

# Returns financial events for the specified data range. It may take up to 48 hours for orders to appear in your
Expand Down Expand Up @@ -114,7 +114,7 @@ def list_financial_events(max_results_per_page: nil, posted_after: nil, posted_b
"NextToken" => next_token,
}.compact

rate_limit(2.0).get(path, params:)
rate_limit(0.5).get(path, params:)
end
end
end
Expand Down
Loading

0 comments on commit 0e58e2c

Please sign in to comment.