class EasyPost::Client

Constants

SERVICE_CLASSES

Attributes

api_base[R]
open_timeout[R]
read_timeout[R]

Public Class Methods

new(api_key:, read_timeout: 60, open_timeout: 30, api_base: 'https://api.easypost.com', custom_client_exec: nil) click to toggle source

Initialize a new Client object @param api_key [String] the API key to be used for requests @param read_timeout [Integer] (60) the number of seconds to wait for a response before timing out @param open_timeout [Integer] (30) the number of seconds to wait for the connection to open before timing out @param api_base [String] (‘api.easypost.com’) the base URL for the API @param custom_client_exec [Proc] (nil) a custom client execution block to be used for requests instead of the default HTTP client (function signature: method, uri, headers, open_timeout, read_timeout, body = nil) @return [EasyPost::Client] the client object

# File lib/easypost/client.rb, line 19
def initialize(api_key:, read_timeout: 60, open_timeout: 30, api_base: 'https://api.easypost.com',
               custom_client_exec: nil)
  raise EasyPost::Errors::MissingParameterError.new('api_key') if api_key.nil?

  @api_key = api_key
  @api_base = api_base
  @api_version = 'v2'
  @read_timeout = read_timeout
  @open_timeout = open_timeout
  @lib_version = File.open(File.expand_path('../../VERSION', __dir__)).read.strip

  # Make an HTTP client once, reuse it for all requests made by this client
  # Configuration is immutable, so this is safe
  @http_client = EasyPost::HttpClient.new(api_base, http_config, custom_client_exec)
end

Public Instance Methods

make_request( method, endpoint, body = nil, api_version = EasyPost::InternalUtilities::Constants::API_VERSION ) click to toggle source

Make an HTTP request

@param method [Symbol] the HTTP Verb (get, method, put, post, etc.) @param endpoint [String] URI path of the resource @param body [Object] (nil) object to be dumped to JSON @param api_version [String] the version of API to hit @raise [EasyPost::Error] if the response has a non-2xx status code @return [Hash] JSON object parsed from the response body

# File lib/easypost/client.rb, line 79
def make_request(
  method,
  endpoint,
  body = nil,
  api_version = EasyPost::InternalUtilities::Constants::API_VERSION
)
  response = @http_client.request(method, endpoint, nil, body, api_version)

  potential_error = EasyPost::Errors::ApiError.handle_api_error(response)
  raise potential_error unless potential_error.nil?

  EasyPost::InternalUtilities::Json.parse_json(response.body)
end
subscribe_request_hook(name = SecureRandom.hex.to_sym, &block) click to toggle source

Subscribe a request hook

@param name [Symbol] the name of the hook. Defaults ot a ranom hexadecimal-based symbol @param block [Block] a code block that will be executed before a request is made @return [Symbol] the name of the request hook

# File lib/easypost/client.rb, line 98
def subscribe_request_hook(name = SecureRandom.hex.to_sym, &block)
  EasyPost::Hooks.subscribe(:request, name, block)
end
subscribe_response_hook(name = SecureRandom.hex.to_sym, &block) click to toggle source

Subscribe a response hook

@param name [Symbol] the name of the hook. Defaults ot a ranom hexadecimal-based symbol @param block [Block] a code block that will be executed upon receiving the response from a request @return [Symbol] the name of the response hook

# File lib/easypost/client.rb, line 122
def subscribe_response_hook(name = SecureRandom.hex.to_sym, &block)
  EasyPost::Hooks.subscribe(:response, name, block)
end
unsubscribe_all_request_hooks() click to toggle source

Unsubscribe all request hooks

@return [Hash] a hash containing all request hook subscriptions

# File lib/easypost/client.rb, line 113
def unsubscribe_all_request_hooks
  EasyPost::Hooks.unsubscribe_all(:request)
end
unsubscribe_all_response_hooks() click to toggle source

Unsubscribe all response hooks

@return [Hash] a hash containing all response hook subscriptions

# File lib/easypost/client.rb, line 137
def unsubscribe_all_response_hooks
  EasyPost::Hooks.unsubscribe_all(:response)
end
unsubscribe_request_hook(name) click to toggle source

Unsubscribe a request hook

@param name [Symbol] the name of the hook @return [Block] the hook code block

# File lib/easypost/client.rb, line 106
def unsubscribe_request_hook(name)
  EasyPost::Hooks.unsubscribe(:request, name)
end
unsubscribe_response_hook(name) click to toggle source

Unsubscribe a response hook

@param name [Symbol] the name of the hook @return [Block] the hook code block

# File lib/easypost/client.rb, line 130
def unsubscribe_response_hook(name)
  EasyPost::Hooks.unsubscribe(:response, name)
end