seam 2.166.0 → 2.167.0
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -4
- data/README.md +55 -2
- data/lib/seam/action_attempt_resolver.rb +77 -7
- data/lib/seam/auth.rb +2 -1
- data/lib/seam/base_resource.rb +50 -3
- data/lib/seam/deep_hash_accessor.rb +11 -15
- data/lib/seam/errors.rb +6 -0
- data/lib/seam/http.rb +11 -7
- data/lib/seam/http_single_workspace.rb +15 -3
- data/lib/seam/http_without_workspace.rb +17 -7
- data/lib/seam/invalid_response_error.rb +17 -0
- data/lib/seam/options.rb +2 -1
- data/lib/seam/paginator.rb +34 -29
- data/lib/seam/request_parameters.rb +11 -0
- data/lib/seam/resources/access_code.rb +46 -3
- data/lib/seam/resources/access_grant.rb +38 -4
- data/lib/seam/resources/access_method.rb +18 -1
- data/lib/seam/resources/acs_access_group.rb +0 -17
- data/lib/seam/resources/acs_credential.rb +18 -1
- data/lib/seam/resources/acs_entrance.rb +1 -1
- data/lib/seam/resources/acs_system.rb +17 -31
- data/lib/seam/resources/acs_user.rb +0 -3
- data/lib/seam/resources/action_attempt.rb +4 -216
- data/lib/seam/resources/batch.rb +1 -1
- data/lib/seam/resources/client_session.rb +0 -4
- data/lib/seam/resources/connect_webview.rb +1 -0
- data/lib/seam/resources/connected_account.rb +1 -21
- data/lib/seam/resources/device.rb +354 -311
- data/lib/seam/resources/device_provider.rb +11 -13
- data/lib/seam/resources/event.rb +372 -151
- data/lib/seam/resources/instant_key.rb +1 -1
- data/lib/seam/resources/phone.rb +90 -2
- data/lib/seam/resources/space.rb +21 -1
- data/lib/seam/resources/unmanaged_access_code.rb +44 -4
- data/lib/seam/resources/unmanaged_access_grant.rb +38 -4
- data/lib/seam/resources/unmanaged_access_method.rb +18 -1
- data/lib/seam/resources/unmanaged_device.rb +108 -38
- data/lib/seam/resources/workspace.rb +0 -4
- data/lib/seam/routes/access_codes.rb +19 -23
- data/lib/seam/routes/access_codes_simulate.rb +1 -1
- data/lib/seam/routes/access_codes_unmanaged.rb +7 -5
- data/lib/seam/routes/access_grants.rb +24 -24
- data/lib/seam/routes/access_grants_unmanaged.rb +5 -4
- data/lib/seam/routes/access_methods.rb +20 -14
- data/lib/seam/routes/access_methods_unmanaged.rb +2 -2
- data/lib/seam/routes/acs_access_groups.rb +4 -4
- data/lib/seam/routes/acs_credentials.rb +4 -4
- data/lib/seam/routes/acs_encoders.rb +5 -5
- data/lib/seam/routes/acs_entrances.rb +6 -8
- data/lib/seam/routes/acs_systems.rb +3 -3
- data/lib/seam/routes/acs_users.rb +44 -34
- data/lib/seam/routes/action_attempts.rb +2 -2
- data/lib/seam/routes/cameras_live_views.rb +5 -5
- data/lib/seam/routes/client_sessions.rb +20 -21
- data/lib/seam/routes/connect_webviews.rb +3 -3
- data/lib/seam/routes/connected_accounts.rb +7 -5
- data/lib/seam/routes/customers.rb +4 -5
- data/lib/seam/routes/devices.rb +10 -10
- data/lib/seam/routes/devices_unmanaged.rb +7 -5
- data/lib/seam/routes/events.rb +16 -12
- data/lib/seam/routes/instant_keys.rb +7 -5
- data/lib/seam/routes/locks.rb +10 -8
- data/lib/seam/routes/locks_simulate.rb +2 -2
- data/lib/seam/routes/media.rb +1 -1
- data/lib/seam/routes/noise_sensors.rb +1 -1
- data/lib/seam/routes/noise_sensors_noise_thresholds.rb +9 -3
- data/lib/seam/routes/phones.rb +2 -2
- data/lib/seam/routes/phones_simulate.rb +1 -1
- data/lib/seam/routes/spaces.rb +18 -13
- data/lib/seam/routes/thermostats.rb +14 -18
- data/lib/seam/routes/thermostats_daily_programs.rb +2 -2
- data/lib/seam/routes/thermostats_schedules.rb +3 -3
- data/lib/seam/routes/user_identities.rb +24 -18
- data/lib/seam/routes/user_identities_unmanaged.rb +2 -2
- data/lib/seam/routes/webhooks.rb +3 -3
- data/lib/seam/routes/workspaces.rb +6 -16
- data/lib/seam/url_search_params_serializer.rb +2 -1
- data/lib/seam/version.rb +1 -1
- data/lib/seam/wait_for_action_attempt.rb +3 -1
- data/lib/seam/webhook.rb +25 -5
- data/lib/seam.rb +12 -6
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 61d5bb740b809ea1434c777494580eb4eef352137b3d50058b1c117e635582b1
|
|
4
|
+
data.tar.gz: af6291cbf3f7c121a53685f87bc6e54b318c645357caa8da7cb775c57277c04c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 16fd6fc21c791414780500cd6ebae38132a9b5a297ef2718c88f08a3823ef9a34199edd21608c713fe141f72249b75bb0292214c4ed19c1f27acd06c33d3b9f0
|
|
7
|
+
data.tar.gz: 2e26b246ac94d54d90adb50d5eb0b8be7498c15b9eec2105548ee006c1f05cdef8b485839e0d513f364b39fc9c897fb07f359160678eaa58dff807ca964c2267
|
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
seam (2.
|
|
4
|
+
seam (2.167.0)
|
|
5
5
|
faraday (~> 2.7)
|
|
6
6
|
faraday-retry (~> 2.2)
|
|
7
7
|
svix (~> 1.30)
|
|
@@ -9,7 +9,7 @@ PATH
|
|
|
9
9
|
GEM
|
|
10
10
|
remote: https://rubygems.org/
|
|
11
11
|
specs:
|
|
12
|
-
addressable (2.9.
|
|
12
|
+
addressable (2.9.1)
|
|
13
13
|
public_suffix (>= 2.0.2, < 8.0)
|
|
14
14
|
ansi (1.6.0)
|
|
15
15
|
ast (2.4.3)
|
|
@@ -133,7 +133,7 @@ DEPENDENCIES
|
|
|
133
133
|
webmock (~> 3.26.2)
|
|
134
134
|
|
|
135
135
|
CHECKSUMS
|
|
136
|
-
addressable (2.9.
|
|
136
|
+
addressable (2.9.1) sha256=f1212365945455c84e8d031eea40fc6bedf0c663a78d95b3ab93591040bc5c35
|
|
137
137
|
ansi (1.6.0) sha256=ac9ea0c0ea8d32fb4e271348e609963ac78882f34b73836c2a02b3622e666658
|
|
138
138
|
ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383
|
|
139
139
|
base64 (0.3.0) sha256=27337aeabad6ffae05c265c450490628ef3ebd4b67be58257393227588f5a97b
|
|
@@ -171,7 +171,7 @@ CHECKSUMS
|
|
|
171
171
|
rubocop-ast (1.50.0) sha256=b9ca88300da0803ee222ad20cdb30494c0a784eed06fdc35d254b06d662788db
|
|
172
172
|
rubocop-performance (1.26.1) sha256=cd19b936ff196df85829d264b522fd4f98b6c89ad271fa52744a8c11b8f71834
|
|
173
173
|
ruby-progressbar (1.13.0) sha256=80fc9c47a9b640d6834e0dc7b3c94c9df37f08cb072b7761e4a71e22cff29b33
|
|
174
|
-
seam (2.
|
|
174
|
+
seam (2.167.0)
|
|
175
175
|
simplecov (1.1.1) sha256=25825ef13f0b2e74694d769817dad6ab8e90131dabdaa666e522fea105521e78
|
|
176
176
|
simplecov-console (0.9.5) sha256=b1108bcfff5f210143e2b8301698c367b01586f20d25a73e95475a5df6fc6ff6
|
|
177
177
|
standard (1.56.0) sha256=ae2af4d9669589162ac69ed5ef59dcf9f346d4afc81f7e62b84339310dfcb787
|
data/README.md
CHANGED
|
@@ -114,6 +114,9 @@ Obtain one from the Seam Console.
|
|
|
114
114
|
# Set the `SEAM_API_KEY` environment variable
|
|
115
115
|
seam = Seam.new
|
|
116
116
|
|
|
117
|
+
# Pass as the first argument to the constructor
|
|
118
|
+
seam = Seam.new("your-api-key")
|
|
119
|
+
|
|
117
120
|
# Pass as a keyword argument to the constructor
|
|
118
121
|
seam = Seam.new(api_key: "your-api-key")
|
|
119
122
|
|
|
@@ -163,6 +166,14 @@ When the `wait_for_action_attempt` option is enabled, the SDK:
|
|
|
163
166
|
- Raises a `Seam::ActionAttemptTimeoutError` if the action attempt is still pending when the `timeout` is reached.
|
|
164
167
|
- Both errors expose an `action_attempt` property.
|
|
165
168
|
|
|
169
|
+
Polling stops as soon as the `timeout` passes,
|
|
170
|
+
and every wait polls at least once,
|
|
171
|
+
even when the `timeout` is shorter than the `polling_interval`.
|
|
172
|
+
The `timeout` must not be negative,
|
|
173
|
+
and the `polling_interval` must be greater than zero.
|
|
174
|
+
The option accepts `true`, `false`, or a hash with `timeout` and `polling_interval` keys;
|
|
175
|
+
any other value raises a `Seam::Http::Options::SeamInvalidOptionsError`.
|
|
176
|
+
|
|
166
177
|
The `error` and `result` values are only present for their matching status:
|
|
167
178
|
`error` is `nil` unless the `status` is `"error"`,
|
|
168
179
|
and `result` is `nil` unless the `status` is `"success"`.
|
|
@@ -371,8 +382,18 @@ all_devices = paginator.flatten_to_list
|
|
|
371
382
|
|
|
372
383
|
### Error Handling
|
|
373
384
|
|
|
385
|
+
Every error the SDK raises itself subclasses `Seam::Error`,
|
|
386
|
+
so a single `rescue Seam::Error` covers all of them.
|
|
387
|
+
Transport failures surface as Faraday errors,
|
|
388
|
+
and webhook verification failures as Svix's error,
|
|
389
|
+
described under [Webhooks](#webhooks).
|
|
390
|
+
|
|
374
391
|
Requests rejected by the Seam API raise a `Seam::Http::ApiError` subclass
|
|
375
392
|
carrying the HTTP `status_code`, API error `code`, and `request_id`.
|
|
393
|
+
A successful response that does not carry the resource the endpoint returns,
|
|
394
|
+
for example when a proxy rewrites the body,
|
|
395
|
+
raises a `Seam::Http::InvalidResponseError` exposing the request `path`
|
|
396
|
+
and the `response_key` the SDK expected.
|
|
376
397
|
|
|
377
398
|
#### Validation errors
|
|
378
399
|
|
|
@@ -411,6 +432,9 @@ Obtain one from the Seam Console.
|
|
|
411
432
|
# Set the `SEAM_PERSONAL_ACCESS_TOKEN` environment variable
|
|
412
433
|
seam = Seam::Http::WithoutWorkspace.new
|
|
413
434
|
|
|
435
|
+
# Pass as the first argument to the constructor
|
|
436
|
+
seam = Seam::Http::WithoutWorkspace.new("your-personal-access-token")
|
|
437
|
+
|
|
414
438
|
# Pass as a keyword argument to the constructor
|
|
415
439
|
seam = Seam::Http::WithoutWorkspace.new(
|
|
416
440
|
personal_access_token: "your-personal-access-token"
|
|
@@ -429,6 +453,11 @@ workspaces = seam.workspaces.list
|
|
|
429
453
|
|
|
430
454
|
The Seam API implements webhooks using [Svix](https://www.svix.com). This SDK exports a thin wrapper `Seam::Webhook` around the svix package.
|
|
431
455
|
Use it to parse and validate Seam webhook events.
|
|
456
|
+
Omitting the secret reads `SEAM_WEBHOOK_SECRET` when the helper is constructed.
|
|
457
|
+
An explicit secret, e.g. `Seam::Webhook.new("whsec_...")`, takes precedence over the environment.
|
|
458
|
+
A missing or non-string secret raises `TypeError`; an empty or decoded-empty secret raises
|
|
459
|
+
`Svix::EmptyWebhookSecretError` at construction. Explicit `nil` and empty strings are rejected,
|
|
460
|
+
not replaced by the environment value.
|
|
432
461
|
Known event types load as `Seam::Resources::SeamEvent` subclasses with only that event's accessors; unknown types remain generic `SeamEvent` instances for forward compatibility.
|
|
433
462
|
|
|
434
463
|
> [!TIP]
|
|
@@ -438,7 +467,8 @@ Known event types load as `Seam::Resources::SeamEvent` subclasses with only that
|
|
|
438
467
|
require "sinatra"
|
|
439
468
|
require "seam"
|
|
440
469
|
|
|
441
|
-
|
|
470
|
+
# Set SEAM_WEBHOOK_SECRET before constructing the helper.
|
|
471
|
+
webhook = Seam::Webhook.new
|
|
442
472
|
|
|
443
473
|
post "/webhook" do
|
|
444
474
|
begin
|
|
@@ -450,6 +480,9 @@ post "/webhook" do
|
|
|
450
480
|
event = webhook.verify(request.body.read, headers)
|
|
451
481
|
rescue Seam::WebhookVerificationError
|
|
452
482
|
halt 400, "Bad Request"
|
|
483
|
+
rescue Seam::InvalidWebhookPayloadError => error
|
|
484
|
+
logger.error("Unreadable Seam webhook payload: #{error.message}")
|
|
485
|
+
halt 204
|
|
453
486
|
end
|
|
454
487
|
|
|
455
488
|
begin
|
|
@@ -469,6 +502,14 @@ post "/webhook" do
|
|
|
469
502
|
end
|
|
470
503
|
```
|
|
471
504
|
|
|
505
|
+
Verification failures raise Svix's `WebhookVerificationError`,
|
|
506
|
+
re-exported as `Seam::WebhookVerificationError`:
|
|
507
|
+
treat the payload as forged and respond with an error status so Svix retries.
|
|
508
|
+
A payload that is correctly signed but unreadable raises a `Seam::InvalidWebhookPayloadError` instead:
|
|
509
|
+
it is genuinely from Seam and will never become readable,
|
|
510
|
+
so log it as a bug rather than reporting a verification failure
|
|
511
|
+
and letting Svix retry it through its full backoff schedule.
|
|
512
|
+
|
|
472
513
|
### Advanced Usage
|
|
473
514
|
|
|
474
515
|
#### Additional Options
|
|
@@ -523,7 +564,8 @@ seam = Seam.new(
|
|
|
523
564
|
)
|
|
524
565
|
```
|
|
525
566
|
|
|
526
|
-
A request
|
|
567
|
+
A request whose response does not arrive within the timeout raises `Faraday::TimeoutError`,
|
|
568
|
+
and a connection that cannot be opened within the timeout raises `Faraday::ConnectionFailed`.
|
|
527
569
|
|
|
528
570
|
#### Retry behavior
|
|
529
571
|
|
|
@@ -536,6 +578,17 @@ Retries use exponential backoff with jitter: approximately 200–240 ms before
|
|
|
536
578
|
the first retry and 400–480 ms before the second. A longer `Retry-After` header
|
|
537
579
|
is honored. The request timeout is reset for each attempt.
|
|
538
580
|
|
|
581
|
+
Retries are configured per client. If your application has its own retry
|
|
582
|
+
behavior for some calls, create a second client with retries disabled and send
|
|
583
|
+
those calls through it, while other calls keep the default retries:
|
|
584
|
+
|
|
585
|
+
```ruby
|
|
586
|
+
seam = Seam.from_api_key("some-api-key")
|
|
587
|
+
|
|
588
|
+
seam_without_retries = Seam.from_api_key("some-api-key",
|
|
589
|
+
faraday_retry_options: {max: 0})
|
|
590
|
+
```
|
|
591
|
+
|
|
539
592
|
#### Configuring the Faraday Client
|
|
540
593
|
|
|
541
594
|
The Faraday client and retry behavior may be configured with custom initiation options
|
|
@@ -1,19 +1,65 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "deep_hash_accessor"
|
|
4
|
+
require_relative "options"
|
|
3
5
|
require_relative "wait_for_action_attempt"
|
|
4
6
|
|
|
5
7
|
module Seam
|
|
6
8
|
class ActionAttemptResolver
|
|
9
|
+
TIMEOUT = 5.0
|
|
10
|
+
POLLING_INTERVAL = 0.5
|
|
11
|
+
WAIT_FOR_ACTION_ATTEMPT_OPTION_KEYS = %i[timeout polling_interval].freeze
|
|
12
|
+
|
|
7
13
|
def self.resolve(action_attempt, client, wait_for_action_attempt)
|
|
14
|
+
validate_wait_for_action_attempt(wait_for_action_attempt)
|
|
15
|
+
|
|
8
16
|
return wait_until_resolved(action_attempt, client) if wait_for_action_attempt == true
|
|
9
17
|
|
|
10
18
|
options = wait_options(wait_for_action_attempt)
|
|
11
19
|
return action_attempt if options.nil?
|
|
12
20
|
|
|
21
|
+
options = options.transform_keys(&:to_sym)
|
|
22
|
+
|
|
13
23
|
wait_until_resolved(action_attempt, client, timeout: options[:timeout],
|
|
14
24
|
polling_interval: options[:polling_interval])
|
|
15
25
|
end
|
|
16
26
|
|
|
27
|
+
def self.normalize_wait_for_action_attempt(wait_for_action_attempt)
|
|
28
|
+
return true if wait_for_action_attempt.nil?
|
|
29
|
+
|
|
30
|
+
validate_wait_for_action_attempt(wait_for_action_attempt)
|
|
31
|
+
|
|
32
|
+
wait_for_action_attempt
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.validate_wait_for_action_attempt(wait_for_action_attempt)
|
|
36
|
+
return if wait_for_action_attempt == true || wait_for_action_attempt == false
|
|
37
|
+
|
|
38
|
+
options = wait_options(wait_for_action_attempt)
|
|
39
|
+
|
|
40
|
+
if options.nil?
|
|
41
|
+
raise Http::Options::SeamInvalidOptionsError.new(
|
|
42
|
+
"The wait_for_action_attempt option must be true, false, or a hash with " \
|
|
43
|
+
"\"timeout\" and \"polling_interval\" keys, got #{wait_for_action_attempt.class}"
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
options.each do |key, option|
|
|
48
|
+
unless WAIT_FOR_ACTION_ATTEMPT_OPTION_KEYS.include?(key.to_s.to_sym)
|
|
49
|
+
raise Http::Options::SeamInvalidOptionsError.new(
|
|
50
|
+
"The wait_for_action_attempt option got an unknown key #{key.inspect}, " \
|
|
51
|
+
"expected \"timeout\" or \"polling_interval\""
|
|
52
|
+
)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
if option == true || option == false || !option.is_a?(Numeric)
|
|
56
|
+
raise Http::Options::SeamInvalidOptionsError.new(
|
|
57
|
+
"The wait_for_action_attempt option #{key.inspect} must be a number, got #{option.class}"
|
|
58
|
+
)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
17
63
|
# The client wraps its defaults in a DeepHashAccessor, so the hash form of
|
|
18
64
|
# this option reaches here as an accessor when it comes from the client
|
|
19
65
|
# and as a plain Hash when it comes from the method call.
|
|
@@ -25,16 +71,18 @@ module Seam
|
|
|
25
71
|
end
|
|
26
72
|
|
|
27
73
|
def self.wait_until_resolved(action_attempt, client, timeout: nil, polling_interval: nil)
|
|
28
|
-
timeout = timeout.nil?
|
|
29
|
-
polling_interval = polling_interval.nil?
|
|
74
|
+
timeout = TIMEOUT if timeout.nil?
|
|
75
|
+
polling_interval = POLLING_INTERVAL if polling_interval.nil?
|
|
76
|
+
validate_poll_options(timeout, polling_interval)
|
|
30
77
|
|
|
31
|
-
|
|
78
|
+
deadline = now + timeout
|
|
32
79
|
|
|
33
80
|
while action_attempt.status == "pending"
|
|
34
|
-
|
|
35
|
-
time_waiting += polling_interval
|
|
81
|
+
remaining = deadline - now
|
|
36
82
|
|
|
37
|
-
raise Seam::ActionAttemptTimeoutError.new(action_attempt, timeout) if
|
|
83
|
+
raise Seam::ActionAttemptTimeoutError.new(action_attempt, timeout) if remaining <= 0
|
|
84
|
+
|
|
85
|
+
sleep([polling_interval, remaining].min)
|
|
38
86
|
|
|
39
87
|
action_attempt = update_action_attempt(action_attempt, client)
|
|
40
88
|
end
|
|
@@ -44,11 +92,33 @@ module Seam
|
|
|
44
92
|
action_attempt
|
|
45
93
|
end
|
|
46
94
|
|
|
95
|
+
def self.validate_poll_options(timeout, polling_interval)
|
|
96
|
+
unless timeout >= 0
|
|
97
|
+
raise Http::Options::SeamInvalidOptionsError.new(
|
|
98
|
+
"The timeout option must not be negative, got #{timeout}"
|
|
99
|
+
)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
unless polling_interval > 0
|
|
103
|
+
raise Http::Options::SeamInvalidOptionsError.new(
|
|
104
|
+
"The polling_interval option must be greater than zero, got #{polling_interval}"
|
|
105
|
+
)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
47
109
|
def self.update_action_attempt(action_attempt, client)
|
|
48
110
|
response = client.get("/action_attempts/get", {action_attempt_id: action_attempt.action_attempt_id})
|
|
49
111
|
|
|
50
|
-
action_attempt.update_from_response(
|
|
112
|
+
action_attempt.update_from_response(
|
|
113
|
+
Seam::Resources::ActionAttempt.from_http_response(response, "action_attempt", "/action_attempts/get").data
|
|
114
|
+
)
|
|
51
115
|
action_attempt
|
|
52
116
|
end
|
|
117
|
+
|
|
118
|
+
def self.now
|
|
119
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
private_class_method :validate_poll_options, :update_action_attempt, :now
|
|
53
123
|
end
|
|
54
124
|
end
|
data/lib/seam/auth.rb
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "errors"
|
|
3
4
|
require_relative "options"
|
|
4
5
|
require_relative "token"
|
|
5
6
|
|
|
6
7
|
module Seam
|
|
7
8
|
module Http
|
|
8
9
|
module Auth
|
|
9
|
-
class SeamInvalidTokenError <
|
|
10
|
+
class SeamInvalidTokenError < Seam::Error
|
|
10
11
|
def initialize(message)
|
|
11
12
|
super("Seam received an invalid token: #{message}")
|
|
12
13
|
end
|
data/lib/seam/base_resource.rb
CHANGED
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
require "time"
|
|
4
4
|
require_relative "deep_hash_accessor"
|
|
5
|
+
require_relative "invalid_response_error"
|
|
5
6
|
|
|
6
7
|
module Seam
|
|
7
8
|
module Resources
|
|
8
9
|
class BaseResource
|
|
10
|
+
RESERVED_ATTRIBUTES = %w[data client].freeze
|
|
11
|
+
|
|
9
12
|
attr_accessor :data, :client
|
|
10
13
|
|
|
11
14
|
def initialize(data, client = nil)
|
|
@@ -19,6 +22,47 @@ module Seam
|
|
|
19
22
|
process_data_attributes(@data)
|
|
20
23
|
end
|
|
21
24
|
|
|
25
|
+
def self.from_http_response(response, response_key, path, client = nil)
|
|
26
|
+
value = read_http_response_key(response, response_key, path)
|
|
27
|
+
|
|
28
|
+
unless value.is_a?(Hash)
|
|
29
|
+
raise Http::InvalidResponseError.new(path, response_key, "got #{value.class} instead of an object")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
load_from_response(value, client)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.list_from_http_response(response, response_key, path, client = nil)
|
|
36
|
+
value = read_http_response_key(response, response_key, path)
|
|
37
|
+
|
|
38
|
+
unless value.is_a?(Array)
|
|
39
|
+
raise Http::InvalidResponseError.new(path, response_key, "got #{value.class} instead of a list")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
load_from_response(value, client)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.read_http_response_key(response, response_key, path)
|
|
46
|
+
unless response.success?
|
|
47
|
+
raise Http::InvalidResponseError.new(
|
|
48
|
+
path, response_key, "got a #{response.status} response instead of a success response"
|
|
49
|
+
)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
body = response.body
|
|
53
|
+
|
|
54
|
+
unless body.is_a?(Hash)
|
|
55
|
+
raise Http::InvalidResponseError.new(path, response_key, "got #{body.class} instead of a response object")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
unless body.key?(response_key)
|
|
59
|
+
raise Http::InvalidResponseError.new(path, response_key, "which the response does not contain")
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
body[response_key]
|
|
63
|
+
end
|
|
64
|
+
private_class_method :read_http_response_key
|
|
65
|
+
|
|
22
66
|
def self.load_from_response(data, client = nil)
|
|
23
67
|
return nil if data.nil?
|
|
24
68
|
|
|
@@ -101,9 +145,7 @@ module Seam
|
|
|
101
145
|
def self.date_accessor(*attrs)
|
|
102
146
|
attrs.each do |attr|
|
|
103
147
|
define_method(attr) do
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
value.nil? ? nil : parse_datetime(value)
|
|
148
|
+
parse_datetime(instance_variable_get(:"@#{attr}"))
|
|
107
149
|
end
|
|
108
150
|
end
|
|
109
151
|
end
|
|
@@ -111,12 +153,17 @@ module Seam
|
|
|
111
153
|
protected
|
|
112
154
|
|
|
113
155
|
def parse_datetime(value)
|
|
156
|
+
return nil unless value.is_a?(String)
|
|
157
|
+
|
|
114
158
|
Time.parse(value)
|
|
159
|
+
rescue ArgumentError
|
|
160
|
+
nil
|
|
115
161
|
end
|
|
116
162
|
|
|
117
163
|
def process_data_attributes(data)
|
|
118
164
|
data.each do |key, value|
|
|
119
165
|
next unless key.to_s.match?(/\A[a-zA-Z_][a-zA-Z0-9_]*\z/)
|
|
166
|
+
next if RESERVED_ATTRIBUTES.include?(key.to_s)
|
|
120
167
|
|
|
121
168
|
resource_class = self.class.resource_accessors[key.to_s]
|
|
122
169
|
resource_list_class = self.class.resource_list_accessors[key.to_s]
|
|
@@ -1,36 +1,32 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "date"
|
|
4
|
-
|
|
5
3
|
module Seam
|
|
6
4
|
class DeepHashAccessor
|
|
7
5
|
def initialize(data)
|
|
8
6
|
@data = data
|
|
9
|
-
|
|
7
|
+
@values = data.to_h { |key, value| [key.to_s, process_value(value)] }
|
|
10
8
|
end
|
|
11
9
|
|
|
12
10
|
def [](key)
|
|
13
|
-
|
|
14
|
-
# method access continues to raise NoMethodError.
|
|
15
|
-
name = key.to_s
|
|
16
|
-
return nil unless respond_to?(name)
|
|
17
|
-
|
|
18
|
-
public_send(name)
|
|
11
|
+
@values[key.to_s]
|
|
19
12
|
end
|
|
20
13
|
|
|
21
14
|
def to_h
|
|
22
15
|
@data
|
|
23
16
|
end
|
|
24
17
|
|
|
25
|
-
|
|
18
|
+
def respond_to_missing?(name, include_private = false)
|
|
19
|
+
@values.key?(name.to_s) || super
|
|
20
|
+
end
|
|
26
21
|
|
|
27
|
-
def
|
|
28
|
-
@
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
end
|
|
22
|
+
def method_missing(name, *args, &block)
|
|
23
|
+
return @values[name.to_s] if args.empty? && block.nil? && @values.key?(name.to_s)
|
|
24
|
+
|
|
25
|
+
super
|
|
32
26
|
end
|
|
33
27
|
|
|
28
|
+
private
|
|
29
|
+
|
|
34
30
|
def process_value(value)
|
|
35
31
|
case value
|
|
36
32
|
when Hash
|
data/lib/seam/errors.rb
ADDED
data/lib/seam/http.rb
CHANGED
|
@@ -1,24 +1,28 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "errors"
|
|
3
4
|
require_relative "http_single_workspace"
|
|
4
5
|
|
|
5
6
|
module Seam
|
|
6
7
|
module Http
|
|
7
|
-
def self.new(**
|
|
8
|
-
Http::SingleWorkspace.new(**
|
|
8
|
+
def self.new(api_key = nil, **options)
|
|
9
|
+
Http::SingleWorkspace.new(api_key, **options)
|
|
9
10
|
end
|
|
10
11
|
|
|
11
|
-
def self.from_api_key(api_key, endpoint: nil, wait_for_action_attempt: true, timeout: nil
|
|
12
|
+
def self.from_api_key(api_key, endpoint: nil, wait_for_action_attempt: true, timeout: nil, faraday_options: {},
|
|
13
|
+
faraday_retry_options: {})
|
|
12
14
|
Http::SingleWorkspace.from_api_key(api_key, endpoint: endpoint, wait_for_action_attempt: wait_for_action_attempt,
|
|
13
|
-
timeout: timeout)
|
|
15
|
+
timeout: timeout, faraday_options: faraday_options, faraday_retry_options: faraday_retry_options)
|
|
14
16
|
end
|
|
15
17
|
|
|
16
|
-
def self.from_personal_access_token(personal_access_token, workspace_id, endpoint: nil,
|
|
18
|
+
def self.from_personal_access_token(personal_access_token, workspace_id, endpoint: nil,
|
|
19
|
+
wait_for_action_attempt: true, timeout: nil, faraday_options: {}, faraday_retry_options: {})
|
|
17
20
|
Http::SingleWorkspace.from_personal_access_token(personal_access_token, workspace_id, endpoint: endpoint,
|
|
18
|
-
wait_for_action_attempt: wait_for_action_attempt, timeout: timeout
|
|
21
|
+
wait_for_action_attempt: wait_for_action_attempt, timeout: timeout, faraday_options: faraday_options,
|
|
22
|
+
faraday_retry_options: faraday_retry_options)
|
|
19
23
|
end
|
|
20
24
|
|
|
21
|
-
class ApiError <
|
|
25
|
+
class ApiError < Seam::Error
|
|
22
26
|
attr_reader :code, :status_code, :request_id, :data
|
|
23
27
|
|
|
24
28
|
def initialize(error, status_code, request_id)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "action_attempt_resolver"
|
|
3
4
|
require_relative "request"
|
|
4
5
|
require_relative "parse_options"
|
|
5
6
|
require_relative "resources/index"
|
|
@@ -19,10 +20,20 @@ module Seam
|
|
|
19
20
|
OPTION_NOT_PROVIDED = Object.new.freeze
|
|
20
21
|
private_constant :OPTION_NOT_PROVIDED
|
|
21
22
|
|
|
22
|
-
def initialize(
|
|
23
|
-
workspace_id: OPTION_NOT_PROVIDED, endpoint: OPTION_NOT_PROVIDED,
|
|
23
|
+
def initialize(token = nil, client: nil, api_key: OPTION_NOT_PROVIDED,
|
|
24
|
+
personal_access_token: OPTION_NOT_PROVIDED, workspace_id: OPTION_NOT_PROVIDED, endpoint: OPTION_NOT_PROVIDED,
|
|
24
25
|
wait_for_action_attempt: OPTION_NOT_PROVIDED, timeout: OPTION_NOT_PROVIDED,
|
|
25
26
|
faraday_options: OPTION_NOT_PROVIDED, faraday_retry_options: OPTION_NOT_PROVIDED)
|
|
27
|
+
unless token.nil?
|
|
28
|
+
unless api_key.equal?(OPTION_NOT_PROVIDED)
|
|
29
|
+
raise Http::Options::SeamInvalidOptionsError.new(
|
|
30
|
+
"The api_key was given both as a positional argument and as the api_key option"
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
api_key = token
|
|
35
|
+
end
|
|
36
|
+
|
|
26
37
|
supplied_options = {
|
|
27
38
|
api_key: api_key,
|
|
28
39
|
personal_access_token: personal_access_token,
|
|
@@ -41,7 +52,8 @@ module Seam
|
|
|
41
52
|
)
|
|
42
53
|
end
|
|
43
54
|
|
|
44
|
-
wait_for_action_attempt =
|
|
55
|
+
wait_for_action_attempt = nil if wait_for_action_attempt.equal?(OPTION_NOT_PROVIDED)
|
|
56
|
+
wait_for_action_attempt = Seam::ActionAttemptResolver.normalize_wait_for_action_attempt(wait_for_action_attempt)
|
|
45
57
|
@defaults = Seam::DeepHashAccessor.new({"wait_for_action_attempt" => wait_for_action_attempt})
|
|
46
58
|
|
|
47
59
|
# A client carries its own endpoint and authorization, so the auth
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "action_attempt_resolver"
|
|
3
4
|
require_relative "request"
|
|
4
5
|
require_relative "parse_options"
|
|
5
|
-
require_relative "
|
|
6
|
-
require_relative "auth"
|
|
6
|
+
require_relative "deep_hash_accessor"
|
|
7
7
|
require_relative "resources/index"
|
|
8
8
|
require_relative "routes/index"
|
|
9
9
|
require_relative "routes/routes"
|
|
@@ -16,9 +16,19 @@ module Seam
|
|
|
16
16
|
OPTION_NOT_PROVIDED = Object.new.freeze
|
|
17
17
|
private_constant :OPTION_NOT_PROVIDED
|
|
18
18
|
|
|
19
|
-
def initialize(
|
|
20
|
-
wait_for_action_attempt: OPTION_NOT_PROVIDED, timeout: OPTION_NOT_PROVIDED,
|
|
19
|
+
def initialize(token = nil, client: nil, personal_access_token: OPTION_NOT_PROVIDED,
|
|
20
|
+
endpoint: OPTION_NOT_PROVIDED, wait_for_action_attempt: OPTION_NOT_PROVIDED, timeout: OPTION_NOT_PROVIDED,
|
|
21
21
|
faraday_options: OPTION_NOT_PROVIDED, faraday_retry_options: OPTION_NOT_PROVIDED)
|
|
22
|
+
unless token.nil?
|
|
23
|
+
unless personal_access_token.equal?(OPTION_NOT_PROVIDED)
|
|
24
|
+
raise Http::Options::SeamInvalidOptionsError.new(
|
|
25
|
+
"The personal_access_token was given both as a positional argument and as the personal_access_token option"
|
|
26
|
+
)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
personal_access_token = token
|
|
30
|
+
end
|
|
31
|
+
|
|
22
32
|
supplied_options = {
|
|
23
33
|
personal_access_token: personal_access_token,
|
|
24
34
|
endpoint: endpoint,
|
|
@@ -35,9 +45,9 @@ module Seam
|
|
|
35
45
|
)
|
|
36
46
|
end
|
|
37
47
|
|
|
38
|
-
wait_for_action_attempt =
|
|
39
|
-
|
|
40
|
-
@defaults = {"wait_for_action_attempt" => wait_for_action_attempt}
|
|
48
|
+
wait_for_action_attempt = nil if wait_for_action_attempt.equal?(OPTION_NOT_PROVIDED)
|
|
49
|
+
wait_for_action_attempt = Seam::ActionAttemptResolver.normalize_wait_for_action_attempt(wait_for_action_attempt)
|
|
50
|
+
@defaults = Seam::DeepHashAccessor.new({"wait_for_action_attempt" => wait_for_action_attempt})
|
|
41
51
|
|
|
42
52
|
@client = client || begin
|
|
43
53
|
personal_access_token = nil if personal_access_token.equal?(OPTION_NOT_PROVIDED)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "errors"
|
|
4
|
+
|
|
5
|
+
module Seam
|
|
6
|
+
module Http
|
|
7
|
+
class InvalidResponseError < Seam::Error
|
|
8
|
+
attr_reader :path, :response_key
|
|
9
|
+
|
|
10
|
+
def initialize(path, response_key, reason)
|
|
11
|
+
super("Seam returned an invalid response for #{path}: expected \"#{response_key}\", #{reason}")
|
|
12
|
+
@path = path
|
|
13
|
+
@response_key = response_key
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/seam/options.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "defaults"
|
|
4
|
+
require_relative "errors"
|
|
4
5
|
|
|
5
6
|
module Seam
|
|
6
7
|
module Http
|
|
@@ -24,7 +25,7 @@ module Seam
|
|
|
24
25
|
seam_endpoint || seam_api_url
|
|
25
26
|
end
|
|
26
27
|
|
|
27
|
-
class SeamInvalidOptionsError <
|
|
28
|
+
class SeamInvalidOptionsError < Seam::Error
|
|
28
29
|
def initialize(message)
|
|
29
30
|
super("Seam received invalid options: #{message}")
|
|
30
31
|
end
|