Class: Apertur::HttpClient
- Inherits:
-
Object
- Object
- Apertur::HttpClient
- Defined in:
- lib/apertur/http_client.rb
Overview
Low-level HTTP wrapper around Net::HTTP for communicating with the Apertur API.
Handles JSON serialization, Bearer token authentication, multipart uploads, and error mapping.
Instance Method Summary collapse
-
#initialize(base_url, token) ⇒ HttpClient
constructor
A new instance of HttpClient.
-
#request(method, path, body: nil, query: nil, headers: {}) ⇒ Hash, ...
Perform an API request and return the parsed JSON response.
-
#request_multipart(path, file_data, filename:, mime_type:, fields: {}, headers: {}) ⇒ Hash, ...
Perform a multipart/form-data upload request.
-
#request_raw(method, path, query: nil) ⇒ String
Perform an API request and return the raw response body as a binary String.
Constructor Details
#initialize(base_url, token) ⇒ HttpClient
Returns a new instance of HttpClient.
16 17 18 19 |
# File 'lib/apertur/http_client.rb', line 16 def initialize(base_url, token) @base_url = base_url.chomp("/") @token = token end |
Instance Method Details
#request(method, path, body: nil, query: nil, headers: {}) ⇒ Hash, ...
Perform an API request and return the parsed JSON response.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/apertur/http_client.rb', line 30 def request(method, path, body: nil, query: nil, headers: {}) uri = build_uri(path, query) req = build_request(method, uri, headers) if body req["Content-Type"] = "application/json" req.body = body.is_a?(String) ? body : JSON.generate(body) end response = execute(uri, req) handle_response(response) end |
#request_multipart(path, file_data, filename:, mime_type:, fields: {}, headers: {}) ⇒ Hash, ...
Perform a multipart/form-data upload request.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/apertur/http_client.rb', line 69 def request_multipart(path, file_data, filename:, mime_type:, fields: {}, headers: {}) uri = build_uri(path) boundary = "AperturRubySDK#{SecureRandom.hex(16)}" body = build_multipart_body(boundary, file_data, filename, mime_type, fields) req = build_request(:post, uri, headers) req["Content-Type"] = "multipart/form-data; boundary=#{boundary}" req.body = body response = execute(uri, req) handle_response(response) end |
#request_raw(method, path, query: nil) ⇒ String
Perform an API request and return the raw response body as a binary String.
50 51 52 53 54 55 56 57 |
# File 'lib/apertur/http_client.rb', line 50 def request_raw(method, path, query: nil) uri = build_uri(path, query) req = build_request(method, uri) response = execute(uri, req) handle_error(response) unless response.is_a?(Net::HTTPSuccess) response.body end |