Class: Zanshin::SDK::Request::HTTPClient
- Inherits:
-
Object
- Object
- Zanshin::SDK::Request::HTTPClient
- Defined in:
- lib/zanshin/request/zanshin_request.rb
Overview
Zanshin SDK request HTTPClient
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#http_client ⇒ Object
Returns the value of attribute http_client.
Instance Method Summary collapse
-
#initialize(api_key, api_url, user_agent, proxy_url) ⇒ HTTPClient
constructor
Initialize a new HTTP connection to the Zanshin API.
-
#initialize(api_key, api_url, user_agent, proxy_url) ⇒ Object
Request to Zanshin.
Constructor Details
#initialize(api_key, api_url, user_agent, proxy_url) ⇒ HTTPClient
Initialize a new HTTP connection to the Zanshin API
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
#headers ⇒ Object
Returns the value of attribute headers.
13 14 15 |
# File 'lib/zanshin/request/zanshin_request.rb', line 13 def headers @headers end |
#http_client ⇒ Object
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
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 |