diff --git a/lib/muffin_man.rb b/lib/muffin_man.rb index 871904c..45cdf23 100644 --- a/lib/muffin_man.rb +++ b/lib/muffin_man.rb @@ -38,6 +38,7 @@ require "muffin_man/customer_feedback/v20240601" require "muffin_man/uploads/v20201101" require "muffin_man/aplus_content/v20201101" +require "muffin_man/application_management/v20231130" module MuffinMan class Error < StandardError; end diff --git a/lib/muffin_man/application_management/v20231130.rb b/lib/muffin_man/application_management/v20231130.rb new file mode 100644 index 0000000..eadd782 --- /dev/null +++ b/lib/muffin_man/application_management/v20231130.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module MuffinMan + module ApplicationManagement + class V20231130 + def self.rotate_application_client_secret(access_token) + Typhoeus.post( + "https://sellingpartnerapi-na.amazon.com/applications/2023-11-30/clientSecret", + headers: { "x-amz-access-token" => access_token } + ) + end + end + end +end diff --git a/lib/muffin_man/lwa/auth_helper.rb b/lib/muffin_man/lwa/auth_helper.rb index caccfa1..3f53917 100644 --- a/lib/muffin_man/lwa/auth_helper.rb +++ b/lib/muffin_man/lwa/auth_helper.rb @@ -23,5 +23,28 @@ def self.get_refresh_token(client_id, client_secret, auth_code) end JSON.parse(response.body)["refresh_token"] end + + def self.get_access_token(scope, client_id, client_secret) + body = { + grant_type: "client_credentials", + scope: scope, + client_id: client_id, + client_secret: client_secret + } + + response = Typhoeus.post( + ACCESS_TOKEN_URL, + body: URI.encode_www_form(body), + headers: { + "Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8" + } + ) + if response.code != 200 + error_body = JSON.parse(response.body) + error = "#{error_body["error"]}: #{error_body["error_description"]}" + raise MuffinMan::Error, error + end + JSON.parse(response.body)["access_token"] + end end end diff --git a/spec/muffin_man/application_management/v20231130_spec.rb b/spec/muffin_man/application_management/v20231130_spec.rb new file mode 100644 index 0000000..7ca3f72 --- /dev/null +++ b/spec/muffin_man/application_management/v20231130_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +RSpec.describe MuffinMan::ApplicationManagement::V20231130 do + let(:access_token) { "test_access_token" } + let(:new_app_credential_url) { "https://sellingpartnerapi-na.amazon.com/applications/2023-11-30/clientSecret" } + + describe ".rotate_application_client_secret" do + let(:response) { instance_double(Typhoeus::Response) } + + before do + allow(Typhoeus).to receive(:post).and_return(response) + end + + context "when the request is successful (204)" do + before do + allow(response).to receive(:code).and_return(204) + allow(response).to receive(:body).and_return("") + end + + it "returns the response and makes the correct API call" do + expect(Typhoeus).to receive(:post).with( + new_app_credential_url, + headers: { "x-amz-access-token" => access_token } + ) + + result = described_class.rotate_application_client_secret(access_token) + expect(result).to eq(response) + end + end + end +end