lib/easypost.rb in easypost-4.0.0 vs lib/easypost.rb in easypost-4.1.0

- old
+ new

@@ -7,10 +7,11 @@ require 'easypost/version' require 'easypost/util' require 'easypost/object' require 'easypost/resource' require 'easypost/error' +require 'easypost/connection' # Resources require 'easypost/address' require 'easypost/api_key' require 'easypost/batch' @@ -35,124 +36,105 @@ require 'easypost/tracker' require 'easypost/user' require 'easypost/webhook' module EasyPost - @api_key = nil - @api_base = 'https://api.easypost.com' + DEFAULT_API_BASE = 'https://api.easypost.com' + DEFAULT_USER_AGENT = "EasyPost/v2 RubyClient/#{EasyPost::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}" - # Set the ApiKey. - def self.api_key=(api_key) - @api_key = api_key + class << self + attr_accessor :api_key, :api_base + attr_writer :default_connection end - # Get the ApiKey. - def self.api_key - @api_key + self.api_base = DEFAULT_API_BASE + + def self.default_headers + @default_headers ||= { + 'Content-Type' => 'application/json', + 'User-Agent' => EasyPost::DEFAULT_USER_AGENT, + } end - # Set the API base. - def self.api_base=(api_base) - @api_base = api_base + def self.default_connection + @default_connection ||= EasyPost::Connection.new( + uri: URI(api_base), + config: http_config, + ) end - # Get the API base. - def self.api_base - @api_base + def self.authorization(key) + "Basic #{Base64.strict_encode64("#{key}:")}" end # Reset the HTTP config. def self.reset_http_config - @http_config = { + http_config.clear + self.default_connection = nil + end + + def self.default_http_config + http_config = { timeout: 60, open_timeout: 30, verify_ssl: OpenSSL::SSL::VERIFY_PEER, } ruby_version = Gem::Version.new(RUBY_VERSION) if ruby_version >= Gem::Version.new('2.5.0') - @http_config[:min_version] = OpenSSL::SSL::TLS1_2_VERSION + http_config[:min_version] = OpenSSL::SSL::TLS1_2_VERSION else - @http_config[:ssl_version] = :TLSv1_2 # rubocop:disable Naming/VariableNumber + http_config[:ssl_version] = :TLSv1_2 # rubocop:disable Naming/VariableNumber end - @http_config + http_config end # Get the HTTP config. def self.http_config - @http_config ||= reset_http_config + @http_config ||= default_http_config end # Set the HTTP config. def self.http_config=(http_config_params) http_config.merge!(http_config_params) + + self.default_connection = nil end # Create an EasyPost Client. - def self.make_client(uri) - client = if http_config[:proxy] - proxy_uri = URI(http_config[:proxy]) - Net::HTTP.new( - uri.host, - uri.port, - proxy_uri.host, - proxy_uri.port, - proxy_uri.user, - proxy_uri.password, - ) - else - Net::HTTP.new(uri.host, uri.port) - end - client.use_ssl = true - - http_config.each do |name, value| - # Discrepancies between RestClient and Net::HTTP. - case name - when :verify_ssl - name = :verify_mode - when :timeout - name = :read_timeout - end - - # Handled in the creation of the client. - if name == :proxy - next - end - - client.send("#{name}=", value) - end - - client + # + # @deprecated + def self.make_client(url) + EasyPost::Connection.new(uri: URI(url), config: http_config).create end - # Make an HTTP request. + # Make an HTTP request against the {default_connection} + # + # @param method [Symbol] the HTTP Verb (get, method, put, post, etc.) + # @param path [String] URI path of the resource + # @param requested_api_key [String] ({EasyPost.api_key}) key set Authorization header. + # @param body [Object] (nil) object to be dumped to JSON + # @raise [EasyPost::Error] if the response has a non-2xx status code + # @return [Hash] JSON object parsed from the response body def self.make_request(method, path, api_key = nil, body = nil) - client = make_client(URI(@api_base)) + default_connection.call(method, path, api_key || EasyPost.api_key, body) + end - request = Net::HTTP.const_get(method.capitalize).new(path) - if body - request.body = JSON.dump(EasyPost::Util.objects_to_ids(body)) - end + def self.parse_response(status:, body:, json:) + if status >= 400 + error = JSON.parse(body)['error'] - request['Content-Type'] = 'application/json' - request['User-Agent'] = "EasyPost/v2 RubyClient/#{VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}" - if api_key ||= @api_key - request['Authorization'] = "Basic #{Base64.strict_encode64("#{api_key}:")}" + raise EasyPost::Error.new( + error['message'], + status, + error['code'], + error['errors'], + body, + ) end - response = client.request(request) - - if (400..599).include? response.code.to_i - error = JSON.parse(response.body)['error'] - raise EasyPost::Error.new(error['message'], response.code.to_i, error['code'], error['errors'], response.body) - end - - if response['Content-Type'].include? 'application/json' - JSON.parse(response.body) - else - response.body - end + json ? JSON.parse(body) : body rescue JSON::ParserError - raise "Invalid response object from API, unable to decode.\n#{response.body}" + raise "Invalid response object from API, unable to decode.\n#{body}" end end