Class: Apertur::HttpClient

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(base_url, token) ⇒ HttpClient

Returns a new instance of HttpClient.

Parameters:

  • base_url (String)

    the API base URL (e.g. “api.aptr.ca”)

  • token (String)

    the Bearer token (API key or OAuth token)



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.

Parameters:

  • method (Symbol)

    HTTP method (:get, :post, :patch, :put, :delete)

  • path (String)

    the API path (e.g. “/api/v1/stats”)

  • body (Hash, nil) (defaults to: nil)

    request body to be serialized as JSON

  • query (Hash, nil) (defaults to: nil)

    query parameters

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

    additional request headers

Returns:

  • (Hash, Array, nil)

    parsed JSON response, or nil for 204

Raises:



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.

Parameters:

  • path (String)

    the API path

  • file_data (String)

    raw file bytes

  • filename (String)

    the filename to use in the multipart part

  • mime_type (String)

    the MIME type of the file

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

    additional form fields

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

    additional request headers

Returns:

  • (Hash, Array, nil)

    parsed JSON response

Raises:



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.

Parameters:

  • method (Symbol)

    HTTP method

  • path (String)

    the API path

  • query (Hash, nil) (defaults to: nil)

    query parameters

Returns:

  • (String)

    raw response body (binary)

Raises:



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