Class: Tripwire::Server::Client
- Inherits:
-
Object
- Object
- Tripwire::Server::Client
- 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
-
#fingerprints ⇒ Object
readonly
Returns the value of attribute fingerprints.
-
#gate ⇒ Object
readonly
Returns the value of attribute gate.
-
#organizations ⇒ Object
readonly
Returns the value of attribute organizations.
-
#sessions ⇒ Object
readonly
Returns the value of attribute sessions.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#webhooks ⇒ Object
readonly
Returns the value of attribute webhooks.
Instance Method Summary collapse
-
#initialize(secret_key: ENV["TRIPWIRE_SECRET_KEY"], base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, user_agent: nil, transport: nil) ⇒ Client
constructor
A new instance of Client.
- #request_json(method, path, query: {}, body: nil, expect_content: true, auth: { kind: :secret }) ⇒ Object
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.
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) @secret_key = secret_key @base_url = base_url @timeout = timeout @user_agent = user_agent @transport = transport @sessions = SessionsResource.new(self) @fingerprints = FingerprintsResource.new(self) @organizations = OrganizationsResource.new(self) @gate = GateResource.new(self) @webhooks = WebhooksResource.new(self) end |
Instance Attribute Details
#fingerprints ⇒ Object (readonly)
Returns the value of attribute fingerprints.
13 14 15 |
# File 'lib/tripwire/server/client.rb', line 13 def fingerprints @fingerprints end |
#gate ⇒ Object (readonly)
Returns the value of attribute gate.
13 14 15 |
# File 'lib/tripwire/server/client.rb', line 13 def gate @gate end |
#organizations ⇒ Object (readonly)
Returns the value of attribute organizations.
13 14 15 |
# File 'lib/tripwire/server/client.rb', line 13 def organizations @organizations end |
#sessions ⇒ Object (readonly)
Returns the value of attribute sessions.
13 14 15 |
# File 'lib/tripwire/server/client.rb', line 13 def sessions @sessions end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
13 14 15 |
# File 'lib/tripwire/server/client.rb', line 13 def timeout @timeout end |
#webhooks ⇒ Object (readonly)
Returns the value of attribute webhooks.
13 14 15 |
# File 'lib/tripwire/server/client.rb', line 13 def webhooks @webhooks end |
Instance Method Details
#request_json(method, path, query: {}, body: nil, expect_content: true, auth: { kind: :secret }) ⇒ 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, auth: { kind: :secret }) url = build_url(path, query) headers = { "Accept" => "application/json", "X-Tripwire-Client" => SDK_CLIENT_HEADER } headers["User-Agent"] = @user_agent if @user_agent headers["Content-Type"] = "application/json" if body apply_auth_headers(headers, auth) 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[:request_id], field_errors: details[:fields] || [], docs_url: error[:docs_url], 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 |