amz_sp_api 0.2.0 → 0.2.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee4098faefbf5cd215a7bc4e6c11830acb94de49
4
- data.tar.gz: 81907b713cc2d88ba565a0e88a8c98334081404d
3
+ metadata.gz: 129a4bc559aca17253362d03710e1fb6b5dcc55c
4
+ data.tar.gz: 7e0dbcb797f06e10deaebb2532add432cb58ac1f
5
5
  SHA512:
6
- metadata.gz: cc89c9f8dbb50680584948b29aa79232d9fa762b8fddba04411f94a6bc359936866c711f35ea6cec758bb06556edd2962bfc3c905378bc0d3d539909abe47622
7
- data.tar.gz: 2f9831e66d81599745ddf667c0c65f571324fe2853a777cec492d2cb1ded13b975f14913d741355e95970f16fb80d1fb6344a9b182b2f14f44e3a23abecbc770
6
+ metadata.gz: 16ea2e4a0c8843c7f4a034d2bbfd5dff049079a1391a44da6d4aa864f48c351233ebcab67ca7b219850ca4a601f4ec4a8c7b79534bc3b268634e55497523b91b
7
+ data.tar.gz: 5c4154fd81e34f66f0c1ad31960cf338762097a705b147c44a496769f555520f0ea5250955cd707f8364d78b018288e02043e445d45efa596b79ea06ba6910ac
data/README.md CHANGED
@@ -8,32 +8,7 @@ Auto-generated documentation is nested here, but the references in https://githu
8
8
 
9
9
  ## Installation
10
10
 
11
- ### Build a gem
12
-
13
- To build the Ruby code into a gem:
14
-
15
- ```shell
16
- gem build amz_sp_api.gemspec
17
- ```
18
-
19
- Then either install the gem locally:
20
-
21
- ```shell
22
- gem install ./amz_sp_api-0.1.0.gem
23
- ```
24
- (for development, run `gem install --dev ./amz_sp_api-0.1.0.gem` to install the development dependencies)
25
-
26
- or publish the gem to a gem hosting service, e.g. [RubyGems](https://rubygems.org/).
27
-
28
- Finally add this to the Gemfile:
29
-
30
- gem 'amz_sp_api', '~> 0.1'
31
-
32
- ### Install from Git
33
-
34
- If the Ruby gem is hosted at a git repository: https://github.com/ericcj/amz_sp_api, then add the following in the Gemfile:
35
-
36
- gem 'amz_sp_api', :git => 'https://github.com/ericcj/amz_sp_api.git', branch: 'main'
11
+ Add the gem to your Gemfile as per https://rubygems.org/gems/amz_sp_api
37
12
 
38
13
  ## Getting Started
39
14
 
@@ -48,8 +23,19 @@ require 'fulfillment-outbound-api-model'
48
23
  config.refresh_token =
49
24
  config.client_id =
50
25
  config.client_secret =
26
+
27
+ # either use these:
51
28
  config.aws_access_key_id =
52
29
  config.aws_secret_access_key =
30
+
31
+ # OR config.credentials_provider which is passed along to https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/Sigv4/Signer.html, e.g.
32
+ # require 'aws-sdk-core'
33
+ # config.credentials_provider = Aws::STS::Client.new(
34
+ # region: AmzSpApi::SpConfiguration::AWS_REGION_MAP['eu'],
35
+ # access_key_id: ,
36
+ # secret_access_key:
37
+ # ).assume_role(role_arn: , role_session_name: SecureRandom.uuid)
38
+
53
39
  config.region = 'eu'
54
40
  config.timeout = 20 # seconds
55
41
  # config.debugging = true
@@ -69,6 +55,31 @@ require 'fulfillment-outbound-api-model'
69
55
  end
70
56
  ```
71
57
 
58
+ ## Documentation
59
+
60
+ This is a handy way to see all the API model class names and corresponding files you need to require for them, e.g. require 'finances-api-model' to use https://www.rubydoc.info/gems/amz_sp_api/AmzSpApi/FinancesApiModel/DefaultApi
61
+
62
+ ## Feeds
63
+
64
+ This gem also offers encrypt/decrypt helper methods for feeds, but actually using that API as per https://github.com/amzn/selling-partner-api-docs/blob/main/guides/en-US/use-case-guides/feeds-api-use-case-guide requires the following calls:
65
+
66
+ ```ruby
67
+ feeds = AmzSpApi::FeedsApiModel::FeedsApi.new(AmzSpApi::SpApiClient.new)
68
+ response = feeds.create_feed_document({"contentType" => content_type})
69
+ feed_document_id = response&.payload&.dig(:feedDocumentId)
70
+ url = response.payload[:url]
71
+ encrypted = AmzSpApi.encrypt_feed(feed_content, response.payload)
72
+ # PUT to url with lowercase "content-type" header, it's already pre-signed
73
+ response = feeds.create_feed({"feedType" => feed_type, "marketplaceIds" => marketplace_ids, "inputFeedDocumentId" => feed_document_id})
74
+ feed_id = response&.payload&.dig(:feedId)
75
+ response = feeds.get_feed(feed_id)
76
+ result_feed_document_id = response&.payload&.dig(:resultFeedDocumentId) # present once it is successful
77
+ response = feeds.get_feed_document(result_feed_document_id)
78
+ url = response&.payload&.dig(:url)
79
+ # GET response&.payload&.dig(:url) into ciphertext, again it's pre-signed so no authorization needed
80
+ AmzSpApi.decrypt_and_inflate_feed(ciphertext, response.payload)
81
+ ```
82
+
72
83
  ## Thanks
73
84
 
74
85
  to https://github.com/patterninc/muffin_man as the basis for [sp_api_client.rb](lib/sp_api_client.rb)
Binary file
data/lib/amz_sp_api.rb CHANGED
@@ -15,5 +15,38 @@ module AmzSpApi
15
15
  SpConfiguration.default
16
16
  end
17
17
  end
18
+
19
+ def encrypt_feed(feed_content, feed_document_response_payload)
20
+ cipher = feed_cipher(feed_document_response_payload, encrypt: true)
21
+ cipher.update(feed_content) + cipher.final
22
+ end
23
+
24
+ def decrypt_and_inflate_feed(ciphertext, feed_document_response_payload)
25
+ cipher = feed_cipher(feed_document_response_payload, encrypt: false)
26
+
27
+ compression = feed_document_response_payload[:compressionAlgorithm]
28
+ raise "unknown compressionAlgorithm #{compression}" if compression && compression != "GZIP"
29
+
30
+ result = cipher.update(ciphertext) + cipher.final
31
+ result = Zlib::Inflate.inflate(result) if compression
32
+ result
33
+ end
34
+
35
+ # from https://github.com/amzn/selling-partner-api-models/blob/main/clients/sellingpartner-api-documents-helper-java/src/main/java/com/amazon/spapi/documents/impl/AESCryptoStreamFactory.java
36
+ def feed_cipher(response, encrypt:)
37
+ key = Base64.decode64(response.dig(:encryptionDetails, :key))
38
+
39
+ cipher = case response.dig(:encryptionDetails, :standard)
40
+ when "AES"
41
+ OpenSSL::Cipher.new("AES-#{key.size * 8}-CBC")
42
+ else
43
+ raise "unknown encryption standard #{response.inspect}"
44
+ end
45
+
46
+ encrypt ? cipher.encrypt : cipher.decrypt
47
+ cipher.key = key
48
+ cipher.iv = Base64.decode64(response.dig(:encryptionDetails, :initializationVector))
49
+ cipher
50
+ end
18
51
  end
19
52
  end
@@ -1,3 +1,3 @@
1
1
  module AmzSpApi
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amz_sp_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Jensen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-24 00:00:00.000000000 Z
11
+ date: 2021-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -96,6 +96,7 @@ files:
96
96
  - LICENSE
97
97
  - README.md
98
98
  - Rakefile
99
+ - amz_sp_api-0.2.0.gem
99
100
  - amz_sp_api.gemspec
100
101
  - codegen.sh
101
102
  - config.json