sendmux-core 1.2.0 → 2.0.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
- metadata +11 -41
- data/CHANGELOG.md +0 -27
- data/LICENSE +0 -21
- data/README.md +0 -98
- data/lib/sendmux/core/api_key_surface.rb +0 -11
- data/lib/sendmux/core/auth.rb +0 -63
- data/lib/sendmux/core/errors.rb +0 -103
- data/lib/sendmux/core/headers.rb +0 -26
- data/lib/sendmux/core/pagination.rb +0 -64
- data/lib/sendmux/core/retry.rb +0 -31
- data/lib/sendmux/core/retry_options.rb +0 -39
- data/lib/sendmux/core/version.rb +0 -7
- data/lib/sendmux/core.rb +0 -10
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f0be98427379d71222d6ceb17604c9441e7885bae84f1d638e9a553d512c0b10
|
|
4
|
+
data.tar.gz: d2f29306a04cce725da1af143e66721200f6bb3abfafd6fd395ad54bb1e41c8c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1bae7f265812971563803e64e6c448b38279744c18fd85d8653f932306153ea6ab20930a0730df36bbb874f7baa62161f1c0008876925ae021f0183486982b2e
|
|
7
|
+
data.tar.gz: 7545e0f237b64c38aac6b414e0ccf9c305f07d6d3f1c7b0b40833b58b26fd0045319be478f2d68c02e8652dd48ef47e696cf2b5a217c122dc9943754fa831db9
|
metadata
CHANGED
|
@@ -1,35 +1,21 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sendmux-core
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
7
|
+
- Qubrics
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-
|
|
10
|
+
date: 2026-11-19 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
|
-
name:
|
|
14
|
-
requirement: !ruby/object:Gem::Requirement
|
|
15
|
-
requirements:
|
|
16
|
-
- - "~>"
|
|
17
|
-
- !ruby/object:Gem::Version
|
|
18
|
-
version: '2.0'
|
|
19
|
-
type: :runtime
|
|
20
|
-
prerelease: false
|
|
21
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
-
requirements:
|
|
23
|
-
- - "~>"
|
|
24
|
-
- !ruby/object:Gem::Version
|
|
25
|
-
version: '2.0'
|
|
26
|
-
- !ruby/object:Gem::Dependency
|
|
27
|
-
name: faraday-retry
|
|
13
|
+
name: qubrics-core
|
|
28
14
|
requirement: !ruby/object:Gem::Requirement
|
|
29
15
|
requirements:
|
|
30
16
|
- - ">="
|
|
31
17
|
- !ruby/object:Gem::Version
|
|
32
|
-
version:
|
|
18
|
+
version: 2.0.0
|
|
33
19
|
- - "<"
|
|
34
20
|
- !ruby/object:Gem::Version
|
|
35
21
|
version: '3.0'
|
|
@@ -39,35 +25,19 @@ dependencies:
|
|
|
39
25
|
requirements:
|
|
40
26
|
- - ">="
|
|
41
27
|
- !ruby/object:Gem::Version
|
|
42
|
-
version:
|
|
28
|
+
version: 2.0.0
|
|
43
29
|
- - "<"
|
|
44
30
|
- !ruby/object:Gem::Version
|
|
45
31
|
version: '3.0'
|
|
46
|
-
|
|
47
|
-
- contact@sendmux.ai
|
|
32
|
+
description: Installs the required dependency.
|
|
48
33
|
executables: []
|
|
49
34
|
extensions: []
|
|
50
35
|
extra_rdoc_files: []
|
|
51
|
-
files:
|
|
52
|
-
|
|
53
|
-
- LICENSE
|
|
54
|
-
- README.md
|
|
55
|
-
- lib/sendmux/core.rb
|
|
56
|
-
- lib/sendmux/core/api_key_surface.rb
|
|
57
|
-
- lib/sendmux/core/auth.rb
|
|
58
|
-
- lib/sendmux/core/errors.rb
|
|
59
|
-
- lib/sendmux/core/headers.rb
|
|
60
|
-
- lib/sendmux/core/pagination.rb
|
|
61
|
-
- lib/sendmux/core/retry.rb
|
|
62
|
-
- lib/sendmux/core/retry_options.rb
|
|
63
|
-
- lib/sendmux/core/version.rb
|
|
64
|
-
homepage: https://github.com/Sendmux/sendmux-sdk
|
|
36
|
+
files: []
|
|
37
|
+
homepage: https://github.com/qubrics/qubrics-sdk
|
|
65
38
|
licenses:
|
|
66
39
|
- MIT
|
|
67
|
-
metadata:
|
|
68
|
-
homepage_uri: https://github.com/Sendmux/sendmux-sdk
|
|
69
|
-
source_code_uri: https://github.com/Sendmux/sendmux-sdk/tree/main/packages/ruby/core
|
|
70
|
-
changelog_uri: https://github.com/Sendmux/sendmux-sdk/blob/main/packages/ruby/core/CHANGELOG.md
|
|
40
|
+
metadata: {}
|
|
71
41
|
rdoc_options: []
|
|
72
42
|
require_paths:
|
|
73
43
|
- lib
|
|
@@ -84,5 +54,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
84
54
|
requirements: []
|
|
85
55
|
rubygems_version: 3.6.2
|
|
86
56
|
specification_version: 4
|
|
87
|
-
summary:
|
|
57
|
+
summary: Dependency-only package.
|
|
88
58
|
test_files: []
|
data/CHANGELOG.md
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## [1.2.0](https://github.com/Sendmux/sendmux-sdk/compare/ruby-core/v1.1.0...ruby-core/v1.2.0) (2026-09-08)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
### Features
|
|
7
|
-
|
|
8
|
-
* add connection checks across SDKs, CLI and MCP ([77f449e](https://github.com/Sendmux/sendmux-sdk/commit/77f449e2c8cba49bc8bc84c49c35baa61120866a))
|
|
9
|
-
* **sdk:** add connection checks across clients, CLI and MCP ([3e02c67](https://github.com/Sendmux/sendmux-sdk/commit/3e02c67dcab45e37dad7abbdd4c4e1bd0b1fbbe6))
|
|
10
|
-
|
|
11
|
-
## [1.1.0](https://github.com/Sendmux/sendmux-sdk/compare/ruby-core/v1.0.0...ruby-core/v1.1.0) (2026-07-01)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
### Features
|
|
15
|
-
|
|
16
|
-
* allow owner-approved agent tokens for sending ([9d1cb7d](https://github.com/Sendmux/sendmux-sdk/commit/9d1cb7df3df5aef1f59a4990dc087178ba3a7b21))
|
|
17
|
-
|
|
18
|
-
## 1.0.0 (2026-06-02)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
### Features
|
|
22
|
-
|
|
23
|
-
* **ruby:** add generated SDK packages ([f097fdf](https://github.com/Sendmux/sendmux-sdk/commit/f097fdf6afcbc2a048d9fdb1c9a669fff2a7ca4f))
|
|
24
|
-
|
|
25
|
-
## 1.0.0
|
|
26
|
-
|
|
27
|
-
- Initial Ruby core helper package.
|
data/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Sendmux
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|
data/README.md
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
# sendmux-core
|
|
2
|
-
|
|
3
|
-
[](https://rubygems.org/gems/sendmux-core)
|
|
4
|
-
[](https://github.com/Sendmux/sendmux-sdk/actions/workflows/ci.yml)
|
|
5
|
-
[](https://github.com/Sendmux/sendmux-sdk/blob/main/LICENSE)
|
|
6
|
-
|
|
7
|
-
Shared authentication, retry, pagination, header, and error helpers for the Sendmux Ruby SDK packages.
|
|
8
|
-
|
|
9
|
-
## Documentation
|
|
10
|
-
|
|
11
|
-
- Ruby SDK guide: https://sendmux.ai/docs/sdks/ruby
|
|
12
|
-
- Source: https://github.com/Sendmux/sendmux-sdk/tree/main/packages/ruby/core
|
|
13
|
-
- Changelog: https://github.com/Sendmux/sendmux-sdk/blob/main/packages/ruby/core/CHANGELOG.md
|
|
14
|
-
|
|
15
|
-
## Requirements
|
|
16
|
-
|
|
17
|
-
- Ruby 3.1 or newer.
|
|
18
|
-
- A Sendmux API key when using the helpers with a surface client.
|
|
19
|
-
|
|
20
|
-
## Installation
|
|
21
|
-
|
|
22
|
-
```sh
|
|
23
|
-
gem install sendmux-core
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
Or add it to your Gemfile:
|
|
27
|
-
|
|
28
|
-
```ruby
|
|
29
|
-
gem "sendmux-core", "~> 1.0"
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
## Usage
|
|
33
|
-
|
|
34
|
-
The core package is installed automatically by the surface gems, but it can also be used directly when you need shared helpers.
|
|
35
|
-
|
|
36
|
-
```ruby
|
|
37
|
-
require "sendmux/core"
|
|
38
|
-
|
|
39
|
-
surface = Sendmux::Core::Auth.assert_api_key_surface(
|
|
40
|
-
ENV.fetch("SENDMUX_ROOT_KEY"),
|
|
41
|
-
Sendmux::Core::ApiKeySurface::ROOT
|
|
42
|
-
)
|
|
43
|
-
|
|
44
|
-
puts surface
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
Use `Sendmux::Core::ApiKeySurface::ROOT` for root-key management clients, `Sendmux::Core::ApiKeySurface::SENDING` for send-capable `smx_mbx_` keys or owner-approved Sending-resource `smx_agent_` tokens, and `Sendmux::Core::ApiKeySurface::MAILBOX` for mailbox-compatible credentials. Mailbox-compatible credentials can start with `smx_mbx_` or `smx_agent_`.
|
|
48
|
-
|
|
49
|
-
### Header helpers
|
|
50
|
-
|
|
51
|
-
Generated Ruby operations accept option hashes. The header helpers return the option keys expected by those generated operations.
|
|
52
|
-
|
|
53
|
-
```ruby
|
|
54
|
-
headers = Sendmux::Core::Headers.idempotency_key("idem_123")
|
|
55
|
-
etag = Sendmux::Core::Headers.if_match('W/"etag"')
|
|
56
|
-
conditional = Sendmux::Core::Headers.conditional(if_none_match: 'W/"cached"')
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
### Cursor pagination
|
|
60
|
-
|
|
61
|
-
`Sendmux::Core.each_cursor` wraps any generated list call that accepts a cursor option and returns Sendmux pagination metadata.
|
|
62
|
-
|
|
63
|
-
```ruby
|
|
64
|
-
pager = Sendmux::Core.each_cursor(lambda do |opts|
|
|
65
|
-
mailbox_client.mailbox_api.mailbox_list_messages(opts.merge(limit: 50))
|
|
66
|
-
end)
|
|
67
|
-
|
|
68
|
-
pager.each do |message|
|
|
69
|
-
puts message.id
|
|
70
|
-
end
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
### Retries and errors
|
|
74
|
-
|
|
75
|
-
Surface clients accept `Sendmux::Core::RetryOptions` and map generated errors to `Sendmux::Core::ApiError`.
|
|
76
|
-
|
|
77
|
-
```ruby
|
|
78
|
-
retry_options = Sendmux::Core::RetryOptions.new(max_attempts: 4)
|
|
79
|
-
|
|
80
|
-
begin
|
|
81
|
-
# Pass retry_options: retry_options to any Sendmux surface client.
|
|
82
|
-
# Call a generated operation here.
|
|
83
|
-
nil
|
|
84
|
-
rescue Sendmux::Core::ApiError => error
|
|
85
|
-
warn "#{error.code}: #{error.message} (request #{error.request_id})"
|
|
86
|
-
end
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
Retries are enabled for safe requests and for idempotent `POST` requests that use an `Idempotency-Key`.
|
|
90
|
-
|
|
91
|
-
## Support
|
|
92
|
-
|
|
93
|
-
- Documentation: https://sendmux.ai/docs
|
|
94
|
-
- Contact: contact@sendmux.ai
|
|
95
|
-
|
|
96
|
-
## Licence
|
|
97
|
-
|
|
98
|
-
MIT licence. See https://github.com/Sendmux/sendmux-sdk/blob/main/LICENSE.
|
data/lib/sendmux/core/auth.rb
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'uri'
|
|
4
|
-
|
|
5
|
-
module Sendmux
|
|
6
|
-
module Core
|
|
7
|
-
class Auth
|
|
8
|
-
ROOT_PREFIX = 'smx_root_'
|
|
9
|
-
MAILBOX_PREFIX = 'smx_mbx_'
|
|
10
|
-
AGENT_PREFIX = 'smx_agent_'
|
|
11
|
-
|
|
12
|
-
def self.configure_bearer(configuration, api_key, expected_surface, base_url: nil)
|
|
13
|
-
assert_api_key_surface(api_key, expected_surface)
|
|
14
|
-
unless configuration.respond_to?(:access_token=)
|
|
15
|
-
raise ArgumentError, 'Generated configuration does not support bearer access tokens'
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
configuration.access_token = api_key
|
|
19
|
-
configure_base_url(configuration, base_url) if base_url && !base_url.empty?
|
|
20
|
-
configuration
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def self.assert_api_key_surface(api_key, expected_surface)
|
|
24
|
-
actual = surface_for(api_key)
|
|
25
|
-
unless actual
|
|
26
|
-
raise ArgumentError,
|
|
27
|
-
"Sendmux API keys must start with #{ROOT_PREFIX}, #{MAILBOX_PREFIX}, or #{AGENT_PREFIX}"
|
|
28
|
-
end
|
|
29
|
-
return actual if compatible_surface?(api_key, actual, expected_surface)
|
|
30
|
-
|
|
31
|
-
raise ArgumentError, "Expected a #{expected_surface} API key, received a #{actual} API key"
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def self.compatible_surface?(api_key, actual, expected_surface)
|
|
35
|
-
return true if actual == expected_surface
|
|
36
|
-
return true if expected_surface == ApiKeySurface::SENDING &&
|
|
37
|
-
(api_key.start_with?(MAILBOX_PREFIX) || api_key.start_with?(AGENT_PREFIX))
|
|
38
|
-
return true if expected_surface == ApiKeySurface::MAILBOX && actual == ApiKeySurface::MAILBOX
|
|
39
|
-
|
|
40
|
-
false
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def self.surface_for(api_key)
|
|
44
|
-
return ApiKeySurface::ROOT if api_key.start_with?(ROOT_PREFIX)
|
|
45
|
-
return ApiKeySurface::MAILBOX if api_key.start_with?(MAILBOX_PREFIX)
|
|
46
|
-
return ApiKeySurface::MAILBOX if api_key.start_with?(AGENT_PREFIX)
|
|
47
|
-
|
|
48
|
-
nil
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def self.configure_base_url(configuration, base_url)
|
|
52
|
-
uri = URI(base_url)
|
|
53
|
-
raise ArgumentError, 'base_url must include a scheme and host' unless uri.scheme && uri.host
|
|
54
|
-
|
|
55
|
-
configuration.scheme = uri.scheme
|
|
56
|
-
configuration.host = uri.host
|
|
57
|
-
configuration.host += ":#{uri.port}" if uri.port && uri.port != uri.default_port
|
|
58
|
-
configuration.base_path = uri.path
|
|
59
|
-
configuration.ignore_operation_servers = true if configuration.respond_to?(:ignore_operation_servers=)
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
end
|
data/lib/sendmux/core/errors.rb
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'json'
|
|
4
|
-
|
|
5
|
-
module Sendmux
|
|
6
|
-
module Core
|
|
7
|
-
class ApiError < StandardError
|
|
8
|
-
attr_reader :status, :code, :request_id, :param, :retryable, :errors, :response_body, :response_headers
|
|
9
|
-
|
|
10
|
-
def initialize(status:, code:, message:, retryable:, request_id: nil, param: nil, errors: nil,
|
|
11
|
-
response_body: nil, response_headers: {})
|
|
12
|
-
super(message)
|
|
13
|
-
@status = status
|
|
14
|
-
@code = code
|
|
15
|
-
@request_id = request_id
|
|
16
|
-
@param = param
|
|
17
|
-
@retryable = retryable
|
|
18
|
-
@errors = errors
|
|
19
|
-
@response_body = response_body
|
|
20
|
-
@response_headers = response_headers
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
class ErrorMapper
|
|
25
|
-
def self.map(error)
|
|
26
|
-
return error if error.is_a?(ApiError)
|
|
27
|
-
|
|
28
|
-
headers = normalise_headers(error.respond_to?(:response_headers) ? error.response_headers : {})
|
|
29
|
-
body = error.respond_to?(:response_body) ? error.response_body : nil
|
|
30
|
-
payload = payload_from(body)
|
|
31
|
-
detail = hash_at(payload, 'error') || {}
|
|
32
|
-
status = status_from(error)
|
|
33
|
-
|
|
34
|
-
ApiError.new(
|
|
35
|
-
status: status,
|
|
36
|
-
code: string_at(detail, 'code') || 'api_error',
|
|
37
|
-
message: string_at(detail, 'message') || error.message || 'Sendmux API request failed',
|
|
38
|
-
retryable: retryable(detail, status),
|
|
39
|
-
request_id: request_id(payload, headers),
|
|
40
|
-
param: string_at(detail, 'param'),
|
|
41
|
-
errors: detail['errors'],
|
|
42
|
-
response_body: body,
|
|
43
|
-
response_headers: headers
|
|
44
|
-
)
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
def self.status_from(error)
|
|
48
|
-
return error.code if error.respond_to?(:code) && error.code.is_a?(Integer) && error.code.positive?
|
|
49
|
-
|
|
50
|
-
nil
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def self.payload_from(body)
|
|
54
|
-
return nil unless body.is_a?(String) && !body.empty?
|
|
55
|
-
|
|
56
|
-
JSON.parse(body)
|
|
57
|
-
rescue JSON::ParserError
|
|
58
|
-
nil
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def self.request_id(payload, headers)
|
|
62
|
-
string_at(hash_at(payload, 'meta'), 'request_id') || header(headers, 'x-request-id')
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def self.normalise_headers(headers)
|
|
66
|
-
return {} unless headers.respond_to?(:each)
|
|
67
|
-
|
|
68
|
-
headers.each_with_object({}) do |(key, value), result|
|
|
69
|
-
result[key.to_s] = value.is_a?(Array) ? value.join(', ') : value.to_s
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
def self.hash_at(value, key)
|
|
74
|
-
child = value[key] if value.is_a?(Hash)
|
|
75
|
-
child.is_a?(Hash) ? child : nil
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def self.string_at(value, key)
|
|
79
|
-
child = value[key] if value.is_a?(Hash)
|
|
80
|
-
child.is_a?(String) && !child.empty? ? child : nil
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
def self.bool_at(value, key)
|
|
84
|
-
child = value[key] if value.is_a?(Hash)
|
|
85
|
-
[true, false].include?(child) ? child : nil
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
def self.retryable(detail, status)
|
|
89
|
-
explicit = bool_at(detail, 'retryable')
|
|
90
|
-
explicit.nil? ? default_retryable?(status) : explicit
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
def self.header(headers, name)
|
|
94
|
-
pair = headers.find { |key, _value| key.downcase == name }
|
|
95
|
-
pair&.last
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
def self.default_retryable?(status)
|
|
99
|
-
status == 429 || (status.is_a?(Integer) && status >= 500)
|
|
100
|
-
end
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
end
|
data/lib/sendmux/core/headers.rb
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Sendmux
|
|
4
|
-
module Core
|
|
5
|
-
module Headers
|
|
6
|
-
def self.idempotency_key(value)
|
|
7
|
-
{ idempotency_key: value }
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def self.if_match(value)
|
|
11
|
-
{ if_match: value }
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def self.if_none_match(value)
|
|
15
|
-
{ if_none_match: value }
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def self.conditional(if_match: nil, if_none_match: nil)
|
|
19
|
-
{}.tap do |headers|
|
|
20
|
-
headers[:if_match] = if_match if if_match
|
|
21
|
-
headers[:if_none_match] = if_none_match if if_none_match
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Sendmux
|
|
4
|
-
module Core
|
|
5
|
-
class CursorPager
|
|
6
|
-
include Enumerable
|
|
7
|
-
|
|
8
|
-
def initialize(fetch_page, cursor_param: :cursor)
|
|
9
|
-
@fetch_page = fetch_page
|
|
10
|
-
@cursor_param = cursor_param
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def each(&block)
|
|
14
|
-
return enum_for(:each) unless block_given?
|
|
15
|
-
|
|
16
|
-
cursor = nil
|
|
17
|
-
loop do
|
|
18
|
-
response = @fetch_page.call(cursor ? { @cursor_param => cursor } : {})
|
|
19
|
-
extract_items(response).each(&block)
|
|
20
|
-
pagination = extract_pagination(response)
|
|
21
|
-
break unless bool_value?(pagination, 'has_more')
|
|
22
|
-
|
|
23
|
-
cursor = value(pagination, 'next_cursor')
|
|
24
|
-
break unless cursor
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
private
|
|
29
|
-
|
|
30
|
-
def extract_items(response)
|
|
31
|
-
data = value(response, 'data')
|
|
32
|
-
return data if data.is_a?(Array)
|
|
33
|
-
|
|
34
|
-
[]
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def extract_pagination(response)
|
|
38
|
-
meta = value(response, 'meta')
|
|
39
|
-
value(meta, 'pagination') || value(response, 'pagination') || {}
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def bool_value?(object, name)
|
|
43
|
-
value(object, name) == true
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def value(object, name)
|
|
47
|
-
return object[name] || object[name.to_sym] if object.is_a?(Hash)
|
|
48
|
-
return object.public_send(name) if object.respond_to?(name)
|
|
49
|
-
|
|
50
|
-
method = snake_to_camel(name)
|
|
51
|
-
object.public_send(method) if object.respond_to?(method)
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def snake_to_camel(name)
|
|
55
|
-
parts = name.to_s.split('_')
|
|
56
|
-
parts.first + parts.drop(1).map(&:capitalize).join
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def self.each_cursor(fetch_page, cursor_param: :cursor)
|
|
61
|
-
CursorPager.new(fetch_page, cursor_param: cursor_param)
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
end
|
data/lib/sendmux/core/retry.rb
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'faraday/retry'
|
|
4
|
-
|
|
5
|
-
module Sendmux
|
|
6
|
-
module Core
|
|
7
|
-
module Retry
|
|
8
|
-
SAFE_METHODS = %w[GET HEAD OPTIONS].freeze
|
|
9
|
-
|
|
10
|
-
def self.configure(configuration, retry_options = nil)
|
|
11
|
-
options = retry_options || RetryOptions.new
|
|
12
|
-
configuration.request(:retry, options.to_faraday_options)
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def self.retryable_request?(env)
|
|
16
|
-
method = env[:method].to_s.upcase
|
|
17
|
-
return true if SAFE_METHODS.include?(method)
|
|
18
|
-
|
|
19
|
-
method == 'POST' && header(env[:request_headers] || {}, 'Idempotency-Key') && replayable_body?(env[:body])
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def self.header(headers, name)
|
|
23
|
-
headers.find { |key, _value| key.to_s.downcase == name.downcase }&.last
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def self.replayable_body?(body)
|
|
27
|
-
body.nil? || body.is_a?(String) || body.respond_to?(:rewind)
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
end
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Sendmux
|
|
4
|
-
module Core
|
|
5
|
-
class RetryOptions
|
|
6
|
-
RETRY_STATUSES = [408, 409, 425, 429, 500, 502, 503, 504].freeze
|
|
7
|
-
|
|
8
|
-
attr_reader :max_attempts, :base_delay_seconds, :max_delay_seconds, :jitter
|
|
9
|
-
|
|
10
|
-
def initialize(max_attempts: 3, base_delay_seconds: 0.25, max_delay_seconds: 5.0, jitter: true)
|
|
11
|
-
raise ArgumentError, 'max_attempts must be at least 1' if max_attempts < 1
|
|
12
|
-
if base_delay_seconds.negative? || max_delay_seconds.negative?
|
|
13
|
-
raise ArgumentError,
|
|
14
|
-
'retry delays must be non-negative'
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
@max_attempts = max_attempts
|
|
18
|
-
@base_delay_seconds = base_delay_seconds
|
|
19
|
-
@max_delay_seconds = max_delay_seconds
|
|
20
|
-
@jitter = jitter
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def to_faraday_options
|
|
24
|
-
{
|
|
25
|
-
max: max_attempts - 1,
|
|
26
|
-
interval: base_delay_seconds,
|
|
27
|
-
max_interval: max_delay_seconds,
|
|
28
|
-
interval_randomness: jitter ? 0.5 : 0.0,
|
|
29
|
-
backoff_factor: 2,
|
|
30
|
-
methods: [],
|
|
31
|
-
retry_statuses: RETRY_STATUSES,
|
|
32
|
-
rate_limit_retry_header: 'Retry-After',
|
|
33
|
-
rate_limit_reset_header: 'X-RateLimit-Reset',
|
|
34
|
-
retry_if: ->(env, _exception) { Retry.retryable_request?(env) }
|
|
35
|
-
}
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
end
|
data/lib/sendmux/core/version.rb
DELETED
data/lib/sendmux/core.rb
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'sendmux/core/api_key_surface'
|
|
4
|
-
require 'sendmux/core/auth'
|
|
5
|
-
require 'sendmux/core/errors'
|
|
6
|
-
require 'sendmux/core/headers'
|
|
7
|
-
require 'sendmux/core/pagination'
|
|
8
|
-
require 'sendmux/core/retry'
|
|
9
|
-
require 'sendmux/core/retry_options'
|
|
10
|
-
require 'sendmux/core/version'
|