Class: AhoSdk::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/aho_sdk/http_client.rb

Overview

HTTP client with retry logic, credential redaction, and error handling

Constant Summary collapse

DEFAULT_TIMEOUT =

seconds

30
DEFAULT_MAX_RETRIES =
3
DEFAULT_RETRY_DELAY =

seconds

1.0
IDEMPOTENT_METHODS =

Only retry idempotent methods to prevent duplicate operations

%i[get delete put head options].freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: "https://api.aho.com", timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES, retry_delay: DEFAULT_RETRY_DELAY, logger: nil) ⇒ HttpClient

Returns a new instance of HttpClient.

Parameters:

  • api_key (String, nil)

    API key for authentication (nil for public endpoints)

  • base_url (String) (defaults to: "https://api.aho.com")

    Base URL for the API

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    Request timeout in seconds

  • max_retries (Integer) (defaults to: DEFAULT_MAX_RETRIES)

    Maximum number of retries on rate limit (default: 3)

  • retry_delay (Float) (defaults to: DEFAULT_RETRY_DELAY)

    Base delay in seconds for exponential backoff (default: 1.0)

  • logger (Logger, nil) (defaults to: nil)

    Optional logger for debugging



26
27
28
29
30
31
32
33
# File 'lib/aho_sdk/http_client.rb', line 26

def initialize(api_key:, base_url: "https://api.aho.com", timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES, retry_delay: DEFAULT_RETRY_DELAY, logger: nil)
  @api_key = api_key
  @base_url = base_url.chomp("/")
  @timeout = timeout
  @max_retries = max_retries
  @retry_delay = retry_delay
  @logger = logger
end

Instance Method Details

#delete(path, params: {}, accept: "application/json") ⇒ Object



51
52
53
# File 'lib/aho_sdk/http_client.rb', line 51

def delete(path, params: {}, accept: "application/json")
  request(:delete, path, params: params, accept: accept)
end

#get(path, params: {}, accept: "application/json") ⇒ Object



35
36
37
# File 'lib/aho_sdk/http_client.rb', line 35

def get(path, params: {}, accept: "application/json")
  request(:get, path, params: params, accept: accept)
end

#head(path, params: {}) ⇒ Object



55
56
57
# File 'lib/aho_sdk/http_client.rb', line 55

def head(path, params: {})
  request(:head, path, params: params, accept: "*/*")
end

#options(path, params: {}) ⇒ Object



59
60
61
# File 'lib/aho_sdk/http_client.rb', line 59

def options(path, params: {})
  request(:options, path, params: params, accept: "*/*")
end

#patch(path, body: nil, params: {}, accept: "application/json", idempotency_key: nil) ⇒ Object



43
44
45
# File 'lib/aho_sdk/http_client.rb', line 43

def patch(path, body: nil, params: {}, accept: "application/json", idempotency_key: nil)
  request(:patch, path, body: body, params: params, accept: accept, idempotency_key: idempotency_key)
end

#patch_multipart(path, files: {}, fields: {}, params: {}, idempotency_key: nil) ⇒ Object

PATCH with multipart/form-data for file uploads



74
75
76
# File 'lib/aho_sdk/http_client.rb', line 74

def patch_multipart(path, files: {}, fields: {}, params: {}, idempotency_key: nil)
  multipart_request(:patch, path, files: files, fields: fields, params: params, idempotency_key: idempotency_key)
end

#post(path, body: nil, params: {}, accept: "application/json", idempotency_key: nil) ⇒ Object



39
40
41
# File 'lib/aho_sdk/http_client.rb', line 39

def post(path, body: nil, params: {}, accept: "application/json", idempotency_key: nil)
  request(:post, path, body: body, params: params, accept: accept, idempotency_key: idempotency_key)
end

#post_multipart(path, files: {}, fields: {}, params: {}, idempotency_key: nil) ⇒ Object

POST with multipart/form-data for file uploads

Parameters:

  • path (String)

    API path

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

    File parameters { param_name: File or String path }

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

    Additional form fields

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

    Query parameters

  • idempotency_key (String, nil) (defaults to: nil)

    Optional idempotency key



69
70
71
# File 'lib/aho_sdk/http_client.rb', line 69

def post_multipart(path, files: {}, fields: {}, params: {}, idempotency_key: nil)
  multipart_request(:post, path, files: files, fields: fields, params: params, idempotency_key: idempotency_key)
end

#put(path, body: nil, params: {}, accept: "application/json", idempotency_key: nil) ⇒ Object



47
48
49
# File 'lib/aho_sdk/http_client.rb', line 47

def put(path, body: nil, params: {}, accept: "application/json", idempotency_key: nil)
  request(:put, path, body: body, params: params, accept: accept, idempotency_key: idempotency_key)
end

#put_multipart(path, files: {}, fields: {}, params: {}, idempotency_key: nil) ⇒ Object

PUT with multipart/form-data for file uploads



79
80
81
# File 'lib/aho_sdk/http_client.rb', line 79

def put_multipart(path, files: {}, fields: {}, params: {}, idempotency_key: nil)
  multipart_request(:put, path, files: files, fields: fields, params: params, idempotency_key: idempotency_key)
end