helo-email-sdk 1.4.0 → 2.1.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/README.md +29 -36
- data/lib/helo/apis/sending.rb +0 -4
- data/lib/helo/configuration.rb +1 -1
- data/lib/helo/core/client.rb +20 -71
- data/lib/helo/core/configuration.rb +3 -7
- data/lib/helo/enums/batch_validation_mode.rb +12 -0
- data/lib/helo/models/send_message_batch_request.rb +1 -0
- data/lib/helo/models/send_message_failed_response.rb +1 -0
- data/lib/helo/version.rb +1 -1
- metadata +16 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f7a6aed6efad7398a95797b7e9fc53c03e2d8a1ce111fd0a2db2c08690f3657e
|
|
4
|
+
data.tar.gz: 1846b059a58aadba3f2c2f8fb1d9b9f4e238921d20e8b0335012d44449d550db
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 002b7f03d1cf4c6b4bceda0439b9fbfc3a8ab7108134356cbedd25109d4e75a9f0dcbae22bde080e353e48f4a01ae881dea8d5c30ccdf9d6b9bc1de47b7ee552
|
|
7
|
+
data.tar.gz: 33a4520906168a841abf7183673783a5cfedc7ccaeb3ef3d7958e54c659dd53425655969b52122a57b829e30ea6711bf2ee35f5d036114058fac4461c799e6e7
|
data/README.md
CHANGED
|
@@ -28,28 +28,6 @@ Helo.configure do |config|
|
|
|
28
28
|
end
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
## Automatic retries
|
|
32
|
-
|
|
33
|
-
A request that fails with a network error, a timeout, or a `408`, `429`, `500`, `502`, `503` or
|
|
34
|
-
`504` response is retried up to 2 times, with exponential backoff and full jitter. It
|
|
35
|
-
is only retried when sending it again can't do anything twice: a `GET`, `PUT` or `DELETE`,
|
|
36
|
-
or a request carrying an idempotency key. A `429` is always retried, since the server
|
|
37
|
-
refused the request without acting on it. A `Retry-After` header is honoured; one asking
|
|
38
|
-
for more than 8 seconds is not retried early, and the error is returned instead.
|
|
39
|
-
|
|
40
|
-
Operations that take an idempotency key generate one for each call when you don't pass
|
|
41
|
-
one, and send it on every retry, so a send whose response was lost is never sent twice.
|
|
42
|
-
Pass your own key to make your own retries (a background job run again, say) safe too.
|
|
43
|
-
|
|
44
|
-
Set `config.max_retries` to change how many, or to `0` to disable them:
|
|
45
|
-
|
|
46
|
-
```ruby
|
|
47
|
-
Helo.configure do |config|
|
|
48
|
-
config.api_key = ENV.fetch("HELO_API_KEY")
|
|
49
|
-
config.max_retries = 0
|
|
50
|
-
end
|
|
51
|
-
```
|
|
52
|
-
|
|
53
31
|
## Usage
|
|
54
32
|
|
|
55
33
|
Each API is a class with class-level methods. Responses come back as model objects with
|
|
@@ -67,20 +45,6 @@ result = Helo::Channels.list(limit: 10)
|
|
|
67
45
|
|
|
68
46
|
See the [API docs](#apis) for every method, with a runnable example each.
|
|
69
47
|
|
|
70
|
-
### Errors
|
|
71
|
-
|
|
72
|
-
Failed requests raise `Helo::APIError`:
|
|
73
|
-
|
|
74
|
-
```ruby
|
|
75
|
-
begin
|
|
76
|
-
Helo::Channels.list
|
|
77
|
-
rescue Helo::APIError => e
|
|
78
|
-
e.code # HTTP status
|
|
79
|
-
e.detail # human-readable detail from the API
|
|
80
|
-
e.errors # field-level validation errors, when present
|
|
81
|
-
end
|
|
82
|
-
```
|
|
83
|
-
|
|
84
48
|
## Rails (ActionMailer)
|
|
85
49
|
|
|
86
50
|
The gem registers a `:helo` delivery method with ActionMailer, so existing
|
|
@@ -127,6 +91,35 @@ end
|
|
|
127
91
|
After delivery, `message[:helo_message_id].value` is the id the API assigned the
|
|
128
92
|
message, and failed requests raise `Helo::APIError` (subject to `config.action_mailer.raise_delivery_errors`).
|
|
129
93
|
|
|
94
|
+
## Errors
|
|
95
|
+
|
|
96
|
+
Failed requests raise `Helo::APIError`:
|
|
97
|
+
|
|
98
|
+
```ruby
|
|
99
|
+
begin
|
|
100
|
+
Helo::Channels.list
|
|
101
|
+
rescue Helo::APIError => e
|
|
102
|
+
e.code # HTTP status
|
|
103
|
+
e.detail # human-readable detail from the API
|
|
104
|
+
e.errors # field-level validation errors, when present
|
|
105
|
+
end
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Automatic retries
|
|
109
|
+
|
|
110
|
+
A request that fails with a network error, a timeout, or a `408`, `429` or `5xx` response is
|
|
111
|
+
retried up to 2 times with backoff, when sending it again is safe. Operations that take an
|
|
112
|
+
idempotency key generate one if you don't pass it, so a retried send is never delivered twice.
|
|
113
|
+
|
|
114
|
+
Retries use [faraday-retry](https://github.com/lostisland/faraday-retry); `config.retry_options`
|
|
115
|
+
overrides its options:
|
|
116
|
+
|
|
117
|
+
```ruby
|
|
118
|
+
Helo.configure do |config|
|
|
119
|
+
config.retry_options = { max: 0 } # no retries
|
|
120
|
+
end
|
|
121
|
+
```
|
|
122
|
+
|
|
130
123
|
## Webhook signature verification
|
|
131
124
|
|
|
132
125
|
Webhook deliveries are signed with the endpoint's signing key. Verify every delivery before
|
data/lib/helo/apis/sending.rb
CHANGED
|
@@ -6,7 +6,6 @@ module Helo
|
|
|
6
6
|
request = SendMessageRequest.new(request_data)
|
|
7
7
|
headers = {}
|
|
8
8
|
headers["X-Helo-Channel-Id"] = channel_id unless channel_id.nil?
|
|
9
|
-
# Generated once per call, so a retry is recognized as the same request.
|
|
10
9
|
headers["X-Helo-Idempotency-Key"] = idempotency_key || Client.idempotency_key
|
|
11
10
|
response = @client.request(:post, "/send/transactional", body: request.to_params, headers: headers)
|
|
12
11
|
SendMessageAcceptedResponse.from_hash(response.body)
|
|
@@ -16,7 +15,6 @@ module Helo
|
|
|
16
15
|
request = SendMessageBatchRequest.new(request_data)
|
|
17
16
|
headers = {}
|
|
18
17
|
headers["X-Helo-Channel-Id"] = channel_id unless channel_id.nil?
|
|
19
|
-
# Generated once per call, so a retry is recognized as the same request.
|
|
20
18
|
headers["X-Helo-Idempotency-Key"] = idempotency_key || Client.idempotency_key
|
|
21
19
|
response = @client.request(:post, "/send/transactional/batch", body: request.to_params, headers: headers)
|
|
22
20
|
SendMessageBatchResponse.from_hash(response.body)
|
|
@@ -26,7 +24,6 @@ module Helo
|
|
|
26
24
|
request = SendBroadcastRequest.new(request_data)
|
|
27
25
|
headers = {}
|
|
28
26
|
headers["X-Helo-Channel-Id"] = channel_id unless channel_id.nil?
|
|
29
|
-
# Generated once per call, so a retry is recognized as the same request.
|
|
30
27
|
headers["X-Helo-Idempotency-Key"] = idempotency_key || Client.idempotency_key
|
|
31
28
|
response = @client.request(:post, "/send/broadcast", body: request.to_params, headers: headers)
|
|
32
29
|
SendBroadcastResponse.from_hash(response.body)
|
|
@@ -36,7 +33,6 @@ module Helo
|
|
|
36
33
|
request = SendMessageRequest.new(request_data)
|
|
37
34
|
headers = {}
|
|
38
35
|
headers["X-Helo-Channel-Id"] = channel_id unless channel_id.nil?
|
|
39
|
-
# Generated once per call, so a retry is recognized as the same request.
|
|
40
36
|
headers["X-Helo-Idempotency-Key"] = idempotency_key || Client.idempotency_key
|
|
41
37
|
response = @client.request(:post, "/send/broadcast/message", body: request.to_params, headers: headers)
|
|
42
38
|
SendMessageAcceptedResponse.from_hash(response.body)
|
data/lib/helo/configuration.rb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module Helo
|
|
4
4
|
class Configuration < Helo::Core::Configuration
|
|
5
|
-
def initialize(base_url: "https://api.helohq.com", api_key: nil,
|
|
5
|
+
def initialize(base_url: "https://api.helohq.com", api_key: nil, retry_options: {})
|
|
6
6
|
super
|
|
7
7
|
end
|
|
8
8
|
end
|
data/lib/helo/core/client.rb
CHANGED
|
@@ -1,28 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "faraday"
|
|
4
|
+
require "faraday/retry"
|
|
4
5
|
require "securerandom"
|
|
5
|
-
require "time"
|
|
6
6
|
|
|
7
7
|
module Helo::Core
|
|
8
|
-
# Sends requests, retrying (up to configuration.max_retries times, with jittered
|
|
9
|
-
# backoff) after a network failure or a 408, 429 or 5xx response, but only when
|
|
10
|
-
# repeating the request cannot do anything twice: its method is idempotent, or
|
|
11
|
-
# it carries an idempotency key. A 429 is retried regardless, since the server
|
|
12
|
-
# refused it without acting on it. A Retry-After longer than the SDK waits is
|
|
13
|
-
# not retried early; the error goes to the caller.
|
|
14
|
-
#
|
|
15
|
-
# The same policy as every Helo SDK (Helo::SdkGeneratorCommon::Retries in
|
|
16
|
-
# helo-sdk-generator); keep these numbers in step with it.
|
|
17
8
|
class Client
|
|
18
|
-
INITIAL_RETRY_DELAY = 0.5
|
|
19
|
-
MAX_RETRY_DELAY = 8.0
|
|
20
|
-
RETRY_STATUSES = [ 408, 429, 500, 502, 503, 504 ].freeze
|
|
21
|
-
IDEMPOTENT_METHODS = %i[get head options put delete].freeze
|
|
22
|
-
|
|
23
9
|
attr_reader :configuration
|
|
24
10
|
|
|
25
|
-
# A random UUID, for an idempotency key the caller didn't supply.
|
|
26
11
|
def self.idempotency_key
|
|
27
12
|
SecureRandom.uuid
|
|
28
13
|
end
|
|
@@ -34,33 +19,12 @@ module Helo::Core
|
|
|
34
19
|
end
|
|
35
20
|
|
|
36
21
|
def request(method, path, params: {}, body: nil, headers: {})
|
|
37
|
-
|
|
38
|
-
headers.keys.any? { |name| name.to_s.downcase.end_with?("idempotency-key") }
|
|
39
|
-
attempt = 0
|
|
40
|
-
|
|
41
|
-
loop do
|
|
42
|
-
response = begin
|
|
43
|
-
send_request(method, path, params, body, headers)
|
|
44
|
-
rescue Faraday::ConnectionFailed, Faraday::TimeoutError
|
|
45
|
-
raise unless can_repeat && attempt < max_retries
|
|
46
|
-
|
|
47
|
-
sleep(backoff_delay(attempt))
|
|
48
|
-
attempt += 1
|
|
49
|
-
next
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
delay = retry_delay(response, attempt, can_repeat)
|
|
53
|
-
return handle_response(response) unless delay
|
|
54
|
-
|
|
55
|
-
sleep(delay)
|
|
56
|
-
attempt += 1
|
|
57
|
-
end
|
|
22
|
+
handle_response(send_request(method, path, params, body, headers))
|
|
58
23
|
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
|
|
59
24
|
handle_connection_error(e)
|
|
60
25
|
end
|
|
61
26
|
|
|
62
|
-
#
|
|
63
|
-
# was generated for; Helo::Core on its own has no package to name.
|
|
27
|
+
# Overridden by the generated Client.
|
|
64
28
|
def user_agent
|
|
65
29
|
nil
|
|
66
30
|
end
|
|
@@ -72,60 +36,45 @@ module Helo::Core
|
|
|
72
36
|
req.params = params
|
|
73
37
|
req.body = body
|
|
74
38
|
|
|
75
|
-
# Set Authorization header with dynamic or static token
|
|
76
39
|
token = current_api_key
|
|
77
40
|
req.headers["Authorization"] = "Bearer #{token}" if token
|
|
78
41
|
|
|
79
42
|
req.headers["User-Agent"] = user_agent if user_agent
|
|
80
43
|
|
|
81
|
-
# Apply per-request headers verbatim (already in wire form)
|
|
82
44
|
headers.each do |key, value|
|
|
83
45
|
req.headers[key.to_s] = value.to_s
|
|
84
46
|
end
|
|
85
47
|
end
|
|
86
48
|
end
|
|
87
49
|
|
|
88
|
-
def
|
|
89
|
-
|
|
50
|
+
def retry_options
|
|
51
|
+
{
|
|
52
|
+
max: 2,
|
|
53
|
+
interval: 0.5,
|
|
54
|
+
backoff_factor: 2,
|
|
55
|
+
interval_randomness: 0.5,
|
|
56
|
+
max_interval: 8.0,
|
|
57
|
+
retry_statuses: [ 408, 429, 500, 502, 503, 504 ],
|
|
58
|
+
exceptions: [ Faraday::ConnectionFailed, Faraday::TimeoutError, Faraday::RetriableResponse ],
|
|
59
|
+
methods: %i[get head options put delete],
|
|
60
|
+
retry_if: method(:retry_non_idempotent?)
|
|
61
|
+
}.merge(configuration.retry_options || {})
|
|
90
62
|
end
|
|
91
63
|
|
|
92
|
-
#
|
|
93
|
-
def
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return nil if status != 429 && !can_repeat
|
|
97
|
-
|
|
98
|
-
retry_after = retry_after_seconds(response)
|
|
99
|
-
return backoff_delay(attempt) if retry_after.nil?
|
|
100
|
-
|
|
101
|
-
retry_after <= MAX_RETRY_DELAY ? retry_after : nil
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
# Exponential backoff with full jitter.
|
|
105
|
-
def backoff_delay(attempt)
|
|
106
|
-
rand * [ MAX_RETRY_DELAY, INITIAL_RETRY_DELAY * (2**attempt) ].min
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
# Retry-After as seconds, from either a number or an HTTP date.
|
|
110
|
-
def retry_after_seconds(response)
|
|
111
|
-
value = response.headers["retry-after"].to_s
|
|
112
|
-
return nil if value.empty?
|
|
113
|
-
return [ Float(value), 0.0 ].max if value.match?(/\A\d+(\.\d+)?\z/)
|
|
114
|
-
|
|
115
|
-
delay = Time.httpdate(value) - Time.now
|
|
116
|
-
delay.positive? ? delay : nil
|
|
117
|
-
rescue ArgumentError
|
|
118
|
-
nil
|
|
64
|
+
# Only asked about methods outside `methods`.
|
|
65
|
+
def retry_non_idempotent?(env, exception)
|
|
66
|
+
rate_limited = exception.is_a?(Faraday::RetriableResponse) && exception.response.status == 429
|
|
67
|
+
rate_limited || env.request_headers.keys.any? { |name| name.downcase.end_with?("idempotency-key") }
|
|
119
68
|
end
|
|
120
69
|
|
|
121
70
|
def connection
|
|
122
71
|
@_connection ||= Faraday.new(url: configuration.base_url) do |f|
|
|
72
|
+
f.request :retry, retry_options
|
|
123
73
|
f.request :json
|
|
124
74
|
f.response :json
|
|
125
75
|
f.adapter Faraday.default_adapter
|
|
126
76
|
f.options.params_encoder = Faraday::FlatParamsEncoder
|
|
127
77
|
|
|
128
|
-
# Allow custom Faraday configuration
|
|
129
78
|
configuration.faraday_configuration_block&.call(f)
|
|
130
79
|
end
|
|
131
80
|
end
|
|
@@ -2,17 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
module Helo::Core
|
|
4
4
|
class Configuration
|
|
5
|
-
|
|
6
|
-
# See Helo::Core::Client for which requests are retried.
|
|
7
|
-
DEFAULT_MAX_RETRIES = 2
|
|
8
|
-
|
|
9
|
-
attr_accessor :base_url, :api_key, :max_retries
|
|
5
|
+
attr_accessor :base_url, :api_key, :retry_options
|
|
10
6
|
attr_reader :faraday_configuration_block
|
|
11
7
|
|
|
12
|
-
def initialize(base_url: nil, api_key: nil,
|
|
8
|
+
def initialize(base_url: nil, api_key: nil, retry_options: {})
|
|
13
9
|
@base_url = base_url
|
|
14
10
|
@api_key = api_key
|
|
15
|
-
@
|
|
11
|
+
@retry_options = retry_options
|
|
16
12
|
@faraday_configuration_block = nil
|
|
17
13
|
end
|
|
18
14
|
|
data/lib/helo/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: helo-email-sdk
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 2.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Helo Email
|
|
@@ -37,6 +37,20 @@ dependencies:
|
|
|
37
37
|
- - ">="
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
39
|
version: '2.0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: faraday-retry
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '2.0'
|
|
47
|
+
type: :runtime
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '2.0'
|
|
40
54
|
- !ruby/object:Gem::Dependency
|
|
41
55
|
name: actionmailer
|
|
42
56
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -123,6 +137,7 @@ files:
|
|
|
123
137
|
- lib/helo/core/model.rb
|
|
124
138
|
- lib/helo/core/model_type.rb
|
|
125
139
|
- lib/helo/enums/attachment_disposition.rb
|
|
140
|
+
- lib/helo/enums/batch_validation_mode.rb
|
|
126
141
|
- lib/helo/enums/broadcast_status.rb
|
|
127
142
|
- lib/helo/enums/delivery_type.rb
|
|
128
143
|
- lib/helo/enums/dns_record_status.rb
|