Class: EasyPost::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/easypost/client.rb

Constant Summary collapse

SERVICE_CLASSES =
[
  EasyPost::Services::Address,
  EasyPost::Services::ApiKey,
  EasyPost::Services::Batch,
  EasyPost::Services::BetaRate,
  EasyPost::Services::BetaReferralCustomer,
  EasyPost::Services::Billing,
  EasyPost::Services::CarrierAccount,
  EasyPost::Services::,
  EasyPost::Services::CarrierType,
  EasyPost::Services::Claim,
  EasyPost::Services::CustomerPortal,
  EasyPost::Services::CustomsInfo,
  EasyPost::Services::CustomsItem,
  EasyPost::Services::Embeddable,
  EasyPost::Services::EndShipper,
  EasyPost::Services::Event,
  EasyPost::Services::FedexRegistration,
  EasyPost::Services::Insurance,
  EasyPost::Services::Luma,
  EasyPost::Services::Order,
  EasyPost::Services::Parcel,
  EasyPost::Services::Pickup,
  EasyPost::Services::Rate,
  EasyPost::Services::ReferralCustomer,
  EasyPost::Services::Refund,
  EasyPost::Services::Report,
  EasyPost::Services::ScanForm,
  EasyPost::Services::Shipment,
  EasyPost::Services::SmartRate,
  EasyPost::Services::Tracker,
  EasyPost::Services::User,
  EasyPost::Services::Webhook,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, read_timeout: 60, open_timeout: 30, api_base: 'https://api.easypost.com', custom_client_exec: nil) ⇒ EasyPost::Client

Initialize a new Client object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

Instance Attribute Details

#api_baseObject (readonly)

Returns the value of attribute api_base.



10
11
12
# File 'lib/easypost/client.rb', line 10

def api_base
  @api_base
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



10
11
12
# File 'lib/easypost/client.rb', line 10

def open_timeout
  @open_timeout
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



10
11
12
# File 'lib/easypost/client.rb', line 10

def read_timeout
  @read_timeout
end

Instance Method Details

#make_api_call(method, endpoint, params = nil) ⇒ EasyPost::Models::EasyPostObject

Make an API call to the EasyPost API

This public, generic interface is useful for making arbitrary API calls to the EasyPost API that are not yet supported by the client library’s services. When possible, the service for your use case should be used instead as it provides a more convenient and higher-level interface depending on the endpoint.



157
158
159
160
161
# File 'lib/easypost/client.rb', line 157

def make_api_call(method, endpoint, params = nil)
  response = make_request(method, endpoint, params)

  EasyPost::InternalUtilities::Json.convert_json_to_object(response)
end

#make_request(method, endpoint, params = nil, api_version = EasyPost::InternalUtilities::Constants::API_VERSION) ⇒ Hash

Make an HTTP request

Raises:



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/easypost/client.rb', line 85

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

#subscribe_request_hook(name = SecureRandom.hex.to_sym, &block) ⇒ Symbol

Subscribe a request hook



104
105
106
# File 'lib/easypost/client.rb', line 104

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) ⇒ Symbol

Subscribe a response hook



128
129
130
# File 'lib/easypost/client.rb', line 128

def subscribe_response_hook(name = SecureRandom.hex.to_sym, &block)
  EasyPost::Hooks.subscribe(:response, name, block)
end

#unsubscribe_all_request_hooksHash

Unsubscribe all request hooks



119
120
121
# File 'lib/easypost/client.rb', line 119

def unsubscribe_all_request_hooks
  EasyPost::Hooks.unsubscribe_all(:request)
end

#unsubscribe_all_response_hooksHash

Unsubscribe all response hooks



143
144
145
# File 'lib/easypost/client.rb', line 143

def unsubscribe_all_response_hooks
  EasyPost::Hooks.unsubscribe_all(:response)
end

#unsubscribe_request_hook(name) ⇒ Block

Unsubscribe a request hook



112
113
114
# File 'lib/easypost/client.rb', line 112

def unsubscribe_request_hook(name)
  EasyPost::Hooks.unsubscribe(:request, name)
end

#unsubscribe_response_hook(name) ⇒ Block

Unsubscribe a response hook



136
137
138
# File 'lib/easypost/client.rb', line 136

def unsubscribe_response_hook(name)
  EasyPost::Hooks.unsubscribe(:response, name)
end