Skip to content

Commit 0c2c49e

Browse files
authored
Merge pull request #55 from ericcj/restricted_operations
support for restricted operations
2 parents 18a2613 + fa386df commit 0c2c49e

File tree

5 files changed

+110
-18
lines changed

5 files changed

+110
-18
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,41 @@ require 'fulfillment-outbound-api-model'
5757
end
5858
```
5959

60+
## Restricted operations
61+
62+
Configure as per above but also create a new client for each restrictedResources you need, e.g.:
63+
64+
```
65+
require 'orders-api-model'
66+
67+
client = AmzSpApi::RestrictedSpApiClient.new({
68+
'restrictedResources' => [
69+
{
70+
'method' => 'GET',
71+
'path' => "/orders/v0/orders",
72+
'dataElements' => ['buyerInfo', 'shippingAddress']
73+
}
74+
]
75+
})
76+
api_orders = AmzSpApi::OrdersApiModel::OrdersV0Api.new(client)
77+
api_orders.get_orders(marketplace_ids, created_after: 1.day.ago.iso8601)
78+
79+
client = AmzSpApi::RestrictedSpApiClient.new({
80+
'restrictedResources' => [
81+
{
82+
'method' => 'GET',
83+
'path' => "/orders/v0/orders/#{my_order_id}",
84+
'dataElements' => ['buyerInfo', 'shippingAddress']
85+
}
86+
]
87+
})
88+
api_orders = AmzSpApi::OrdersApiModel::OrdersV0Api.new(client)
89+
api_orders.get_order(my_order_id)
90+
91+
# or you can use models AmzSpApi::RestrictedSpApiClient.new(AmzSpApi::TokensApiModel::CreateRestrictedDataTokenRequest.new(restricted_resources: [
92+
AmzSpApi::TokensApiModel::RestrictedResource.new(...
93+
```
94+
6095
## Feeds and reports
6196

6297
This gem also offers encrypt/decrypt helper methods for feeds and reports, but actually using that API as per https://developer-docs.amazon.com/sp-api/docs/ requires the following calls, e.g. for feeds but reports is the same pattern:

lib/amz_sp_api.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
require 'amz_sp_api_version'
22
require 'api_error'
3-
require 'sp_api_client'
43
require 'sp_configuration'
4+
require 'sp_api_client'
5+
require 'restricted_sp_api_client'
56

67
module AmzSpApi
78
class << self

lib/amz_sp_api_version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module AmzSpApi
2-
VERSION = '1.0.0'
2+
VERSION = '1.0.1'
33
end

lib/restricted_sp_api_client.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'api_error'
2+
require 'api_client'
3+
require 'configuration'
4+
require 'sp_api_client'
5+
6+
require 'tokens-api-model'
7+
8+
module AmzSpApi
9+
class RestrictedSpApiClient < ApiClient
10+
11+
def initialize(create_restricted_data_token_request, config = SpConfiguration.default)
12+
super(config)
13+
raise "pass create_restricted_data_token_request to RestrictedSpApiClient.new" if create_restricted_data_token_request.kind_of?(Configuration)
14+
@wrapped_client = SpApiClient.new(config)
15+
@create_restricted_data_token_request = create_restricted_data_token_request
16+
@cache_key = config.access_token_key + "-RDT-" + Digest::MD5.hexdigest(create_restricted_data_token_request.to_s)
17+
end
18+
19+
alias_method :super_call_api, :call_api
20+
def call_api(http_method, path, opts = {})
21+
unsigned_request = build_request(http_method, path, opts)
22+
aws_headers = auth_headers(http_method, unsigned_request.url, unsigned_request.encoded_body)
23+
signed_opts = opts.merge(:header_params => aws_headers.merge(opts[:header_params] || {}))
24+
super(http_method, path, signed_opts)
25+
end
26+
27+
private
28+
29+
def retrieve_rdt_access_token
30+
return request_rdt_access_token[:access_token] unless config.get_access_token
31+
stored_token = config.get_access_token.call(@cache_key)
32+
if stored_token.nil?
33+
new_token = request_rdt_access_token
34+
config.save_access_token.call(@cache_key, new_token) if config.save_access_token
35+
return new_token[:access_token]
36+
else
37+
return stored_token
38+
end
39+
end
40+
41+
def request_rdt_access_token
42+
api_tokens = AmzSpApi::TokensApiModel::TokensApi.new(@wrapped_client)
43+
response = api_tokens.create_restricted_data_token(@create_restricted_data_token_request)
44+
45+
{access_token: response.restricted_data_token,
46+
expires_in: response.expires_in}
47+
end
48+
49+
def auth_headers(http_method, url, body)
50+
SpApiClient.signed_request_headers(config, http_method, url, body).merge({
51+
'x-amz-access-token' => retrieve_rdt_access_token
52+
})
53+
end
54+
55+
end
56+
end

lib/sp_api_client.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ def call_api(http_method, path, opts = {})
1818
super(http_method, path, signed_opts)
1919
end
2020

21+
def self.signed_request_headers(config, http_method, url, body)
22+
request_config = {
23+
service: 'execute-api',
24+
region: config.aws_region
25+
}
26+
if config.credentials_provider
27+
request_config[:credentials_provider] = config.credentials_provider
28+
else
29+
request_config[:access_key_id] = config.aws_access_key_id
30+
request_config[:secret_access_key] = config.aws_secret_access_key
31+
end
32+
signer = Aws::Sigv4::Signer.new(request_config)
33+
signer.sign_request(http_method: http_method.to_s, url: url, body: body).headers
34+
end
35+
2136
private
2237

2338
def retrieve_lwa_access_token
@@ -58,23 +73,8 @@ def request_lwa_access_token
5873
data
5974
end
6075

61-
def signed_request_headers(http_method, url, body)
62-
request_config = {
63-
service: 'execute-api',
64-
region: config.aws_region
65-
}
66-
if config.credentials_provider
67-
request_config[:credentials_provider] = config.credentials_provider
68-
else
69-
request_config[:access_key_id] = config.aws_access_key_id
70-
request_config[:secret_access_key] = config.aws_secret_access_key
71-
end
72-
signer = Aws::Sigv4::Signer.new(request_config)
73-
signer.sign_request(http_method: http_method.to_s, url: url, body: body).headers
74-
end
75-
7676
def auth_headers(http_method, url, body)
77-
signed_request_headers(http_method, url, body).merge({
77+
self.class.signed_request_headers(config, http_method, url, body).merge({
7878
'x-amz-access-token' => retrieve_lwa_access_token
7979
})
8080
end

0 commit comments

Comments
 (0)