Class: EasyPost::HttpClient

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

Instance Method Summary collapse

Constructor Details

#initialize(base_url, config, custom_client_exec = nil) ⇒ HttpClient

Returns a new instance of HttpClient.



4
5
6
7
8
# File 'lib/easypost/http_client.rb', line 4

def initialize(base_url, config, custom_client_exec = nil)
  @base_url = base_url
  @config = config
  @custom_client_exec = custom_client_exec
end

Instance Method Details

#default_request_execute(method, uri, headers, open_timeout, read_timeout, body = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/easypost/http_client.rb', line 36

def default_request_execute(method, uri, headers, open_timeout, read_timeout, body = nil)
  # Create the request, set the headers and body if necessary.
  request = Net::HTTP.const_get(method.capitalize).new(uri)
  headers.each { |k, v| request[k] = v }
  request.body = body if body

  begin
    # Attempt to make the request and return the response.
    Net::HTTP.start(
      uri.host,
      uri.port,
      use_ssl: true,
      read_timeout: read_timeout,
      open_timeout: open_timeout,
    ) do |http|
      http.request(request)
    end
  rescue Net::ReadTimeout, Net::OpenTimeout, Errno::EHOSTUNREACH => e
    # Raise a timeout error if the request times out.
    raise EasyPost::Errors::TimeoutError.new(e.message)
  rescue OpenSSL::SSL::SSLError => e
    # Raise an SSL error if the request fails due to an SSL error.
    raise EasyPost::Errors::SslError.new(e.message)
  rescue StandardError => e
    # Raise an unknown HTTP error if anything else causes the request to fail to complete
    # (this is different from processing 4xx/5xx errors from the API)
    raise EasyPost::Errors::UnknownApiError.new(e.message)
  end
end

#request(method, path, headers = nil, body = nil, api_version = EasyPost::InternalUtilities::Constants::API_VERSION) ⇒ Object

Execute an HTTP request to the API.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/easypost/http_client.rb', line 11

def request(
  method,
  path,
  headers = nil,
  body = nil,
  api_version = EasyPost::InternalUtilities::Constants::API_VERSION
)
  # Remove leading slash from path.
  path = path[1..] if path[0] == '/'

  uri = URI.parse("#{@base_url}/#{api_version}/#{path}")
  headers = @config[:headers].merge(headers || {})
  body = JSON.dump(EasyPost::InternalUtilities.objects_to_ids(body)) if body
  open_timeout = @config[:open_timeout]
  read_timeout = @config[:read_timeout]

  # Execute the request, return the response.

  if @custom_client_exec
    @custom_client_exec.call(method, uri, headers, open_timeout, read_timeout, body)
  else
    default_request_execute(method, uri, headers, open_timeout, read_timeout, body)
  end
end