amz_sp_api 0.2.1 → 0.2.2

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: 129a4bc559aca17253362d03710e1fb6b5dcc55c
4
- data.tar.gz: 7e0dbcb797f06e10deaebb2532add432cb58ac1f
3
+ metadata.gz: b1935461ccb847a6cbcde207477ffc65875ffaf6
4
+ data.tar.gz: c218101c0d50578bd07847e6cd0c277b8e68a12b
5
5
  SHA512:
6
- metadata.gz: 16ea2e4a0c8843c7f4a034d2bbfd5dff049079a1391a44da6d4aa864f48c351233ebcab67ca7b219850ca4a601f4ec4a8c7b79534bc3b268634e55497523b91b
7
- data.tar.gz: 5c4154fd81e34f66f0c1ad31960cf338762097a705b147c44a496769f555520f0ea5250955cd707f8364d78b018288e02043e445d45efa596b79ea06ba6910ac
6
+ metadata.gz: 39e75fe5e3a4a7ea79b884e57bb723df123b025eb46128d99374d3e96a00b34f422e54757526d53fe7f361cd81f238615fbff16308ca6fdeac0324cd149bc1f1
7
+ data.tar.gz: 2c23f34bfc73d8c09df0c219b677a7b1ae66901533f4595d22285bd6302f865bd9f6c83daaade2dc4a5e2e20740adc607dfdd7fae50e2a3a569661eef84ed6de
data/README.md CHANGED
@@ -59,9 +59,9 @@ require 'fulfillment-outbound-api-model'
59
59
 
60
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
61
 
62
- ## Feeds
62
+ ## Feeds and reports
63
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:
64
+ This gem also offers encrypt/decrypt helper methods for feeds and reports, 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, e.g. for feeds but reports is the same pattern:
65
65
 
66
66
  ```ruby
67
67
  feeds = AmzSpApi::FeedsApiModel::FeedsApi.new(AmzSpApi::SpApiClient.new)
@@ -77,7 +77,7 @@ result_feed_document_id = response&.payload&.dig(:resultFeedDocumentId) # presen
77
77
  response = feeds.get_feed_document(result_feed_document_id)
78
78
  url = response&.payload&.dig(:url)
79
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)
80
+ AmzSpApi.decrypt_and_inflate_document(ciphertext, response.payload)
81
81
  ```
82
82
 
83
83
  ## Thanks
Binary file
data/lib/amz_sp_api.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'amz_sp_api_version'
2
+ require 'api_error'
2
3
  require 'sp_api_client'
3
4
  require 'sp_configuration'
4
5
 
@@ -16,37 +17,43 @@ module AmzSpApi
16
17
  end
17
18
  end
18
19
 
19
- def encrypt_feed(feed_content, feed_document_response_payload)
20
- cipher = feed_cipher(feed_document_response_payload, encrypt: true)
20
+ def encrypt_feed(feed_content, document_response_payload)
21
+ cipher = document_cipher(document_response_payload, encrypt: true)
21
22
  cipher.update(feed_content) + cipher.final
22
23
  end
23
24
 
24
- def decrypt_and_inflate_feed(ciphertext, feed_document_response_payload)
25
- cipher = feed_cipher(feed_document_response_payload, encrypt: false)
25
+ def decrypt_and_inflate_document(ciphertext, document_response_payload)
26
+ body = if cipher = document_cipher(document_response_payload, encrypt: false)
27
+ cipher.update(ciphertext) + cipher.final
28
+ else
29
+ ciphertext
30
+ end
26
31
 
27
- compression = feed_document_response_payload[:compressionAlgorithm]
28
- raise "unknown compressionAlgorithm #{compression}" if compression && compression != "GZIP"
32
+ inflate_document(body, document_response_payload)
33
+ end
34
+ alias_method :decrypt_and_inflate_feed, :decrypt_and_inflate_document
29
35
 
30
- result = cipher.update(ciphertext) + cipher.final
31
- result = Zlib::Inflate.inflate(result) if compression
32
- result
36
+ def inflate_document(body, document_response_payload)
37
+ compression = document_response_payload[:compressionAlgorithm]
38
+ raise AmzSpApi::ApiError.new("unknown compressionAlgorithm #{compression}") if compression && compression != "GZIP"
39
+ compression ? Zlib::Inflate.inflate(body) : body
33
40
  end
34
41
 
35
42
  # 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}"
43
+ def document_cipher(response, encrypt:)
44
+ if key = Base64.decode64(response.dig(:encryptionDetails, :key))
45
+ cipher = case response.dig(:encryptionDetails, :standard)
46
+ when "AES"
47
+ OpenSSL::Cipher.new("AES-#{key.size * 8}-CBC")
48
+ else
49
+ raise AmzSpApi::ApiError.new("unknown encryption standard #{response.inspect}")
50
+ end
51
+
52
+ encrypt ? cipher.encrypt : cipher.decrypt
53
+ cipher.key = key
54
+ cipher.iv = Base64.decode64(response.dig(:encryptionDetails, :initializationVector))
55
+ cipher
44
56
  end
45
-
46
- encrypt ? cipher.encrypt : cipher.decrypt
47
- cipher.key = key
48
- cipher.iv = Base64.decode64(response.dig(:encryptionDetails, :initializationVector))
49
- cipher
50
57
  end
51
58
  end
52
59
  end
@@ -1,3 +1,3 @@
1
1
  module AmzSpApi
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
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.1
4
+ version: 0.2.2
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-04-20 00:00:00.000000000 Z
11
+ date: 2021-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -97,6 +97,7 @@ files:
97
97
  - README.md
98
98
  - Rakefile
99
99
  - amz_sp_api-0.2.0.gem
100
+ - amz_sp_api-0.2.1.gem
100
101
  - amz_sp_api.gemspec
101
102
  - codegen.sh
102
103
  - config.json