Class: Restless::Client

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

Overview

The public entry point.

client = Restless::Client.new(ENV["RESTLESS_KEY"])
client.setup { |request| { api_key: client.mask(request.header("authorization")) } }
use client.rack

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, base_url: nil, api: nil, redact: nil, transport: nil) ⇒ Client

CONFIG-001..003, CONFIG-010..015.

redact extends the built-in denylists; it can never shrink or replace them (REDACT-014). Both the settings file and this option are additive on top of the defaults (REDACT-015).

transport is an internal test hook -- anything responding to call(url, headers, body) -> [status, body] -- and is not public API.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/restless/client.rb', line 27

def initialize(api_key = nil, base_url: nil, api: nil, redact: nil,
               transport: nil)
  resolved_key = Env.resolve_api_key(api_key)

  # CONFIG-002. Construction still succeeds and capture still runs; only
  # upload is disabled.
  if resolved_key.empty? && !Env.test_run?
    warn("[restless-sdk] no API key found -- set RESTLESS_KEY in your " \
         "environment or pass it to Restless::Client.new. Captured requests " \
         "will not be uploaded.")
  end

  # CONFIG-013/CONFIG-014 raise here, deliberately: guessing which API
  # entry to use would silently apply the wrong redaction list.
  entry = Settings.resolve_api(Settings.load, api)
  settings_redact = entry && entry[:redact]

  @engine = CaptureEngine.new(
    api_key: resolved_key,
    base_url: Env.resolve_base_url(base_url),
    request_id_prefix: entry && entry[:request_id_prefix],
    redact: merge_redact(settings_redact, redact),
    transport: transport
  )
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



17
18
19
# File 'lib/restless/client.rb', line 17

def engine
  @engine
end

Instance Method Details

#conformance_levelObject



90
91
92
# File 'lib/restless/client.rb', line 90

def conformance_level
  CONFORMANCE_LEVEL
end

#flushObject

BATCH-005.



72
73
74
# File 'lib/restless/client.rb', line 72

def flush
  @engine.flush
end

#mask(api_key) ⇒ Object

MASK-001. Pass the RAW header value through; never substitute a placeholder like "anonymous", whose last 4 characters would become the mask tail and cluster unrelated callers together (SETUP-001).



67
68
69
# File 'lib/restless/client.rb', line 67

def mask(api_key)
  Mask.mask(api_key)
end

#new_request_idObject



76
77
78
# File 'lib/restless/client.rb', line 76

def new_request_id
  RequestId.new_request_id
end

#rack(**options) ⇒ Object

The Rack middleware factory. use client.rack in a config.ru, or app = client.rack.new(app) by hand.



82
83
84
# File 'lib/restless/client.rb', line 82

def rack(**options)
  Middleware.factory(self, **options)
end

#setup(callable = nil, &block) ⇒ Object

Register the per-request callback. Accepts a block or any callable.

client.setup do |request|
  { api_key: client.mask(request.header("authorization")),
    owner: { id: workspace_id, enrich: ->(id) { load_workspace(id) } } }
end


59
60
61
62
# File 'lib/restless/client.rb', line 59

def setup(callable = nil, &block)
  @engine.callback = callable || block
  self
end

#spec_versionObject



86
87
88
# File 'lib/restless/client.rb', line 86

def spec_version
  SPEC_VERSION
end