Class: Nahook::Client
- Inherits:
-
Object
- Object
- Nahook::Client
- Defined in:
- lib/nahook/client.rb
Overview
Client for sending webhook payloads through the Nahook ingestion API.
Supports sending to specific endpoints, fan-out by event type, and batch operations. Includes configurable retry with exponential backoff.
Instance Method Summary collapse
-
#initialize(api_key, base_url: nil, timeout_ms: HttpClient::DEFAULT_TIMEOUT_MS, retries: 0, adapter: nil, connection: nil) ⇒ Client
constructor
A new instance of Client.
-
#send(endpoint_id, payload:, idempotency_key: nil) ⇒ Hash
Send a payload to a specific endpoint.
-
#send_batch(items) ⇒ Hash
Batch send to multiple specific endpoints (max 20 items).
-
#trigger(event_type, payload:, metadata: nil) ⇒ Hash
Fan-out a payload by event type to all subscribed endpoints.
-
#trigger_batch(items) ⇒ Hash
Batch fan-out by event types (max 20 items).
Constructor Details
#initialize(api_key, base_url: nil, timeout_ms: HttpClient::DEFAULT_TIMEOUT_MS, retries: 0, adapter: nil, connection: nil) ⇒ Client
Returns a new instance of Client.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/nahook/client.rb', line 35 def initialize(api_key, base_url: nil, timeout_ms: HttpClient::DEFAULT_TIMEOUT_MS, retries: 0, adapter: nil, connection: nil) unless api_key.start_with?("nhk_") raise ArgumentError, "Invalid API key: must start with 'nhk_'" end resolved_url = base_url || HttpClient.resolve_base_url(api_key) http_kwargs = { token: api_key, base_url: resolved_url, timeout_ms: timeout_ms, retries: retries, } http_kwargs[:adapter] = adapter if adapter http_kwargs[:connection] = connection if connection @http = HttpClient.new(**http_kwargs) end |
Instance Method Details
#send(endpoint_id, payload:, idempotency_key: nil) ⇒ Hash
Send a payload to a specific endpoint.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/nahook/client.rb', line 64 def send(endpoint_id, payload:, idempotency_key: nil) key = idempotency_key || SecureRandom.uuid @http.request( method: :post, path: "/api/ingest/#{CGI.escape(endpoint_id)}", body: { "payload" => payload, "idempotencyKey" => key } ) end |
#send_batch(items) ⇒ Hash
Batch send to multiple specific endpoints (max 20 items).
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/nahook/client.rb', line 106 def send_batch(items) mapped = items.map do |item| entry = { "endpointId" => item[:endpoint_id] || item["endpoint_id"], "payload" => item[:payload] || item["payload"] } key = item[:idempotency_key] || item["idempotency_key"] entry["idempotencyKey"] = key if key entry end @http.request( method: :post, path: "/api/ingest/batch", body: { "items" => mapped } ) end |
#trigger(event_type, payload:, metadata: nil) ⇒ Hash
Fan-out a payload by event type to all subscribed endpoints.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/nahook/client.rb', line 84 def trigger(event_type, payload:, metadata: nil) body = { "payload" => payload } body["metadata"] = if @http.request( method: :post, path: "/api/ingest/event/#{CGI.escape(event_type)}", body: body ) end |
#trigger_batch(items) ⇒ Hash
Batch fan-out by event types (max 20 items).
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/nahook/client.rb', line 135 def trigger_batch(items) mapped = items.map do |item| entry = { "eventType" => item[:event_type] || item["event_type"], "payload" => item[:payload] || item["payload"] } = item[:metadata] || item["metadata"] entry["metadata"] = if entry end @http.request( method: :post, path: "/api/ingest/event/batch", body: { "items" => mapped } ) end |