Class: Rev::HttpClient
- Inherits:
-
Object
- Object
- Rev::HttpClient
- 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
-
#get(operation, headers = {}) ⇒ HTTParty::Response
Performs HTTP GET of JSON data.
-
#get_binary(operation, headers = {}) {|resp| ... } ⇒ Net::HTTP::Response
Performs HTTP GET of binary data.
-
#initialize(client_api_key, user_api_key, host) ⇒ HttpClient
constructor
Create a new HttpClient, connecting to given host, and using the given Client and User API Keys.
-
#post(operation, data = {}, headers = {}) ⇒ HTTParty::Response
Performs HTTP POST of JSON data.
-
#post_binary(operation, file, headers = {}) ⇒ Net::HTTP::Response
Performs HTTP POST of binary data.
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.
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.
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.
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.
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.
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 |