Class: Tripwire::Server::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tripwire/server/client.rb

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.tripwirejs.com".freeze
DEFAULT_TIMEOUT =
30
SDK_CLIENT_HEADER =
"tripwire-server-ruby/0.1.0".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret_key: ENV["TRIPWIRE_SECRET_KEY"], base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, user_agent: nil, transport: nil) ⇒ Client

Returns a new instance of Client.

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tripwire/server/client.rb', line 15

def initialize(secret_key: ENV["TRIPWIRE_SECRET_KEY"], base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, user_agent: nil, transport: nil)
  raise ConfigurationError, "Missing Tripwire secret key. Pass secret_key explicitly or set TRIPWIRE_SECRET_KEY." if secret_key.nil? || secret_key.empty?

  @secret_key = secret_key
  @base_url = base_url
  @timeout = timeout
  @user_agent = user_agent
  @transport = transport

  @sessions = SessionsResource.new(self)
  @fingerprints = FingerprintsResource.new(self)
  @teams = TeamsResource.new(self)
end

Instance Attribute Details

#fingerprintsObject (readonly)

Returns the value of attribute fingerprints.



13
14
15
# File 'lib/tripwire/server/client.rb', line 13

def fingerprints
  @fingerprints
end

#sessionsObject (readonly)

Returns the value of attribute sessions.



13
14
15
# File 'lib/tripwire/server/client.rb', line 13

def sessions
  @sessions
end

#teamsObject (readonly)

Returns the value of attribute teams.



13
14
15
# File 'lib/tripwire/server/client.rb', line 13

def teams
  @teams
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



13
14
15
# File 'lib/tripwire/server/client.rb', line 13

def timeout
  @timeout
end

Instance Method Details

#request_json(method, path, query: {}, body: nil, expect_content: true) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tripwire/server/client.rb', line 29

def request_json(method, path, query: {}, body: nil, expect_content: true)
  url = build_url(path, query)
  headers = {
    "Authorization" => "Bearer #{@secret_key}",
    "Accept" => "application/json",
    "X-Tripwire-Client" => SDK_CLIENT_HEADER
  }
  headers["User-Agent"] = @user_agent if @user_agent
  headers["Content-Type"] = "application/json" if body

  status, response_headers, response_body =
    if @transport
      @transport.call(method: method, url: url.to_s, headers: headers, body: body.nil? ? nil : JSON.dump(body))
    else
      perform_http_request(method, url, headers, body)
    end

  request_id = response_headers["x-request-id"] || response_headers["X-Request-Id"]

  if status >= 400
    payload = parse_json(response_body)
    if payload[:error].is_a?(Hash)
      error = payload[:error]
      details = error[:details].is_a?(Hash) ? error[:details] : {}
      raise ApiError.new(
        status: status,
        code: error[:code] || "request.failed",
        message: error[:message] || response_body.to_s,
        request_id: request_id || error[:requestId],
        field_errors: details[:fieldErrors] || [],
        docs_url: error[:docsUrl],
        body: payload
      )
    end

    raise ApiError.new(status: status, code: "request.failed", message: response_body.to_s, request_id: request_id, body: payload)
  end

  return {} unless expect_content
  return {} if status == 204 || response_body.nil? || response_body.empty?

  parse_json(response_body)
end