helo-email-sdk 1.3.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
- data/README.md +24 -0
- data/lib/helo/apis/sending.rb +4 -4
- data/lib/helo/configuration.rb +1 -1
- data/lib/helo/core/client.rb +38 -13
- data/lib/helo/core/configuration.rb +3 -2
- data/lib/helo/version.rb +1 -1
- metadata +15 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 63282f0b8d0d5cafd4d1d147d861fba45702b20828e1d98813f36bb7a82de4c5
|
|
4
|
+
data.tar.gz: cb9f8a9732bc517d7f5ef5ce288025833955add3872cba15aa4177272bafcc8f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5103b924c9bda3235dc11373c549b2e260ec7ffcaada3ea48493c661c7f9bb87725b50599c59da753a7109e14bc1f47a5409d1445c659a9af0d28f1f0a733031
|
|
7
|
+
data.tar.gz: dd999d86b4e388c46cce2a12d5f0fb5421db3f34a75c96d0739f16036ba1174a6917ba1cb71e1d6f4d02c71bdcd1be6c297d4a531866d51f20d2c250b3c42f5b
|
data/README.md
CHANGED
|
@@ -28,6 +28,30 @@ 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 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
|
+
Retries are done by [faraday-retry](https://github.com/lostisland/faraday-retry), and
|
|
45
|
+
`config.retry_options` is merged over the options the SDK gives it: `max:` changes how many
|
|
46
|
+
(`0` disables them), `interval:` the first wait, and `retry_block:` can log each retry.
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
Helo.configure do |config|
|
|
50
|
+
config.api_key = ENV.fetch("HELO_API_KEY")
|
|
51
|
+
config.retry_options = { max: 0 }
|
|
52
|
+
end
|
|
53
|
+
```
|
|
54
|
+
|
|
31
55
|
## Usage
|
|
32
56
|
|
|
33
57
|
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,7 @@ 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
|
-
headers["X-Helo-Idempotency-Key"] = idempotency_key
|
|
9
|
+
headers["X-Helo-Idempotency-Key"] = idempotency_key || Client.idempotency_key
|
|
10
10
|
response = @client.request(:post, "/send/transactional", body: request.to_params, headers: headers)
|
|
11
11
|
SendMessageAcceptedResponse.from_hash(response.body)
|
|
12
12
|
end
|
|
@@ -15,7 +15,7 @@ module Helo
|
|
|
15
15
|
request = SendMessageBatchRequest.new(request_data)
|
|
16
16
|
headers = {}
|
|
17
17
|
headers["X-Helo-Channel-Id"] = channel_id unless channel_id.nil?
|
|
18
|
-
headers["X-Helo-Idempotency-Key"] = idempotency_key
|
|
18
|
+
headers["X-Helo-Idempotency-Key"] = idempotency_key || Client.idempotency_key
|
|
19
19
|
response = @client.request(:post, "/send/transactional/batch", body: request.to_params, headers: headers)
|
|
20
20
|
SendMessageBatchResponse.from_hash(response.body)
|
|
21
21
|
end
|
|
@@ -24,7 +24,7 @@ module Helo
|
|
|
24
24
|
request = SendBroadcastRequest.new(request_data)
|
|
25
25
|
headers = {}
|
|
26
26
|
headers["X-Helo-Channel-Id"] = channel_id unless channel_id.nil?
|
|
27
|
-
headers["X-Helo-Idempotency-Key"] = idempotency_key
|
|
27
|
+
headers["X-Helo-Idempotency-Key"] = idempotency_key || Client.idempotency_key
|
|
28
28
|
response = @client.request(:post, "/send/broadcast", body: request.to_params, headers: headers)
|
|
29
29
|
SendBroadcastResponse.from_hash(response.body)
|
|
30
30
|
end
|
|
@@ -33,7 +33,7 @@ module Helo
|
|
|
33
33
|
request = SendMessageRequest.new(request_data)
|
|
34
34
|
headers = {}
|
|
35
35
|
headers["X-Helo-Channel-Id"] = channel_id unless channel_id.nil?
|
|
36
|
-
headers["X-Helo-Idempotency-Key"] = idempotency_key
|
|
36
|
+
headers["X-Helo-Idempotency-Key"] = idempotency_key || Client.idempotency_key
|
|
37
37
|
response = @client.request(:post, "/send/broadcast/message", body: request.to_params, headers: headers)
|
|
38
38
|
SendMessageAcceptedResponse.from_hash(response.body)
|
|
39
39
|
end
|
data/lib/helo/configuration.rb
CHANGED
data/lib/helo/core/client.rb
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "faraday"
|
|
4
|
+
require "faraday/retry"
|
|
5
|
+
require "securerandom"
|
|
4
6
|
|
|
5
7
|
module Helo::Core
|
|
6
8
|
class Client
|
|
7
9
|
attr_reader :configuration
|
|
8
10
|
|
|
11
|
+
def self.idempotency_key
|
|
12
|
+
SecureRandom.uuid
|
|
13
|
+
end
|
|
14
|
+
|
|
9
15
|
def initialize(configuration)
|
|
10
16
|
raise ArgumentError, "base_url is not configured" if configuration.base_url.to_s.empty?
|
|
11
17
|
|
|
@@ -13,43 +19,62 @@ module Helo::Core
|
|
|
13
19
|
end
|
|
14
20
|
|
|
15
21
|
def request(method, path, params: {}, body: nil, headers: {})
|
|
16
|
-
|
|
22
|
+
handle_response(send_request(method, path, params, body, headers))
|
|
23
|
+
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
|
|
24
|
+
handle_connection_error(e)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Overridden by the generated Client.
|
|
28
|
+
def user_agent
|
|
29
|
+
nil
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def send_request(method, path, params, body, headers)
|
|
35
|
+
connection.public_send(method, path) do |req|
|
|
17
36
|
req.params = params
|
|
18
37
|
req.body = body
|
|
19
38
|
|
|
20
|
-
# Set Authorization header with dynamic or static token
|
|
21
39
|
token = current_api_key
|
|
22
40
|
req.headers["Authorization"] = "Bearer #{token}" if token
|
|
23
41
|
|
|
24
42
|
req.headers["User-Agent"] = user_agent if user_agent
|
|
25
43
|
|
|
26
|
-
# Apply per-request headers verbatim (already in wire form)
|
|
27
44
|
headers.each do |key, value|
|
|
28
45
|
req.headers[key.to_s] = value.to_s
|
|
29
46
|
end
|
|
30
47
|
end
|
|
31
|
-
|
|
32
|
-
handle_response(response)
|
|
33
|
-
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
|
|
34
|
-
handle_connection_error(e)
|
|
35
48
|
end
|
|
36
49
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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 || {})
|
|
41
62
|
end
|
|
42
63
|
|
|
43
|
-
|
|
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") }
|
|
68
|
+
end
|
|
44
69
|
|
|
45
70
|
def connection
|
|
46
71
|
@_connection ||= Faraday.new(url: configuration.base_url) do |f|
|
|
72
|
+
f.request :retry, retry_options
|
|
47
73
|
f.request :json
|
|
48
74
|
f.response :json
|
|
49
75
|
f.adapter Faraday.default_adapter
|
|
50
76
|
f.options.params_encoder = Faraday::FlatParamsEncoder
|
|
51
77
|
|
|
52
|
-
# Allow custom Faraday configuration
|
|
53
78
|
configuration.faraday_configuration_block&.call(f)
|
|
54
79
|
end
|
|
55
80
|
end
|
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
module Helo::Core
|
|
4
4
|
class Configuration
|
|
5
|
-
attr_accessor :base_url, :api_key
|
|
5
|
+
attr_accessor :base_url, :api_key, :retry_options
|
|
6
6
|
attr_reader :faraday_configuration_block
|
|
7
7
|
|
|
8
|
-
def initialize(base_url: nil, api_key: nil)
|
|
8
|
+
def initialize(base_url: nil, api_key: nil, retry_options: {})
|
|
9
9
|
@base_url = base_url
|
|
10
10
|
@api_key = api_key
|
|
11
|
+
@retry_options = retry_options
|
|
11
12
|
@faraday_configuration_block = nil
|
|
12
13
|
end
|
|
13
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:
|
|
4
|
+
version: 2.0.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
|