Class: Rev::HttpClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/rev-api/http_client.rb

Overview

HTTP client handling authentication and HTTP requests at the low level for the Api class. Not indended to be used directly - clients should be using the Api class instead.

Constant Summary collapse

USER_AGENT =
"RevOfficialRubySDK/#{VERSION}"

Instance Method Summary collapse

Constructor Details

#initialize(client_api_key, user_api_key, host) ⇒ HttpClient

Create a new HttpClient, connecting to given host, and using the given Client and User API Keys.

Parameters:

  • client_api_key (String)

    the client API key to use for authenticating

  • user_api_key (String)

    the user API key to use for authenticating

  • host (String)

    the host to send requests to. Should be one of Rev::Api::PRODCUTION_HOST or Rev::Api::SANDBOX_HOST



16
17
18
19
20
21
22
23
24
25
# File 'lib/rev-api/http_client.rb', line 16

def initialize(client_api_key, user_api_key, host)
  endpoint_uri = "https://#{host}/api/v1"
  self.class.base_uri(endpoint_uri)

  auth_string = "Rev #{client_api_key}:#{user_api_key}"
  @default_headers = {
    'Authorization' => auth_string,
    'User-Agent' => USER_AGENT # to track usage of SDK
  }
end

Instance Method Details

#get(operation, headers = {}) ⇒ HTTParty::Response

Performs HTTP GET of JSON data.

Parameters:

  • operation (String)

    URL suffix describing specific operation, like ‘/orders’

  • headers (Hash) (defaults to: {})

    hash of headers to use for the request

Returns:

  • (HTTParty::Response)

    response



32
33
34
35
# File 'lib/rev-api/http_client.rb', line 32

def get(operation, headers = {})
  headers = @default_headers.merge(headers)
  self.class.get(operation, :headers => headers)
end

#get_binary(operation, headers = {}) {|resp| ... } ⇒ Net::HTTP::Response

Performs HTTP GET of binary data. Note, unlike post, this returns a Net::HTTP::Response, not HTTParty::Response.

If this method is passed a block, will pass response to that block. in that case the response is not yet read into memory, so the block can read it progressively. otherwise, returns the response.

Parameters:

  • operation (String)

    URL suffix describing specific operation, like ‘/orders’

  • headers (Hash) (defaults to: {})

    hash of headers to use for the request

Yield Parameters:

  • resp (Net::HTTP::Response)

    the response, ready to be read

Returns:

  • (Net::HTTP::Response)

    response



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rev-api/http_client.rb', line 47

def get_binary(operation, headers = {}, &block)
  uri = URI.parse("#{self.class.base_uri}#{operation}")
  headers = @default_headers.merge(headers)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  get = Net::HTTP::Get.new(uri.request_uri, headers)
  if block_given?
    http.request(get) do |resp|
      yield resp
    end
  else
    http.request(get)
  end
end

#post(operation, data = {}, headers = {}) ⇒ HTTParty::Response

Performs HTTP POST of JSON data.

Parameters:

  • operation (String)

    URL suffix describing specific operation

  • data (Hash) (defaults to: {})

    hash of keys/values to post in request body

  • headers (Hash) (defaults to: {})

    hash of headers to use for the request

Returns:

  • (HTTParty::Response)

    response



70
71
72
73
# File 'lib/rev-api/http_client.rb', line 70

def post(operation, data = {}, headers = {})
  headers = @default_headers.merge(headers)
  self.class.post(operation, :headers => headers, :body => data)
end

#post_binary(operation, file, headers = {}) ⇒ Net::HTTP::Response

Performs HTTP POST of binary data. Note, unlike post, this returns a Net::HTTP::Response, not HTTParty::Response.

Parameters:

  • operation (String)

    URL suffix describing specific operation

  • file (File)

    file-like object containing the data to post

  • headers (Hash) (defaults to: {})

    hash of headers to use for the request

Returns:

  • (Net::HTTP::Response)

    response



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rev-api/http_client.rb', line 83

def post_binary(operation, file, headers = {})
  uri = URI.parse("#{self.class.base_uri}#{operation}")
  headers = @default_headers.merge(headers)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  post = Net::HTTP::Post.new(uri.request_uri, headers)
  post["Content-Length"] = file.stat.size.to_s
  post.body_stream = file

  response = http.request(post)
end