muffin_man 1.2.0 → 1.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bed4f016d8045bd0f40bfe4e4ca1be2467fac83ff0549674610c20ad3cba9795
4
- data.tar.gz: f955bc7597541ed72eeaeb343d42d83a26892acf31fd3041a287697ae05c6828
3
+ metadata.gz: 82c3118e9e61b82872329714fbfbaf80fd7613ff98ce126992f2ac3b834e3a51
4
+ data.tar.gz: 201afe55ba25967d8e519ff5f6a0ac9a66e452462cc4fe805ab2a34b61edec9a
5
5
  SHA512:
6
- metadata.gz: faed598abf08c41f6e68b627fddaf35e2ccf1a64b95585167827aff7dd996093e6051c2f8caa7e939fe9545b8c91876c8df50bf74f97620b9cb9137934ff5b36
7
- data.tar.gz: 0b9a3a286fcd8dc499fc209a276a11f933270f8e430910281f729b817d2f1eebe684d835e0b73250127888550f0f190353c2f84fe51513d65711136e7b7c90a0
6
+ metadata.gz: 9411d153ce20efc02882b456a9b6be233cf88e691472d35bbca5b8890fc422395d5a925a66996c129a1a5835883ac66153415043a94e37ad2d9c6e7e3b17baf5
7
+ data.tar.gz: 7859bc864e708516ce37087a32791a1fbbae140af2307fbc8d7c530933517e1d914faf704151b7269c290853cfa3a44c741c552a8aadf06bf605cc5ba2745d28
data/.rubocop.yml CHANGED
@@ -11,3 +11,6 @@ Style/StringLiteralsInInterpolation:
11
11
 
12
12
  Layout/LineLength:
13
13
  Max: 120
14
+
15
+ Metrics/MethodLength:
16
+ Max: 30
data/README.md CHANGED
@@ -63,6 +63,27 @@ MuffinMan.configure do |config|
63
63
  end
64
64
  ```
65
65
 
66
+ ### Retrieiving the refresh token
67
+
68
+ To retrieve the refresh token from an LWA Website authorization workflow, you can use the LWA helper:
69
+
70
+ ```ruby
71
+ # Get your auth code first, either through the Website oauth flow or Authorization API
72
+ credentials = {
73
+ client_id: CLIENT_ID,
74
+ client_secret: CLIENT_SECRET,
75
+ aws_access_key_id: AWS_ACCESS_KEY_ID,
76
+ aws_secret_access_key: AWS_SECRET_ACCESS_KEY,
77
+ sts_iam_role_arn: STS_IAM_ROLE_ARN, # Optional
78
+ scope: 'sellingpartnerapi::migration' # Grantless scope for MWS migration
79
+ }
80
+ client = MuffinMan::Authorization::V1.new(credentials)
81
+ resp = JSON.parse(client.get_authorization_code(seller_id, developer_id, mws_auth_token).body)
82
+ auth_code = resp['payload']['authorizationCode']
83
+ # Then query retrieve the refresh token to store
84
+ refresh_token = MuffinMan::Lwa::AuthHelper.get_refresh_token(CLIENT_ID, CLIENT_SECRET, auth_code)
85
+ ```
86
+
66
87
  ## Contributing
67
88
 
68
89
  Bug reports and pull requests are welcome on GitHub at https://github.com/patterninc/muffin_man. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/patterninc/muffin_man/blob/master/CODE_OF_CONDUCT.md).
@@ -7,7 +7,6 @@ module MuffinMan
7
7
  "developerId" => developer_id,
8
8
  "mwsAuthToken" => mws_auth_token
9
9
  }
10
- @query_params = {} if sandbox
11
10
  @request_type = "GET"
12
11
  @local_var_path = "/authorization/v1/authorizationCode"
13
12
  call_api
@@ -1,14 +1,19 @@
1
1
  module MuffinMan
2
2
  module Finances
3
3
  class V0 < SpApiClient
4
-
5
4
  def list_financial_event_groups(max_results_per_page = nil, financial_event_group_started_before = nil, financial_event_group_started_after = nil, next_token = nil)
6
5
  @local_var_path = "/finances/v0/financialEventGroups"
7
6
  @query_params = {}
8
- @query_params['MaxResultsPerPage'] = max_results_per_page unless max_results_per_page.nil?
9
- @query_params['FinancialEventGroupStartedBefore'] = financial_event_group_started_before unless financial_event_group_started_before.nil?
10
- @query_params['FinancialEventGroupStartedAfter'] = financial_event_group_started_after unless financial_event_group_started_after.nil?
11
- @query_params['NextToken'] = next_token unless next_token.nil?
7
+ @query_params["MaxResultsPerPage"] = max_results_per_page unless max_results_per_page.nil?
8
+ unless financial_event_group_started_before.nil?
9
+ @query_params["FinancialEventGroupStartedBefore"] =
10
+ financial_event_group_started_before
11
+ end
12
+ unless financial_event_group_started_after.nil?
13
+ @query_params["FinancialEventGroupStartedAfter"] =
14
+ financial_event_group_started_after
15
+ end
16
+ @query_params["NextToken"] = next_token unless next_token.nil?
12
17
  @request_type = "GET"
13
18
  call_api
14
19
  end
@@ -16,10 +21,10 @@ module MuffinMan
16
21
  def list_financial_events_by_group_id(event_group_id, max_results_per_page = nil, posted_after = nil, posted_before = nil, next_token = nil)
17
22
  @local_var_path = "/finances/v0/financialEventGroups/#{event_group_id}/financialEvents"
18
23
  @query_params = {}
19
- @query_params['MaxResultsPerPage'] = max_results_per_page unless max_results_per_page.nil?
20
- @query_params['PostedAfter'] = posted_after unless posted_after.nil?
21
- @query_params['PostedBefore'] = posted_before unless posted_before.nil?
22
- @query_params['NextToken'] = next_token unless next_token.nil?
24
+ @query_params["MaxResultsPerPage"] = max_results_per_page unless max_results_per_page.nil?
25
+ @query_params["PostedAfter"] = posted_after unless posted_after.nil?
26
+ @query_params["PostedBefore"] = posted_before unless posted_before.nil?
27
+ @query_params["NextToken"] = next_token unless next_token.nil?
23
28
  @request_type = "GET"
24
29
  call_api
25
30
  end
@@ -0,0 +1,27 @@
1
+ module MuffinMan::Lwa
2
+ class AuthHelper
3
+ ACCESS_TOKEN_URL = "https://api.amazon.com/auth/o2/token".freeze
4
+
5
+ def self.get_refresh_token(client_id, client_secret, auth_code)
6
+ body = {
7
+ grant_type: "authorization_code",
8
+ code: auth_code,
9
+ client_id: client_id,
10
+ client_secret: client_secret
11
+ }
12
+ response = Typhoeus.post(
13
+ ACCESS_TOKEN_URL,
14
+ body: URI.encode_www_form(body),
15
+ headers: {
16
+ "Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8"
17
+ }
18
+ )
19
+ if response.code != 200
20
+ error_body = JSON.parse(response.body)
21
+ error = "#{error_body["error"]}: #{error_body["error_description"]}"
22
+ raise MuffinMan::Error, error
23
+ end
24
+ JSON.parse(response.body)["refresh_token"]
25
+ end
26
+ end
27
+ end
@@ -44,7 +44,7 @@ module MuffinMan
44
44
  @local_var_path = "/reports/2021-06-30/reports"
45
45
  @request_body = {
46
46
  "reportType" => report_type,
47
- "marketplaceIds" => marketplace_ids,
47
+ "marketplaceIds" => marketplace_ids
48
48
  }
49
49
  @request_body["dataStartTime"] = start_time unless start_time.nil?
50
50
  @request_body["dataEndTime"] = end_time unless end_time.nil?
@@ -11,9 +11,11 @@ module MuffinMan
11
11
  def create_product_review_and_seller_feedback_solicitation(amazon_order_id, marketplace_ids)
12
12
  @amazon_order_id = sandbox ? SANDBOX_AMAZON_ORDER_ID : amazon_order_id
13
13
  @marketplace_ids = sandbox ? SANDBOX_MARKETPLACE_IDS : marketplace_ids
14
- @local_var_path = '/solicitations/v1/orders/{amazonOrderId}/solicitations/productReviewAndSellerFeedback'.sub('{' + 'amazonOrderId' + '}', @amazon_order_id)
14
+ @local_var_path = "/solicitations/v1/orders/{amazonOrderId}/solicitations/productReviewAndSellerFeedback".sub(
15
+ "{" + "amazonOrderId" + "}", @amazon_order_id
16
+ )
15
17
  @query_params = { "marketplaceIds" => @marketplace_ids }
16
- @request_type = 'POST'
18
+ @request_type = "POST"
17
19
  call_api
18
20
  end
19
21
  end
@@ -1,20 +1,21 @@
1
- require 'aws-sigv4'
2
- require 'uri'
3
- require 'aws-sdk-core'
4
- require 'typhoeus'
5
- require 'securerandom'
1
+ require "aws-sigv4"
2
+ require "uri"
3
+ require "aws-sdk-core"
4
+ require "typhoeus"
5
+ require "securerandom"
6
6
 
7
7
  module MuffinMan
8
8
  class SpApiClient
9
9
  attr_reader :refresh_token, :client_id, :client_secret, :aws_access_key_id,
10
- :aws_secret_access_key, :sts_iam_role_arn, :sandbox, :config, :region, :request_type,
11
- :local_var_path, :query_params, :request_body, :scope
12
- ACCESS_TOKEN_URL = 'https://api.amazon.com/auth/o2/token'.freeze
13
- SERVICE_NAME = 'execute-api'.freeze
10
+ :aws_secret_access_key, :sts_iam_role_arn, :sandbox, :config, :region, :request_type,
11
+ :local_var_path, :query_params, :request_body, :scope
12
+
13
+ ACCESS_TOKEN_URL = "https://api.amazon.com/auth/o2/token".freeze
14
+ SERVICE_NAME = "execute-api".freeze
14
15
  AWS_REGION_MAP = {
15
- 'na' => 'us-east-1',
16
- 'eu' => 'eu-west-1',
17
- 'fe' => 'us-west-2'
16
+ "na" => "us-east-1",
17
+ "eu" => "eu-west-1",
18
+ "fe" => "us-west-2"
18
19
  }.freeze
19
20
 
20
21
  def initialize(credentials, sandbox = false)
@@ -24,10 +25,10 @@ module MuffinMan
24
25
  @aws_access_key_id = credentials[:aws_access_key_id]
25
26
  @aws_secret_access_key = credentials[:aws_secret_access_key]
26
27
  @sts_iam_role_arn = credentials[:sts_iam_role_arn]
27
- @region = credentials[:region] || 'na'
28
+ @region = credentials[:region] || "na"
28
29
  @scope = credentials[:scope]
29
30
  @sandbox = sandbox
30
- Typhoeus::Config.user_agent = ''
31
+ Typhoeus::Config.user_agent = ""
31
32
  @config = MuffinMan.configuration
32
33
  end
33
34
 
@@ -39,9 +40,7 @@ module MuffinMan
39
40
 
40
41
  def request_opts
41
42
  opts = { headers: headers }
42
- if request_body
43
- opts[:body] = request_body.to_json
44
- end
43
+ opts[:body] = request_body.to_json if request_body
45
44
  opts
46
45
  end
47
46
 
@@ -64,20 +63,21 @@ module MuffinMan
64
63
  end
65
64
 
66
65
  def retrieve_lwa_access_token
67
- return request_lwa_access_token['access_token'] unless defined?(config.get_access_token)
66
+ return request_lwa_access_token["access_token"] unless defined?(config.get_access_token)
67
+
68
68
  stored_token = config.get_access_token.call(client_id)
69
69
  if stored_token.nil?
70
70
  new_token = request_lwa_access_token
71
71
  config.save_access_token.call(client_id, new_token) if defined?(config.save_access_token)
72
- return new_token['access_token']
72
+ new_token["access_token"]
73
73
  else
74
- return stored_token
74
+ stored_token
75
75
  end
76
76
  end
77
77
 
78
78
  def request_lwa_access_token
79
79
  body = {
80
- grant_type: 'refresh_token',
80
+ grant_type: "refresh_token",
81
81
  refresh_token: refresh_token,
82
82
  client_id: client_id,
83
83
  client_secret: client_secret
@@ -86,7 +86,7 @@ module MuffinMan
86
86
  ACCESS_TOKEN_URL,
87
87
  body: URI.encode_www_form(body),
88
88
  headers: {
89
- 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'
89
+ "Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8"
90
90
  }
91
91
  )
92
92
  JSON.parse(response.body)
@@ -142,16 +142,20 @@ module MuffinMan
142
142
  def headers
143
143
  access_token = scope ? retrieve_grantless_access_token : retrieve_lwa_access_token
144
144
  headers = {
145
- 'x-amz-access-token' => access_token,
146
- 'user-agent' => "MuffinMan/#{VERSION} (Language=Ruby)",
147
- 'content-type' => "application/json"
145
+ "x-amz-access-token" => access_token,
146
+ "user-agent" => "MuffinMan/#{VERSION} (Language=Ruby)",
147
+ "content-type" => "application/json"
148
148
  }
149
149
  signed_request.headers.merge(headers)
150
150
  end
151
151
 
152
152
  def derive_aws_region
153
153
  @aws_region ||= AWS_REGION_MAP[region]
154
- raise MuffinMan::Error.new("#{region} is not supported or does not exist. Region must be one of the following: #{AWS_REGION_MAP.keys.join(', ')}") unless @aws_region
154
+ unless @aws_region
155
+ raise MuffinMan::Error,
156
+ "#{region} is not supported or does not exist. Region must be one of the following: #{AWS_REGION_MAP.keys.join(", ")}"
157
+ end
158
+
155
159
  @aws_region
156
160
  end
157
161
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MuffinMan
4
- VERSION = "1.2.0"
4
+ VERSION = "1.3.0"
5
5
  end
data/lib/muffin_man.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "muffin_man/version"
2
2
  require "muffin_man/sp_api_client"
3
+ require "muffin_man/lwa/auth_helper"
3
4
  require "muffin_man/solicitations/v1"
4
5
  require "muffin_man/reports/v20210630"
5
6
  require "muffin_man/catalog_items/v20201201"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: muffin_man
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2022-04-21 00:00:00.000000000 Z
13
+ date: 2022-05-02 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -126,6 +126,7 @@ files:
126
126
  - lib/muffin_man/authorization/v1.rb
127
127
  - lib/muffin_man/catalog_items/v20201201.rb
128
128
  - lib/muffin_man/finances/v0.rb
129
+ - lib/muffin_man/lwa/auth_helper.rb
129
130
  - lib/muffin_man/reports/v20210630.rb
130
131
  - lib/muffin_man/solicitations/v1.rb
131
132
  - lib/muffin_man/sp_api_client.rb