helo-email-sdk 1.3.0 → 1.4.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 +22 -0
- data/lib/helo/apis/sending.rb +8 -4
- data/lib/helo/configuration.rb +1 -1
- data/lib/helo/core/client.rb +85 -9
- data/lib/helo/core/configuration.rb +7 -2
- data/lib/helo/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bda1f9e2b1c75a0550f90e52c4a3ccb32ec20faec196fe878997ddd86c3b239f
|
|
4
|
+
data.tar.gz: 9ebbea0e7cd719dc1e36478a586f657575dc33c25de906ff1bedb5923f5c1bfc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 265f798a582db36a9b77772e2de2af5e3640e6d2f30e6af83e471af76210214cb13f0f12156996ab1bfbacbe7287d2dd115b9ebe918f2afbbdc4ce1eb7de80b1
|
|
7
|
+
data.tar.gz: f9c884a5b91fec459b2da6d4da971734d2c5c23bf09cb212cfbaba04d30aabafc36cf624f94502e78cb20d57aad5b11431f0d0db9a739578546c194360bc8c06
|
data/README.md
CHANGED
|
@@ -28,6 +28,28 @@ 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
|
+
|
|
31
53
|
## Usage
|
|
32
54
|
|
|
33
55
|
Each API is a class with class-level methods. Responses come back as model objects with
|
data/lib/helo/apis/sending.rb
CHANGED
|
@@ -6,7 +6,8 @@ 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
|
-
|
|
9
|
+
# Generated once per call, so a retry is recognized as the same request.
|
|
10
|
+
headers["X-Helo-Idempotency-Key"] = idempotency_key || Client.idempotency_key
|
|
10
11
|
response = @client.request(:post, "/send/transactional", body: request.to_params, headers: headers)
|
|
11
12
|
SendMessageAcceptedResponse.from_hash(response.body)
|
|
12
13
|
end
|
|
@@ -15,7 +16,8 @@ module Helo
|
|
|
15
16
|
request = SendMessageBatchRequest.new(request_data)
|
|
16
17
|
headers = {}
|
|
17
18
|
headers["X-Helo-Channel-Id"] = channel_id unless channel_id.nil?
|
|
18
|
-
|
|
19
|
+
# Generated once per call, so a retry is recognized as the same request.
|
|
20
|
+
headers["X-Helo-Idempotency-Key"] = idempotency_key || Client.idempotency_key
|
|
19
21
|
response = @client.request(:post, "/send/transactional/batch", body: request.to_params, headers: headers)
|
|
20
22
|
SendMessageBatchResponse.from_hash(response.body)
|
|
21
23
|
end
|
|
@@ -24,7 +26,8 @@ module Helo
|
|
|
24
26
|
request = SendBroadcastRequest.new(request_data)
|
|
25
27
|
headers = {}
|
|
26
28
|
headers["X-Helo-Channel-Id"] = channel_id unless channel_id.nil?
|
|
27
|
-
|
|
29
|
+
# Generated once per call, so a retry is recognized as the same request.
|
|
30
|
+
headers["X-Helo-Idempotency-Key"] = idempotency_key || Client.idempotency_key
|
|
28
31
|
response = @client.request(:post, "/send/broadcast", body: request.to_params, headers: headers)
|
|
29
32
|
SendBroadcastResponse.from_hash(response.body)
|
|
30
33
|
end
|
|
@@ -33,7 +36,8 @@ module Helo
|
|
|
33
36
|
request = SendMessageRequest.new(request_data)
|
|
34
37
|
headers = {}
|
|
35
38
|
headers["X-Helo-Channel-Id"] = channel_id unless channel_id.nil?
|
|
36
|
-
|
|
39
|
+
# Generated once per call, so a retry is recognized as the same request.
|
|
40
|
+
headers["X-Helo-Idempotency-Key"] = idempotency_key || Client.idempotency_key
|
|
37
41
|
response = @client.request(:post, "/send/broadcast/message", body: request.to_params, headers: headers)
|
|
38
42
|
SendMessageAcceptedResponse.from_hash(response.body)
|
|
39
43
|
end
|
data/lib/helo/configuration.rb
CHANGED
data/lib/helo/core/client.rb
CHANGED
|
@@ -1,11 +1,32 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "faraday"
|
|
4
|
+
require "securerandom"
|
|
5
|
+
require "time"
|
|
4
6
|
|
|
5
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.
|
|
6
17
|
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
|
+
|
|
7
23
|
attr_reader :configuration
|
|
8
24
|
|
|
25
|
+
# A random UUID, for an idempotency key the caller didn't supply.
|
|
26
|
+
def self.idempotency_key
|
|
27
|
+
SecureRandom.uuid
|
|
28
|
+
end
|
|
29
|
+
|
|
9
30
|
def initialize(configuration)
|
|
10
31
|
raise ArgumentError, "base_url is not configured" if configuration.base_url.to_s.empty?
|
|
11
32
|
|
|
@@ -13,7 +34,41 @@ module Helo::Core
|
|
|
13
34
|
end
|
|
14
35
|
|
|
15
36
|
def request(method, path, params: {}, body: nil, headers: {})
|
|
16
|
-
|
|
37
|
+
can_repeat = IDEMPOTENT_METHODS.include?(method.to_sym) ||
|
|
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
|
|
58
|
+
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
|
|
59
|
+
handle_connection_error(e)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# The generated Client subclass overrides this with the package identity it
|
|
63
|
+
# was generated for; Helo::Core on its own has no package to name.
|
|
64
|
+
def user_agent
|
|
65
|
+
nil
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
def send_request(method, path, params, body, headers)
|
|
71
|
+
connection.public_send(method, path) do |req|
|
|
17
72
|
req.params = params
|
|
18
73
|
req.body = body
|
|
19
74
|
|
|
@@ -28,19 +83,40 @@ module Helo::Core
|
|
|
28
83
|
req.headers[key.to_s] = value.to_s
|
|
29
84
|
end
|
|
30
85
|
end
|
|
86
|
+
end
|
|
31
87
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
handle_connection_error(e)
|
|
88
|
+
def max_retries
|
|
89
|
+
configuration.max_retries || 0
|
|
35
90
|
end
|
|
36
91
|
|
|
37
|
-
#
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
nil
|
|
92
|
+
# How long to wait before retrying this response, or nil not to retry it.
|
|
93
|
+
def retry_delay(response, attempt, can_repeat)
|
|
94
|
+
status = response.status
|
|
95
|
+
return nil if attempt >= max_retries || !RETRY_STATUSES.include?(status)
|
|
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
|
|
41
102
|
end
|
|
42
103
|
|
|
43
|
-
|
|
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
|
|
119
|
+
end
|
|
44
120
|
|
|
45
121
|
def connection
|
|
46
122
|
@_connection ||= Faraday.new(url: configuration.base_url) do |f|
|
|
@@ -2,12 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
module Helo::Core
|
|
4
4
|
class Configuration
|
|
5
|
-
|
|
5
|
+
# Retries after a network failure, timeout, 408, 429 or 5xx; 0 disables them.
|
|
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
|
|
6
10
|
attr_reader :faraday_configuration_block
|
|
7
11
|
|
|
8
|
-
def initialize(base_url: nil, api_key: nil)
|
|
12
|
+
def initialize(base_url: nil, api_key: nil, max_retries: DEFAULT_MAX_RETRIES)
|
|
9
13
|
@base_url = base_url
|
|
10
14
|
@api_key = api_key
|
|
15
|
+
@max_retries = max_retries
|
|
11
16
|
@faraday_configuration_block = nil
|
|
12
17
|
end
|
|
13
18
|
|
data/lib/helo/version.rb
CHANGED