lib/peddler/client.rb in peddler-1.6.7 vs lib/peddler/client.rb in peddler-2.0.0
- old
+ new
@@ -1,160 +1,108 @@
# frozen_string_literal: true
require 'forwardable'
require 'jeff'
-require 'peddler/errors/parser'
+require 'peddler/errors/builder'
require 'peddler/marketplace'
require 'peddler/operation'
require 'peddler/parser'
module Peddler
# An abstract client
#
- # Subclass to implement an MWS API section.
+ # Subclass this to implement an MWS API section.
class Client
extend Forwardable
include Jeff
- # The MWSAuthToken used to access another seller's account
- # @return [String]
- attr_accessor :auth_token
-
- attr_writer :merchant_id, :primary_marketplace_id, :path
-
- # @api private
- attr_writer :version
-
- # The body of the HTTP request
- # @return [String]
- attr_reader :body
-
- alias configure tap
-
- def_delegators :marketplace, :host, :encoding
-
- params(
- 'SellerId' => -> { merchant_id },
- 'MWSAuthToken' => -> { auth_token },
- 'Version' => -> { version }
- )
-
class << self
# @api private
- attr_accessor :error_handler, :parser
+ attr_accessor :parser, :path, :version
- # @api private
- def path(path = nil)
- path ? @path = path : @path ||= '/'
- end
-
- # @api private
- def version(version = nil)
- version ? @version = version : @version ||= nil
- end
-
- # Sets an error handler
- # @yieldparam [Excon::Error] error
- def on_error(&blk)
- @error_handler = blk
- end
-
private
def inherited(base)
base.parser = parser
- base.error_handler = error_handler
- base.path(path)
- base.params(params)
+ base.params params
end
end
- self.error_handler = proc { raise }
+ params 'SellerId' => -> { merchant_id },
+ 'MWSAuthToken' => -> { auth_token },
+ 'Version' => -> { version }
self.parser = Parser
- # Creates a new client instance
- #
+ def_delegators :marketplace, :host, :encoding
+ def_delegators :'self.class', :parser, :version
+
+ # Creates a new client
# @param [Hash] opts
- # @option opts [String] :primary_marketplace_id
- # @option opts [String] :merchant_id
# @option opts [String] :aws_access_key_id
# @option opts [String] :aws_secret_access_key
+ # @option opts [String, Peddler::Marketplace] :marketplace
+ # @option opts [String] :merchant_id
# @option opts [String] :auth_token
def initialize(opts = {})
opts.each { |k, v| send("#{k}=", v) }
end
- # @api private
- def aws_endpoint
- "https://#{host}#{path}"
- end
+ # The MWS Auth Token for a seller's account
+ # @note You can omit this if you are accessing your own seller account
+ # @return [String]
+ attr_accessor :auth_token
- # The merchant's Marketplace ID
- # @!parse attr_reader :primary_marketplace_id
+ # The seller's Merchant ID
# @return [String]
- def primary_marketplace_id
- @primary_marketplace_id ||= ENV['MWS_MARKETPLACE_ID']
- end
+ attr_accessor :merchant_id
- # @deprecated Use {#primary_marketplace_id}.
- def marketplace_id
- @primary_marketplace_id
- end
+ # The marketplace where you signed up as application developer
+ # @note You can pass the two-letter country code of the marketplace as
+ # shorthand when setting
+ # @return [Peddler::Marketplace]
+ attr_reader :marketplace
- # @deprecated Use {#primary_marketplace_id=}.
- def marketplace_id=(marketplace_id)
- @primary_marketplace_id = marketplace_id
+ # @!parse attr_writer :marketplace
+ def marketplace=(marketplace)
+ @marketplace =
+ if marketplace.is_a?(Marketplace)
+ marketplace
+ else
+ Marketplace.find(marketplace)
+ end
end
- # The merchant's Seller ID
- # @!parse attr_reader :merchant_id
+ # The body of the HTTP request
# @return [String]
- def merchant_id
- @merchant_id ||= ENV['MWS_MERCHANT_ID']
+ attr_reader :body
+
+ # @!parse attr_writer :body
+ def body=(str)
+ str ? add_content(str) : clear_content!
end
# @api private
- def marketplace
- @marketplace ||= find_marketplace
- end
+ attr_writer :path
- # The HTTP path of the API
- # @!parse attr_reader :path
- # @return [String]
+ # @api private
def path
@path ||= self.class.path
end
# @api private
- def version
- @version ||= self.class.version
- end
-
- # @!parse attr_writer :body
- def body=(str)
- str ? add_content(str) : clear_content!
- end
-
- # @api private
def defaults
@defaults ||= { expects: 200 }
end
# @api private
def headers
@headers ||= {}
end
- # Sets an error handler
- # @yieldparam [Excon::Error] error
- def on_error(&blk)
- @error_handler = blk
- end
-
# @api private
- def error_handler
- (@error_handler ||= nil) || self.class.error_handler
+ def aws_endpoint
+ "https://#{host}#{path}"
end
# @api private
def operation(action = nil)
action ? @operation = Operation.new(action) : @operation
@@ -166,20 +114,16 @@
opts.store(:response_block, Proc.new) if block_given?
res = post(opts)
self.body = nil if res.status == 200
parser.new(res, encoding)
- rescue Excon::Error => e
- handle_error(e)
+ rescue ::Excon::Error::HTTPStatus => error
+ handle_http_status_error(error)
end
private
- def find_marketplace
- Marketplace.find(primary_marketplace_id)
- end
-
def clear_content!
headers.delete('Content-Type')
@body = nil
end
@@ -196,37 +140,16 @@
def extract_options(args)
args.last.is_a?(Hash) ? args.pop : {}
end
- def parser
- self.class.parser
- end
-
def build_options
opts = defaults.merge(query: operation, headers: headers)
body ? opts.update(body: body) : opts
end
- def handle_error(e)
- e = decorate_error(e)
- error_handler.call(*deprecate_error_handler_arguments(e))
- end
-
- def decorate_error(e)
- if e.is_a?(:: Excon::Error::HTTPStatus)
- e.instance_variable_set(:@response, Errors::Parser.new(e.response))
- end
-
- e
- end
-
- def deprecate_error_handler_arguments(e)
- if error_handler.parameters.size == 2
- warn '[DEPRECATION] Error handler now expects exception as argument.'
- [e.request, e.response]
- else
- [e]
- end
+ def handle_http_status_error(error)
+ new_error = Errors::Builder.call(error)
+ raise new_error || error
end
end
end