Class: Datadog::AIGuard::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/ai_guard/api_client.rb

Overview

API Client for AI Guard API. Uses net/http to perform request. Raises on client and server errors.

Constant Summary collapse

DEFAULT_SITE =
"app.datadoghq.com"
DEFAULT_PATH =
"/api/v2/ai-guard"

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, api_key:, application_key:, timeout:) ⇒ APIClient

Returns a new instance of APIClient.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/datadog/ai_guard/api_client.rb', line 15

def initialize(endpoint:, api_key:, application_key:, timeout:)
  @timeout = timeout

  @endpoint_uri = if endpoint
    URI(endpoint) #: URI::HTTP
  else
    URI::HTTPS.build(
      host: Datadog.configuration.site || DEFAULT_SITE,
      path: DEFAULT_PATH
    )
  end

  @headers = {
    "DD-API-KEY": api_key.to_s,
    "DD-APPLICATION-KEY": application_key.to_s,
    "DD-AI-GUARD-VERSION": Datadog::VERSION::STRING,
    "DD-AI-GUARD-SOURCE": "SDK",
    "DD-AI-GUARD-LANGUAGE": "ruby",
    "content-type": "application/json"
  }.freeze
end

Instance Method Details

#post(path, body:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/datadog/ai_guard/api_client.rb', line 37

def post(path, body:)
  Net::HTTP.start(@endpoint_uri.host.to_s, @endpoint_uri.port, use_ssl: use_ssl?, read_timeout: @timeout) do |http|
    request = Net::HTTP::Post.new(@endpoint_uri.request_uri + path, @headers)
    request.body = body.to_json

    response = http.request(request)
    raise_on_http_error!(response)

    parse_response_body(response.body)
  end
rescue Net::ReadTimeout
  raise AIGuardClientError, "Request to AI Guard timed out"
end