class MuffinMan::SpApiClient

Constants

ACCESS_TOKEN_URL
AWS_REGION_MAP
SERVICE_NAME

Attributes

aws_access_key_id[R]
aws_secret_access_key[R]
client_id[R]
client_secret[R]
config[R]
refresh_token[R]
region[R]
sandbox[R]
sts_iam_role_arn[R]

Public Class Methods

new(credentials, sandbox = false) click to toggle source
# File lib/muffin_man/sp_api_client.rb, line 19
def initialize(credentials, sandbox = false)
  @refresh_token = credentials[:refresh_token]
  @client_id = credentials[:client_id]
  @client_secret = credentials[:client_secret]
  @aws_access_key_id = credentials[:aws_access_key_id]
  @aws_secret_access_key = credentials[:aws_secret_access_key]
  @sts_iam_role_arn = credentials[:sts_iam_role_arn]
  @region = credentials[:region] || 'na'
  @sandbox = sandbox
  Typhoeus::Config.user_agent = ''
  @config = MuffinMan.configuration
end

Private Instance Methods

call_api() click to toggle source
# File lib/muffin_man/sp_api_client.rb, line 34
def call_api
  Typhoeus.send(request_type.downcase.to_sym, request.url, headers: headers)
end
canonical_uri() click to toggle source
# File lib/muffin_man/sp_api_client.rb, line 47
def canonical_uri
  # local_var_path is defined in subclasses
  "#{sp_api_url}#{local_var_path}"
end
derive_aws_region() click to toggle source
# File lib/muffin_man/sp_api_client.rb, line 122
def derive_aws_region
  @aws_region ||= AWS_REGION_MAP[region]
  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
  @aws_region
end
headers() click to toggle source
# File lib/muffin_man/sp_api_client.rb, line 113
def headers
  headers = {
    'x-amz-access-token' => retrieve_lwa_access_token,
    'user-agent' => "MuffinMan/#{VERSION} (Language=Ruby)",
    'content-type' => "application/json"
  }
  signed_request.headers.merge(headers)
end
request() click to toggle source
# File lib/muffin_man/sp_api_client.rb, line 52
def request
  @request ||= Typhoeus::Request.new(
    canonical_uri,
    params: query_params
  )
end
request_lwa_access_token() click to toggle source
# File lib/muffin_man/sp_api_client.rb, line 71
def request_lwa_access_token
  body = {
    grant_type: 'refresh_token',
    refresh_token: refresh_token,
    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'
    }
  )
  JSON.parse(response.body)
end
request_sts_token() click to toggle source
# File lib/muffin_man/sp_api_client.rb, line 88
def request_sts_token
  client = Aws::STS::Client.new(
    region: derive_aws_region,
    access_key_id: aws_access_key_id,
    secret_access_key: aws_secret_access_key
  )
  client.assume_role(role_arn: sts_iam_role_arn, role_session_name: SecureRandom.uuid)
end
retrieve_lwa_access_token() click to toggle source
# File lib/muffin_man/sp_api_client.rb, line 59
def retrieve_lwa_access_token
  return request_lwa_access_token['access_token'] unless defined?(config.get_access_token)
  stored_token = config.get_access_token.call(client_id)
  if stored_token.nil?
    new_token = request_lwa_access_token
    config.save_access_token.call(client_id, new_token) if defined?(config.save_access_token)
    return new_token['access_token']
  else
    return stored_token
  end
end
signed_request() click to toggle source
# File lib/muffin_man/sp_api_client.rb, line 97
def signed_request
  request_config = {
    service: SERVICE_NAME,
    region: derive_aws_region,
    endpoint: sp_api_host
  }
  if sts_iam_role_arn.nil?
    request_config[:access_key_id] = aws_access_key_id
    request_config[:secret_access_key] = aws_secret_access_key
  else
    request_config[:credentials_provider] = request_sts_token
  end
  signer = Aws::Sigv4::Signer.new(request_config)
  signer.sign_request(http_method: request_type, url: request.url)
end
sp_api_host() click to toggle source
# File lib/muffin_man/sp_api_client.rb, line 38
def sp_api_host
  hostname = "sellingpartnerapi-#{region}.amazon.com"
  sandbox ? hostname.prepend("sandbox.") : hostname
end
sp_api_url() click to toggle source
# File lib/muffin_man/sp_api_client.rb, line 43
def sp_api_url
  "https://#{sp_api_host}"
end