smplkit 1.0.16 → 1.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/api/default_api.rb +305 -0
  3. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/api_client.rb +441 -0
  4. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/api_error.rb +58 -0
  5. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/api_model_base.rb +88 -0
  6. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/configuration.rb +392 -0
  7. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/models/event.rb +327 -0
  8. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/models/event_list_links.rb +148 -0
  9. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/models/event_list_meta.rb +164 -0
  10. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/models/event_list_response.rb +203 -0
  11. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/models/event_resource.rb +202 -0
  12. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/models/event_response.rb +165 -0
  13. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/models/usage_resource.rb +203 -0
  14. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/models/usage_response.rb +166 -0
  15. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client/version.rb +15 -0
  16. data/lib/smplkit/_generated/audit/lib/smplkit_audit_client.rb +49 -0
  17. data/lib/smplkit/_generated/audit/spec/api/default_api_spec.rb +91 -0
  18. data/lib/smplkit/_generated/audit/spec/models/event_list_links_spec.rb +36 -0
  19. data/lib/smplkit/_generated/audit/spec/models/event_list_meta_spec.rb +36 -0
  20. data/lib/smplkit/_generated/audit/spec/models/event_list_response_spec.rb +48 -0
  21. data/lib/smplkit/_generated/audit/spec/models/event_resource_spec.rb +48 -0
  22. data/lib/smplkit/_generated/audit/spec/models/event_response_spec.rb +36 -0
  23. data/lib/smplkit/_generated/audit/spec/models/event_spec.rb +96 -0
  24. data/lib/smplkit/_generated/audit/spec/models/usage_resource_spec.rb +48 -0
  25. data/lib/smplkit/_generated/audit/spec/models/usage_response_spec.rb +36 -0
  26. data/lib/smplkit/_generated/audit/spec/spec_helper.rb +111 -0
  27. data/lib/smplkit/audit/buffer.rb +158 -0
  28. data/lib/smplkit/audit/client.rb +29 -0
  29. data/lib/smplkit/audit/events.rb +125 -0
  30. data/lib/smplkit/client.rb +7 -2
  31. data/lib/smplkit.rb +6 -2
  32. metadata +29 -1
@@ -0,0 +1,36 @@
1
+ =begin
2
+ #smplkit Audit API
3
+
4
+ #Append-only change-history substrate for smpl.* resources and customer-application events. ADR-047.
5
+
6
+ The version of the OpenAPI document: 0.1.0
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.22.0
10
+
11
+ =end
12
+
13
+ require 'spec_helper'
14
+ require 'json'
15
+ require 'date'
16
+
17
+ # Unit tests for SmplkitGeneratedClient::Audit::EventResponse
18
+ # Automatically generated by openapi-generator (https://openapi-generator.tech)
19
+ # Please update as you see appropriate
20
+ describe SmplkitGeneratedClient::Audit::EventResponse do
21
+ #let(:instance) { SmplkitGeneratedClient::Audit::EventResponse.new }
22
+
23
+ describe 'test an instance of EventResponse' do
24
+ it 'should create an instance of EventResponse' do
25
+ # uncomment below to test the instance creation
26
+ #expect(instance).to be_instance_of(SmplkitGeneratedClient::Audit::EventResponse)
27
+ end
28
+ end
29
+
30
+ describe 'test attribute "data"' do
31
+ it 'should work' do
32
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,96 @@
1
+ =begin
2
+ #smplkit Audit API
3
+
4
+ #Append-only change-history substrate for smpl.* resources and customer-application events. ADR-047.
5
+
6
+ The version of the OpenAPI document: 0.1.0
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.22.0
10
+
11
+ =end
12
+
13
+ require 'spec_helper'
14
+ require 'json'
15
+ require 'date'
16
+
17
+ # Unit tests for SmplkitGeneratedClient::Audit::Event
18
+ # Automatically generated by openapi-generator (https://openapi-generator.tech)
19
+ # Please update as you see appropriate
20
+ describe SmplkitGeneratedClient::Audit::Event do
21
+ #let(:instance) { SmplkitGeneratedClient::Audit::Event.new }
22
+
23
+ describe 'test an instance of Event' do
24
+ it 'should create an instance of Event' do
25
+ # uncomment below to test the instance creation
26
+ #expect(instance).to be_instance_of(SmplkitGeneratedClient::Audit::Event)
27
+ end
28
+ end
29
+
30
+ describe 'test attribute "action"' do
31
+ it 'should work' do
32
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
33
+ end
34
+ end
35
+
36
+ describe 'test attribute "resource_type"' do
37
+ it 'should work' do
38
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
39
+ end
40
+ end
41
+
42
+ describe 'test attribute "resource_id"' do
43
+ it 'should work' do
44
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
45
+ end
46
+ end
47
+
48
+ describe 'test attribute "occurred_at"' do
49
+ it 'should work' do
50
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
51
+ end
52
+ end
53
+
54
+ describe 'test attribute "snapshot"' do
55
+ it 'should work' do
56
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
57
+ end
58
+ end
59
+
60
+ describe 'test attribute "data"' do
61
+ it 'should work' do
62
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
63
+ end
64
+ end
65
+
66
+ describe 'test attribute "created_at"' do
67
+ it 'should work' do
68
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
69
+ end
70
+ end
71
+
72
+ describe 'test attribute "actor_type"' do
73
+ it 'should work' do
74
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
75
+ end
76
+ end
77
+
78
+ describe 'test attribute "actor_id"' do
79
+ it 'should work' do
80
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
81
+ end
82
+ end
83
+
84
+ describe 'test attribute "actor_label"' do
85
+ it 'should work' do
86
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
87
+ end
88
+ end
89
+
90
+ describe 'test attribute "idempotency_key"' do
91
+ it 'should work' do
92
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
93
+ end
94
+ end
95
+
96
+ end
@@ -0,0 +1,48 @@
1
+ =begin
2
+ #smplkit Audit API
3
+
4
+ #Append-only change-history substrate for smpl.* resources and customer-application events. ADR-047.
5
+
6
+ The version of the OpenAPI document: 0.1.0
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.22.0
10
+
11
+ =end
12
+
13
+ require 'spec_helper'
14
+ require 'json'
15
+ require 'date'
16
+
17
+ # Unit tests for SmplkitGeneratedClient::Audit::UsageResource
18
+ # Automatically generated by openapi-generator (https://openapi-generator.tech)
19
+ # Please update as you see appropriate
20
+ describe SmplkitGeneratedClient::Audit::UsageResource do
21
+ #let(:instance) { SmplkitGeneratedClient::Audit::UsageResource.new }
22
+
23
+ describe 'test an instance of UsageResource' do
24
+ it 'should create an instance of UsageResource' do
25
+ # uncomment below to test the instance creation
26
+ #expect(instance).to be_instance_of(SmplkitGeneratedClient::Audit::UsageResource)
27
+ end
28
+ end
29
+
30
+ describe 'test attribute "id"' do
31
+ it 'should work' do
32
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
33
+ end
34
+ end
35
+
36
+ describe 'test attribute "type"' do
37
+ it 'should work' do
38
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
39
+ end
40
+ end
41
+
42
+ describe 'test attribute "attributes"' do
43
+ it 'should work' do
44
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,36 @@
1
+ =begin
2
+ #smplkit Audit API
3
+
4
+ #Append-only change-history substrate for smpl.* resources and customer-application events. ADR-047.
5
+
6
+ The version of the OpenAPI document: 0.1.0
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.22.0
10
+
11
+ =end
12
+
13
+ require 'spec_helper'
14
+ require 'json'
15
+ require 'date'
16
+
17
+ # Unit tests for SmplkitGeneratedClient::Audit::UsageResponse
18
+ # Automatically generated by openapi-generator (https://openapi-generator.tech)
19
+ # Please update as you see appropriate
20
+ describe SmplkitGeneratedClient::Audit::UsageResponse do
21
+ #let(:instance) { SmplkitGeneratedClient::Audit::UsageResponse.new }
22
+
23
+ describe 'test an instance of UsageResponse' do
24
+ it 'should create an instance of UsageResponse' do
25
+ # uncomment below to test the instance creation
26
+ #expect(instance).to be_instance_of(SmplkitGeneratedClient::Audit::UsageResponse)
27
+ end
28
+ end
29
+
30
+ describe 'test attribute "data"' do
31
+ it 'should work' do
32
+ # assertion here. ref: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,111 @@
1
+ =begin
2
+ #smplkit Audit API
3
+
4
+ #Append-only change-history substrate for smpl.* resources and customer-application events. ADR-047.
5
+
6
+ The version of the OpenAPI document: 0.1.0
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.22.0
10
+
11
+ =end
12
+
13
+ # load the gem
14
+ require 'smplkit_audit_client'
15
+
16
+ # The following was generated by the `rspec --init` command. Conventionally, all
17
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
18
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
19
+ # this file to always be loaded, without a need to explicitly require it in any
20
+ # files.
21
+ #
22
+ # Given that it is always loaded, you are encouraged to keep this file as
23
+ # light-weight as possible. Requiring heavyweight dependencies from this file
24
+ # will add to the boot time of your test suite on EVERY test run, even for an
25
+ # individual file that may not need all of that loaded. Instead, consider making
26
+ # a separate helper file that requires the additional dependencies and performs
27
+ # the additional setup, and require it from the spec files that actually need
28
+ # it.
29
+ #
30
+ # The `.rspec` file also contains a few flags that are not defaults but that
31
+ # users commonly want.
32
+ #
33
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
34
+ RSpec.configure do |config|
35
+ # rspec-expectations config goes here. You can use an alternate
36
+ # assertion/expectation library such as wrong or the stdlib/minitest
37
+ # assertions if you prefer.
38
+ config.expect_with :rspec do |expectations|
39
+ # This option will default to `true` in RSpec 4. It makes the `description`
40
+ # and `failure_message` of custom matchers include text for helper methods
41
+ # defined using `chain`, e.g.:
42
+ # be_bigger_than(2).and_smaller_than(4).description
43
+ # # => "be bigger than 2 and smaller than 4"
44
+ # ...rather than:
45
+ # # => "be bigger than 2"
46
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
47
+ end
48
+
49
+ # rspec-mocks config goes here. You can use an alternate test double
50
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
51
+ config.mock_with :rspec do |mocks|
52
+ # Prevents you from mocking or stubbing a method that does not exist on
53
+ # a real object. This is generally recommended, and will default to
54
+ # `true` in RSpec 4.
55
+ mocks.verify_partial_doubles = true
56
+ end
57
+
58
+ # The settings below are suggested to provide a good initial experience
59
+ # with RSpec, but feel free to customize to your heart's content.
60
+ =begin
61
+ # These two settings work together to allow you to limit a spec run
62
+ # to individual examples or groups you care about by tagging them with
63
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
64
+ # get run.
65
+ config.filter_run :focus
66
+ config.run_all_when_everything_filtered = true
67
+
68
+ # Allows RSpec to persist some state between runs in order to support
69
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
70
+ # you configure your source control system to ignore this file.
71
+ config.example_status_persistence_file_path = "spec/examples.txt"
72
+
73
+ # Limits the available syntax to the non-monkey patched syntax that is
74
+ # recommended. For more details, see:
75
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
76
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
77
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
78
+ config.disable_monkey_patching!
79
+
80
+ # This setting enables warnings. It's recommended, but in some cases may
81
+ # be too noisy due to issues in dependencies.
82
+ config.warnings = true
83
+
84
+ # Many RSpec users commonly either run the entire suite or an individual
85
+ # file, and it's useful to allow more verbose output when running an
86
+ # individual spec file.
87
+ if config.files_to_run.one?
88
+ # Use the documentation formatter for detailed output,
89
+ # unless a formatter has already been configured
90
+ # (e.g. via a command-line flag).
91
+ config.default_formatter = 'doc'
92
+ end
93
+
94
+ # Print the 10 slowest examples and example groups at the
95
+ # end of the spec run, to help surface which specs are running
96
+ # particularly slow.
97
+ config.profile_examples = 10
98
+
99
+ # Run specs in random order to surface order dependencies. If you find an
100
+ # order dependency and want to debug it, you can fix the order by providing
101
+ # the seed, which is printed after each run.
102
+ # --seed 1234
103
+ config.order = :random
104
+
105
+ # Seed global randomization in this process using the `--seed` CLI option.
106
+ # Setting this allows you to use `--seed` to deterministically reproduce
107
+ # test failures related to randomization by passing the same `--seed` value
108
+ # as the one that triggered the failure.
109
+ Kernel.srand config.seed
110
+ =end
111
+ end
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smplkit
4
+ module Audit
5
+ # Bounded in-memory queue + worker thread for fire-and-forget audit
6
+ # emits (ADR-047 §2.6).
7
+ #
8
+ # +#enqueue+ returns immediately. The worker drains on either a
9
+ # periodic tick or once depth crosses the high-water mark, retries
10
+ # transient failures with exponential backoff, drops permanent 4xx
11
+ # (other than 429), and evicts the oldest item under sustained
12
+ # back-pressure.
13
+ class EventBuffer
14
+ MAX_BUFFER_SIZE = 1000
15
+ WATERMARK = 50
16
+ FLUSH_INTERVAL = 5.0
17
+ MAX_ATTEMPTS = 5
18
+ INITIAL_BACKOFF = 0.25
19
+ MAX_BACKOFF = 8.0
20
+
21
+ def initialize(api)
22
+ @api = api
23
+ @queue = []
24
+ @mutex = Mutex.new
25
+ @cond = ConditionVariable.new
26
+ @closed = false
27
+ @dropped_count = 0
28
+ @worker = Thread.new { run }
29
+ @worker.report_on_exception = false
30
+ end
31
+
32
+ def enqueue(body, idempotency_key)
33
+ depth = nil
34
+ @mutex.synchronize do
35
+ return if @closed
36
+
37
+ if @queue.size >= MAX_BUFFER_SIZE
38
+ @queue.shift
39
+ @dropped_count += 1
40
+ warn "[smplkit.audit] buffer full (size=#{MAX_BUFFER_SIZE}); " \
41
+ "dropped oldest event (total dropped=#{@dropped_count})"
42
+ end
43
+ @queue.push(PendingEvent.new(body, idempotency_key))
44
+ depth = @queue.size
45
+ @cond.signal if depth >= WATERMARK
46
+ end
47
+ end
48
+
49
+ def flush(timeout: 5.0)
50
+ deadline = monotonic_now + timeout
51
+ loop do
52
+ empty = @mutex.synchronize { @queue.empty? }
53
+ return if empty
54
+
55
+ if monotonic_now >= deadline
56
+ warn "[smplkit.audit] flush timed out (timeout=#{timeout}s)"
57
+ return
58
+ end
59
+ @mutex.synchronize { @cond.signal }
60
+ sleep 0.05
61
+ end
62
+ end
63
+
64
+ def close(timeout: 5.0)
65
+ flush(timeout: timeout)
66
+ @mutex.synchronize do
67
+ @closed = true
68
+ @cond.broadcast
69
+ end
70
+ @worker.join(timeout)
71
+ end
72
+
73
+ private
74
+
75
+ def run
76
+ loop do
77
+ drain_once
78
+ should_exit = false
79
+ sleep_for = FLUSH_INTERVAL
80
+ @mutex.synchronize do
81
+ should_exit = @closed && @queue.empty?
82
+ unless @queue.empty?
83
+ head = @queue.first
84
+ if head.next_retry_at
85
+ until_next = head.next_retry_at - monotonic_now
86
+ sleep_for = until_next if until_next.positive? && until_next < sleep_for
87
+ end
88
+ end
89
+ @cond.wait(@mutex, sleep_for) unless should_exit
90
+ end
91
+ break if should_exit
92
+ end
93
+ end
94
+
95
+ def drain_once
96
+ loop do
97
+ item = nil
98
+ @mutex.synchronize do
99
+ return if @queue.empty?
100
+
101
+ head = @queue.first
102
+ return if head.next_retry_at && head.next_retry_at > monotonic_now
103
+
104
+ item = @queue.shift
105
+ end
106
+
107
+ status = 0
108
+ begin
109
+ opts = item.idempotency_key ? { idempotency_key: item.idempotency_key } : {}
110
+ @api.create_event(item.body, opts)
111
+ status = 201
112
+ rescue SmplkitGeneratedClient::Audit::ApiError => e
113
+ status = e.code || 0
114
+ rescue StandardError
115
+ status = 0
116
+ end
117
+
118
+ requeue = handle_outcome(item, status)
119
+ if requeue
120
+ @mutex.synchronize { @queue.unshift(requeue) }
121
+ return
122
+ end
123
+ end
124
+ end
125
+
126
+ def handle_outcome(item, status)
127
+ return nil if status >= 200 && status < 300
128
+
129
+ if status >= 400 && status < 500 && status != 429
130
+ warn "[smplkit.audit] permanent failure status=#{status}; event dropped"
131
+ return nil
132
+ end
133
+
134
+ item.attempts += 1
135
+ if item.attempts >= MAX_ATTEMPTS
136
+ warn "[smplkit.audit] gave up after #{item.attempts} attempts (status=#{status})"
137
+ return nil
138
+ end
139
+ backoff = [MAX_BACKOFF, INITIAL_BACKOFF * (2**(item.attempts - 1))].min
140
+ jitter = rand(0.0..(backoff * 0.25))
141
+ item.next_retry_at = monotonic_now + backoff + jitter
142
+ item
143
+ end
144
+
145
+ def monotonic_now
146
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
147
+ end
148
+
149
+ # Holds an outbound event between attempts.
150
+ PendingEvent = Struct.new(:body, :idempotency_key, :attempts, :next_retry_at) do
151
+ def initialize(body, idempotency_key, attempts = 0, next_retry_at = nil)
152
+ super
153
+ end
154
+ end
155
+ private_constant :PendingEvent
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smplkit
4
+ module Audit
5
+ # Audit-product entry point — accessed via +client.audit+.
6
+ #
7
+ # Today the audit namespace is exclusively +#events+; future
8
+ # iterations may add SIEM exports as additional sub-clients
9
+ # (ADR-047 §2.7 lists SIEM streaming as a Pro-tier capability).
10
+ class AuditClient
11
+ attr_reader :events
12
+
13
+ def initialize(api_key:, base_url:, timeout: 10.0)
14
+ cfg = SmplkitGeneratedClient::Audit::Configuration.new
15
+ cfg.host = URI.parse(base_url).host
16
+ cfg.scheme = URI.parse(base_url).scheme
17
+ cfg.access_token = api_key
18
+ cfg.timeout = timeout
19
+ api_client = SmplkitGeneratedClient::Audit::ApiClient.new(cfg)
20
+ api = SmplkitGeneratedClient::Audit::DefaultApi.new(api_client)
21
+ @events = Events.new(api)
22
+ end
23
+
24
+ def _close
25
+ @events._close
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smplkit
4
+ module Audit
5
+ # Audit events surface — accessed via +client.audit.events+.
6
+ #
7
+ # +#create+ is fire-and-forget per ADR-047 §2.6 — the call enqueues
8
+ # the event onto an in-memory bounded buffer and returns
9
+ # immediately. +#list+ and +#get+ are synchronous.
10
+ class Events
11
+ def initialize(api)
12
+ @api = api
13
+ @buffer = EventBuffer.new(api)
14
+ end
15
+
16
+ # Enqueue an audit event for asynchronous delivery.
17
+ #
18
+ # Customer attempts to record events with +resource_type+ starting
19
+ # with +smpl.+ are rejected by the server with a 403 (the buffer
20
+ # logs and drops permanent failures).
21
+ def create(action:, resource_type:, resource_id:,
22
+ occurred_at: nil, snapshot: nil, data: nil, idempotency_key: nil)
23
+ raise ArgumentError, "action is required" if action.nil? || action.to_s.empty?
24
+ raise ArgumentError, "resource_type is required" if resource_type.nil? || resource_type.to_s.empty?
25
+ raise ArgumentError, "resource_id is required" if resource_id.nil? || resource_id.to_s.empty?
26
+
27
+ attrs = SmplkitGeneratedClient::Audit::Event.new(
28
+ action: action,
29
+ resource_type: resource_type,
30
+ resource_id: resource_id,
31
+ occurred_at: occurred_at,
32
+ snapshot: snapshot,
33
+ data: data
34
+ )
35
+ resource = SmplkitGeneratedClient::Audit::EventResource.new(
36
+ id: "",
37
+ type: "event",
38
+ attributes: attrs
39
+ )
40
+ body = SmplkitGeneratedClient::Audit::EventResponse.new(data: resource)
41
+ @buffer.enqueue(body, idempotency_key)
42
+ end
43
+
44
+ # Single-event retrieval.
45
+ #
46
+ # Raises +SmplkitGeneratedClient::Audit::ApiError+ on non-2xx
47
+ # responses (404 if the event is not in the caller's account).
48
+ def get(event_id)
49
+ resp = @api.get_event(event_id)
50
+ AuditEvent.from_resource(resp.data)
51
+ end
52
+
53
+ # List events with filters and cursor pagination. Returns a
54
+ # +Smplkit::Audit::ListEventsPage+ whose +#events+ is the page and
55
+ # +#next_cursor+ is the opaque token for the next page (or nil).
56
+ def list(action: nil, resource_type: nil, resource_id: nil,
57
+ actor_type: nil, actor_id: nil, occurred_at_range: nil,
58
+ page_size: nil, page_after: nil)
59
+ opts = {}
60
+ opts[:filteraction] = action if action
61
+ opts[:filterresource_type] = resource_type if resource_type
62
+ opts[:filterresource_id] = resource_id if resource_id
63
+ opts[:filteractor_type] = actor_type if actor_type
64
+ opts[:filteractor_id] = actor_id if actor_id
65
+ opts[:filteroccurred_at] = occurred_at_range if occurred_at_range
66
+ opts[:pagesize] = page_size if page_size
67
+ opts[:pageafter] = page_after if page_after
68
+
69
+ resp = @api.list_events(opts)
70
+ events = (resp.data || []).map { |r| AuditEvent.from_resource(r) }
71
+ next_cursor = nil
72
+ if resp.links&._next.is_a?(String)
73
+ next_link = resp.links._next
74
+ if (idx = next_link.index("page[after]="))
75
+ next_cursor = next_link[(idx + "page[after]=".length)..]
76
+ if (amp = next_cursor.index("&"))
77
+ next_cursor = next_cursor[0...amp]
78
+ end
79
+ end
80
+ end
81
+ ListEventsPage.new(events, next_cursor)
82
+ end
83
+
84
+ # Block until the in-memory buffer is drained or the timeout elapses.
85
+ def flush(timeout: 5.0)
86
+ @buffer.flush(timeout: timeout)
87
+ end
88
+
89
+ # @api private — drains best-effort and stops the worker thread.
90
+ def _close
91
+ @buffer.close
92
+ end
93
+ end
94
+
95
+ # Public-facing audit event resource. ADR-047 §2.3.1.
96
+ AuditEvent = Struct.new(
97
+ :id, :action, :resource_type, :resource_id,
98
+ :occurred_at, :created_at,
99
+ :actor_type, :actor_id, :actor_label,
100
+ :snapshot, :data, :idempotency_key,
101
+ keyword_init: true
102
+ ) do
103
+ def self.from_resource(resource)
104
+ attrs = resource.attributes
105
+ new(
106
+ id: resource.id,
107
+ action: attrs.action,
108
+ resource_type: attrs.resource_type,
109
+ resource_id: attrs.resource_id,
110
+ occurred_at: attrs.occurred_at,
111
+ created_at: attrs.created_at,
112
+ actor_type: attrs.actor_type,
113
+ actor_id: attrs.actor_id,
114
+ actor_label: attrs.actor_label,
115
+ snapshot: attrs.snapshot,
116
+ data: attrs.data || {},
117
+ idempotency_key: attrs.idempotency_key
118
+ )
119
+ end
120
+ end
121
+
122
+ # One page of events plus a cursor for the next page (nil on the last page).
123
+ ListEventsPage = Struct.new(:events, :next_cursor)
124
+ end
125
+ end
@@ -2,6 +2,8 @@
2
2
 
3
3
  require "concurrent"
4
4
 
5
+ require "smplkit/audit/client"
6
+
5
7
  module Smplkit
6
8
  # Synchronous entry point for the smplkit SDK.
7
9
  #
@@ -28,7 +30,7 @@ module Smplkit
28
30
  class Client
29
31
  PERIODIC_FLUSH_INTERVAL = 60.0
30
32
 
31
- attr_reader :manage, :config, :flags, :logging
33
+ attr_reader :manage, :config, :flags, :logging, :audit
32
34
 
33
35
  # Construct, yield to the block, and close on exit.
34
36
  def self.open(**kwargs)
@@ -40,7 +42,7 @@ module Smplkit
40
42
  end
41
43
  end
42
44
 
43
- def initialize(api_key: nil, environment: nil, service: nil, profile: nil,
45
+ def initialize(api_key: nil, environment: nil, service: nil, profile: nil, # rubocop:disable Metrics/AbcSize
44
46
  base_domain: nil, scheme: nil, debug: nil, telemetry: nil)
45
47
  cfg = ConfigResolution.resolve_config(
46
48
  profile: profile, api_key: api_key, base_domain: base_domain, scheme: scheme,
@@ -70,6 +72,7 @@ module Smplkit
70
72
  app_url = ConfigResolution.service_url(cfg.scheme, "app", cfg.base_domain)
71
73
  flags_url = ConfigResolution.service_url(cfg.scheme, "flags", cfg.base_domain)
72
74
  logging_url = ConfigResolution.service_url(cfg.scheme, "logging", cfg.base_domain)
75
+ audit_url = ConfigResolution.service_url(cfg.scheme, "audit", cfg.base_domain)
73
76
  @app_base_url = app_url
74
77
 
75
78
  @metrics = if cfg.telemetry
@@ -84,6 +87,7 @@ module Smplkit
84
87
  flags_base_url: flags_url, app_base_url: app_url)
85
88
  @logging = Logging::LoggingClient.new(self, manage: @manage, metrics: @metrics,
86
89
  logging_base_url: logging_url, app_base_url: app_url)
90
+ @audit = Audit::AuditClient.new(api_key: cfg.api_key, base_url: audit_url)
87
91
 
88
92
  @closed = false
89
93
  schedule_periodic_flush
@@ -153,6 +157,7 @@ module Smplkit
153
157
  @logging._close
154
158
  @flags._close
155
159
  @config._close
160
+ @audit._close
156
161
  @ws_manager&.stop
157
162
  @ws_manager = nil
158
163
  @manage.close