class EasyPost::Client
Constants
- SERVICE_CLASSES
Attributes
Public Class Methods
Source
# 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
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
Public Instance Methods
Source
# File lib/easypost/client.rb, line 81 def make_request( method, endpoint, params = nil, api_version = EasyPost::InternalUtilities::Constants::API_VERSION ) response = @http_client.request(method, endpoint, nil, params, 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
Make an HTTP request
@param method [Symbol] the HTTP Verb (get, method, put, post, etc.) @param endpoint [String] URI path of the resource @param params [Object] (nil) object to be used as the request parameters @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
Source
# File lib/easypost/client.rb, line 100 def subscribe_request_hook(name = SecureRandom.hex.to_sym, &block) EasyPost::Hooks.subscribe(:request, name, block) end
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
Source
# File lib/easypost/client.rb, line 124 def subscribe_response_hook(name = SecureRandom.hex.to_sym, &block) EasyPost::Hooks.subscribe(:response, name, block) end
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
Source
# File lib/easypost/client.rb, line 115 def unsubscribe_all_request_hooks EasyPost::Hooks.unsubscribe_all(:request) end
Unsubscribe all request hooks
@return [Hash] a hash containing all request hook subscriptions
Source
# File lib/easypost/client.rb, line 139 def unsubscribe_all_response_hooks EasyPost::Hooks.unsubscribe_all(:response) end
Unsubscribe all response hooks
@return [Hash] a hash containing all response hook subscriptions
Source
# File lib/easypost/client.rb, line 108 def unsubscribe_request_hook(name) EasyPost::Hooks.unsubscribe(:request, name) end
Unsubscribe a request hook
@param name [Symbol] the name of the hook @return [Block] the hook code block
Source
# File lib/easypost/client.rb, line 132 def unsubscribe_response_hook(name) EasyPost::Hooks.unsubscribe(:response, name) end
Unsubscribe a response hook
@param name [Symbol] the name of the hook @return [Block] the hook code block