Class: Zanshin::SDK::Request::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/zanshin/request/zanshin_request.rb

Overview

Zanshin SDK request HTTPClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_url, user_agent, proxy_url) ⇒ HTTPClient

Initialize a new HTTP connection to the Zanshin API

Parameters:

  • api_key (String)

    API key to use

  • api_url (String)

    URL of the Zanshin API

  • user_agent (String)

    User agent to use in requests performed

  • proxy_url (String)

    Optional URL indicating which proxy server to use


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/zanshin/request/zanshin_request.rb', line 21

def initialize(api_key, api_url, user_agent, proxy_url = nil)
  @headers = { 'Authorization' => "Bearer #{api_key}",
               'User-Agent' => user_agent,
               'Content-Type' => 'application/json' }
  # HACK: Needs to figure out why Net::HTTP is not automatically decoded `gzip` and `deflate` encoding
  # 'Accept-Encoding' => 'gzip, deflate'

  uri = URI.parse(api_url)
  proxy = URI.parse(proxy_url || '')

  @http_client = Net::HTTP.new(uri.host, uri.port, proxy.host, proxy.port, proxy.user, proxy.password)
  @http_client.use_ssl = true
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.


13
14
15
# File 'lib/zanshin/request/zanshin_request.rb', line 13

def headers
  @headers
end

#http_clientObject

Returns the value of attribute http_client.


13
14
15
# File 'lib/zanshin/request/zanshin_request.rb', line 13

def http_client
  @http_client
end

Instance Method Details

#initialize(api_key, api_url, user_agent, proxy_url) ⇒ Object

Request to Zanshin

Parameters:

  • method ('GET', 'POST', 'PUT', 'DELETE')

    HTTP method to pass along to `:HTTPClient`

  • path (String)

    API path to access

  • body (Object)

    request body to pass along to `:HTTPClient`

Raises:


40
41
42
43
44
45
46
47
# File 'lib/zanshin/request/zanshin_request.rb', line 40

def request(method, path, body = nil)
  body = body.to_json if body
  response = @http_client.send_request(method, path, body, @headers)
  result = JSON.parse(response.body)
  raise ZanshinError.new(response.code, result['message'] || result['errorName']) if response.code.to_i >= 400

  result
end