unione 1.2.3 → 1.3.1
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/AGENTS.md +50 -60
- data/LICENSE +1 -1
- data/README.md +5 -4
- data/lib/unione/client/api_client.rb +63 -18
- data/lib/unione/client/api_error.rb +40 -2
- data/lib/unione/client/configuration.rb +1 -1
- data/lib/unione/client/error_info.rb +116 -0
- data/lib/unione/version.rb +1 -1
- data/lib/unione.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c4c11d049f5f0f63d8ec5f50faa47fd67a4aef0096626f3b5dd7aab2f046c6fb
|
|
4
|
+
data.tar.gz: 0addffbd5afeb09f872c22cc2d37606370aae816b740b19807f4783c4b9023a6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 864e8f1d545f9ee5eadcc75816d19c95dfe559499b857e39d9b747b0abeb2b985ac030cdc5b8bbd34cb19d8a5dd190e2e4a429e2eb72787fa33b68397c65add3
|
|
7
|
+
data.tar.gz: 4d5e42f36010214e946dcca5441a373bceb9cf5c8eda4dad65ef9ebba45e18be0f7172e720bb39cf9b9979ca2c6238119a4725dfedac53bc58af796881bb93d7
|
data/AGENTS.md
CHANGED
|
@@ -1,94 +1,70 @@
|
|
|
1
1
|
# AGENTS.md
|
|
2
2
|
|
|
3
|
-
UniOne Ruby SDK — client for the UniOne transactional email Web API (Web API 1.
|
|
3
|
+
UniOne Ruby SDK — client for the UniOne transactional email Web API (Web API 1.92). How to integrate UniOne into an application comes first; the last section is about changing this package.
|
|
4
4
|
|
|
5
|
-
##
|
|
6
|
-
|
|
7
|
-
- **Generated code.** The whole package is generated by the UniOne team from the UniOne Web API OpenAPI spec (Web API 1.90): sources, docs, README.md and this file. Do not hand-edit it: the next release replaces the content of this repository. Report problems and requests in the issues of this repository (https://github.com/unione-repo/unione-ruby-sdk).
|
|
8
|
-
- **Build and check:**
|
|
9
|
-
|
|
10
|
-
```bash
|
|
11
|
-
gem build *.gemspec
|
|
12
|
-
```
|
|
5
|
+
## Integrating UniOne
|
|
13
6
|
|
|
14
|
-
|
|
15
|
-
- **Tests.** Before a release the generator builds and lints the package and runs every example of docs/ and of this file against a mock of the API: no real API key, no real sends.
|
|
16
|
-
- **Release.** The version (1.2.3 in the gemspec) and the tag `v1.2.3` come from the generator; README.md states the Web API version the release was generated from.
|
|
17
|
-
- **Secrets.** Never commit API keys; the examples read `UNIONE_API_KEY` from the environment.
|
|
7
|
+
**On one of these frameworks, install its package instead of this SDK:** it brings the SDK and plugs it into the framework (configuration, client in the container, mailer, webhooks).
|
|
18
8
|
|
|
19
|
-
|
|
9
|
+
| Framework | Package | Guide |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| Ruby on Rails | `unione-rails` | [AGENTS.md](https://github.com/unione-repo/unione-rails/blob/HEAD/AGENTS.md) |
|
|
20
12
|
|
|
21
|
-
|
|
13
|
+
### Install
|
|
22
14
|
|
|
23
15
|
```bash
|
|
24
16
|
gem install unione
|
|
25
17
|
```
|
|
26
18
|
|
|
27
|
-
Requirements: Ruby 2.7+.
|
|
28
|
-
|
|
29
|
-
If the project is built on one of these frameworks, install its package instead: it brings this SDK and plugs it into the framework (configuration, client in the container, mailer, webhooks).
|
|
30
|
-
|
|
31
|
-
| Framework | Package | Guide |
|
|
32
|
-
|---|---|---|
|
|
33
|
-
| Ruby on Rails | `unione-rails` | [AGENTS.md](https://github.com/unione-repo/unione-rails/blob/HEAD/AGENTS.md) |
|
|
34
|
-
|
|
35
|
-
Rules that generated code must follow:
|
|
36
|
-
|
|
37
|
-
- Server-side only. Read the API key from `UNIONE_API_KEY`; never put it in browser or mobile code, never commit it.
|
|
38
|
-
- Base URL `https://api.unione.io/en/transactional/api/v1` (an account registered on another host uses that one: `eu1.unione.io`, `us1.unione.io`); every method is POST with a JSON body; auth header `X-API-KEY`. The SDK does all of it.
|
|
39
|
-
- First email without DNS: `domain/get-sandbox-domain` → `confirmed-email/create` (the user clicks the link within 48 hours) → `email/send` from `noreply@<sandbox-domain>` to the confirmed address. Account API key only; up to 10 confirmed addresses, 100 emails a day; the domain expires after 31 days.
|
|
40
|
-
- Never ship a sandbox domain as the production sender (it is not in `domain/list`); read `from_email` from config. Production needs a verified domain: `domain/get-dns-records` → DNS records → `domain/validate-verification-record`, `domain/validate-dkim` → `domain/list`.
|
|
41
|
-
- Set `message.idempotence_key` on retryable sends; retry network errors, `429` and `5xx` with backoff, not other errors unchanged. Up to 500 recipients and 10 MB per request with the attachments in base64 (a third larger than the files); always `body.html` or `body.plaintext`.
|
|
42
|
-
- Delivery events via webhook (`webhook/set`) with a fast, idempotent HTTPS handler, not polling; check the `auth` field of every notification: the MD5 of the request body with the `auth` value replaced by the API key.
|
|
43
|
-
- Keep the default unsubscribe and tracking behaviour; changes go through UniOne support.
|
|
44
|
-
- Do not invent methods or fields. Check [docs/](docs/) of this package, the [API reference](https://docs.unione.io/en/web-api-ref) or [llms.txt](https://docs.unione.io/llms.txt).
|
|
19
|
+
Requirements: Ruby 2.7+. The API key goes to `UNIONE_API_KEY`.
|
|
45
20
|
|
|
46
21
|
### Send an email
|
|
47
22
|
|
|
48
|
-
The client reads the API key from `UNIONE_API_KEY`:
|
|
49
|
-
|
|
50
23
|
```ruby
|
|
51
24
|
require 'unione'
|
|
52
25
|
|
|
53
|
-
|
|
26
|
+
Body = UniOne::Common::Model::BodyObject
|
|
54
27
|
Email = UniOne::Api::Email::Model
|
|
28
|
+
Recipient = UniOne::Api::Email::Model::MessageObjectRecipientsItem
|
|
55
29
|
|
|
56
30
|
client = UniOne.client(ENV.fetch('UNIONE_API_KEY'))
|
|
57
31
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
)
|
|
68
|
-
],
|
|
69
|
-
body: Common::BodyObject.new(
|
|
70
|
-
html: '<b>Hello, {{to_name}}</b>',
|
|
71
|
-
plaintext: 'Hello, {{to_name}}'
|
|
72
|
-
),
|
|
73
|
-
subject: 'UniOne test email ',
|
|
74
|
-
from_email: 'email@example.com',
|
|
75
|
-
from_name: 'John Smith'
|
|
76
|
-
)
|
|
77
|
-
))
|
|
78
|
-
p response.job_id
|
|
32
|
+
message = Email::MessageObject.new(
|
|
33
|
+
recipients: [Recipient.from_address('Alex <alex@example.com>')],
|
|
34
|
+
body: Body.html('<p>Hello, {{to_name}}! Your order has shipped.</p>'),
|
|
35
|
+
subject: 'Your order',
|
|
36
|
+
from_email: 'shop@example.com',
|
|
37
|
+
from_name: 'Shop'
|
|
38
|
+
)
|
|
39
|
+
response = client.email.send_email(Email::EmailSendRequest.new(message: message))
|
|
40
|
+
puts response.job_id
|
|
79
41
|
```
|
|
80
42
|
|
|
43
|
+
Personalization, attachments, CC/BCC, scheduling, safe retries, error handling, the webhook handler and the sandbox flow: the recipes of [README.md](README.md#recipes).
|
|
44
|
+
|
|
45
|
+
### Rules for generated code
|
|
46
|
+
|
|
47
|
+
- Server-side only. Read the API key from `UNIONE_API_KEY`; never put it in browser or mobile code, never commit it.
|
|
48
|
+
- Build the recipients and the body with the short forms of the SDK (the recipient from `"Name <email>"`, the name goes to `to_name`; per-recipient metadata; the HTML / text body), as in "Send an email" and the README recipes, not with hand-built nested objects.
|
|
49
|
+
- Base URL `https://api.unione.io/en/transactional/api/v1` (an account registered on another host uses that one: `eu1.unione.io`, `us1.unione.io`); every method is POST with a JSON body; auth header `X-API-KEY`. The SDK does all of it.
|
|
50
|
+
- First email without DNS: `domain/get-sandbox-domain` → `confirmed-email/create` (the user clicks the link within 48 hours) → `email/send` from `noreply@<sandbox-domain>` to the confirmed address. Account API key only; up to 10 confirmed addresses, 100 emails a day. The domain is deleted 31 days after it was created: the next `domain/get-sandbox-domain` call creates a new one with another name, so read the name from the response, never hardcode it.
|
|
51
|
+
- Never ship a sandbox domain as the production sender (it is not in `domain/list`); read `from_email` from config. Production needs a verified domain: `domain/get-dns-records` → DNS records (verification TXT as is, DKIM TXT `v=DKIM1; k=rsa; p=<dkim>` on `<selector>._domainkey`, CNAME `_dmarc` → the `dmarc` field; SPF `include:spf.unione.io` is not in the response) → `domain/validate-verification-record`, `domain/validate-dkim` → `domain/list`.
|
|
52
|
+
- Set `message.idempotence_key` on every send that may be retried. Retry only an error that is retryable (the retryable flag of the SDK error: network, timeout, `429`, `5xx`; never a request you cancelled), with backoff and the same `idempotence_key`, within the one-minute idempotence window; beyond it check your own "sent" record before sending again. Do not retry other errors unchanged. Up to 500 recipients and 10 MB per request with the attachments in base64 (a third larger than the files); always `body.html` or `body.plaintext` (or a `template_id`).
|
|
53
|
+
- Delivery events via webhook (`webhook/set`), not polling. Check the `auth` field of every notification (MD5 of the raw request body with the `auth` value replaced by the API key) and answer 401 to a wrong one. Answer 200 within 3 seconds and process the events from a queue, otherwise the request is repeated every 10 minutes for 24 hours; take at least 5 parallel requests of up to 100 events each; deduplicate.
|
|
54
|
+
- Keep the default unsubscribe and tracking behaviour; changes go through UniOne support.
|
|
55
|
+
- Do not invent methods or fields. Check [docs/](docs/) of this package, the [API reference](https://docs.unione.io/en/web-api-ref) or [llms.txt](https://docs.unione.io/llms.txt).
|
|
56
|
+
|
|
81
57
|
### Errors
|
|
82
58
|
|
|
83
|
-
Every method throws `ApiError` when the request fails
|
|
59
|
+
Every method throws `ApiError` when the request fails. Its message names the method and, for an API error, the [API error code](https://docs.unione.io/en/web-api-ref#api-errors), the HTTP status and how to fix it; its kind tells what failed (api, network, timeout, invalid_response, invalid_request: a missing field found before sending) and retryable whether a repeat may succeed (network, timeout, HTTP 429 and 5xx); the API's own message text is in `api_message`.
|
|
84
60
|
|
|
85
61
|
```ruby
|
|
86
62
|
# client created as above
|
|
87
63
|
begin
|
|
88
64
|
client.system.ping
|
|
89
65
|
rescue UniOne::Client::ApiError => e
|
|
90
|
-
|
|
91
|
-
warn "#{e.
|
|
66
|
+
warn e.message
|
|
67
|
+
warn "#{e.kind} #{e.retryable? ? 'retryable' : 'not retryable'}"
|
|
92
68
|
end
|
|
93
69
|
```
|
|
94
70
|
|
|
@@ -97,3 +73,17 @@ end
|
|
|
97
73
|
- Methods and models of every section: [Email](docs/Email.md), [EmailValidation](docs/EmailValidation.md), [ConfirmedEmail](docs/ConfirmedEmail.md), [Template](docs/Template.md), [Webhook](docs/Webhook.md), [Suppression](docs/Suppression.md), [Domain](docs/Domain.md), [EventDump](docs/EventDump.md), [Tag](docs/Tag.md), [Project](docs/Project.md), [System](docs/System.md), [Obsolete](docs/Obsolete.md)
|
|
98
74
|
- [API reference](https://docs.unione.io/en/web-api-ref)
|
|
99
75
|
- [Full API reference for LLMs](https://docs.unione.io/llms-full.txt)
|
|
76
|
+
|
|
77
|
+
## Working on this repository
|
|
78
|
+
|
|
79
|
+
- **Generated code.** The whole package is generated by the UniOne team from the UniOne Web API OpenAPI spec (Web API 1.92): sources, docs, README.md and this file. Do not hand-edit it: the next release replaces the content of this repository. Report problems and requests in the issues of this repository (https://github.com/unione-repo/unione-ruby-sdk).
|
|
80
|
+
- **Build and check:**
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
gem build *.gemspec
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- **Source label.** Every request carries the query parameter `platform=ruby-sdk.1.3.1`, added in one place, the shared request client (`ApiClient`); a package built on this SDK puts its own label in front: `<package>.<version>,ruby-sdk.1.3.1`.
|
|
87
|
+
- **Tests.** `if [ -f Gemfile ]; then bundle install; fi; ruby -Ilib -Itest -e 'Dir["test/**/*_test.rb"].sort.each { |f| require File.expand_path(f) }'` runs the tests of the package (tests/ or test/, not published) offline: the API is faked (a fake transport or a mock server on 127.0.0.1), never the real API, no real key; add a test with every change of the client. Before a release the generator also builds and lints the package and runs every example of docs/ and of this file against a mock of the API. The CI (.github/workflows/test.yml) runs the tests on every push and pull request over the supported runtimes; release.yml publishes a tag only after they pass.
|
|
88
|
+
- **Release.** The version (1.3.1 in the gemspec) and the tag `v1.3.1` come from the generator; README.md states the Web API version the release was generated from.
|
|
89
|
+
- **Secrets.** Never commit API keys; the examples read `UNIONE_API_KEY` from the environment.
|
data/LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -135,7 +135,7 @@ client.email.send_email(Email::EmailSendRequest.new(message: message))
|
|
|
135
135
|
|
|
136
136
|
### Retry safely
|
|
137
137
|
|
|
138
|
-
A repeat with the same `idempotence_key` within a minute is not sent twice: retry network errors, timeouts, 429 and 5xx with it.
|
|
138
|
+
A repeat with the same `idempotence_key` within a minute is not sent twice: retry the retryable errors (network errors, timeouts, 429 and 5xx) with it; after that minute check your own record of the sent email before repeating.
|
|
139
139
|
|
|
140
140
|
```ruby
|
|
141
141
|
message = Email::MessageObject.new(
|
|
@@ -156,8 +156,9 @@ Recipients the API drops (unsubscribed, invalid, suppressed) are not an error: t
|
|
|
156
156
|
begin
|
|
157
157
|
client.email.send_email(request)
|
|
158
158
|
rescue UniOne::Client::ApiError => e
|
|
159
|
-
# HTTP status
|
|
160
|
-
puts
|
|
159
|
+
# the message: method, API error code, HTTP status and how to fix it; kind: what failed; retryable: a repeat may succeed
|
|
160
|
+
puts e.message
|
|
161
|
+
puts "#{e.kind} #{e.retryable? ? 'retryable' : 'not retryable'}"
|
|
161
162
|
end
|
|
162
163
|
```
|
|
163
164
|
|
|
@@ -353,7 +354,7 @@ headers = UniOne::Common::Model::HeadersObject.new(extra: {
|
|
|
353
354
|
|
|
354
355
|
### Error handling
|
|
355
356
|
|
|
356
|
-
Every method raises `ApiError
|
|
357
|
+
Every method raises `ApiError`: an error of the API, a network error or a timeout, a broken response, and a mistake found before sending (an empty API key, a missing or empty `message.recipients` or `email` of a recipient: no request is sent). The message names the method and, for an API error, the [API error code](https://docs.unione.io/en/web-api-ref#api-errors), the HTTP status and how to fix it: `email/send: Unknown recipient (API error 204, HTTP 400). Fix: ...`. The error also tells what failed (its kind: api, network, timeout, invalid_response, invalid_request), whether a repeat may succeed (network, timeout, HTTP 429 and 5xx; not a request you cancelled) and keeps the API's own message text, in full and undecorated, in `api_message` (empty without an API response).
|
|
357
358
|
|
|
358
359
|
## Help
|
|
359
360
|
|
|
@@ -7,7 +7,7 @@ require 'uri'
|
|
|
7
7
|
module UniOne
|
|
8
8
|
module Client
|
|
9
9
|
# The SDK and its version in the "platform" query parameter of every request.
|
|
10
|
-
PLATFORM = 'ruby-sdk.1.
|
|
10
|
+
PLATFORM = 'ruby-sdk.1.3.1'
|
|
11
11
|
|
|
12
12
|
# Default transport (HTTP layer of ApiClient): Net::HTTP of the standard library.
|
|
13
13
|
#
|
|
@@ -56,37 +56,82 @@ module UniOne
|
|
|
56
56
|
# Calls an API method, e.g. call('/email/send.json', {...}), and returns the decoded response.
|
|
57
57
|
# @raise [ApiError]
|
|
58
58
|
def call(path, body = {})
|
|
59
|
+
api_method = ErrorInfo.method_of(path)
|
|
60
|
+
if @config.api_key.to_s.empty?
|
|
61
|
+
raise invalid_request(api_method, 'the API key is empty: pass the API key of the user or of the project to the client')
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
json = encode(api_method, body)
|
|
65
|
+
missing = ErrorInfo.missing(api_method, JSON.parse(json))
|
|
66
|
+
raise invalid_request(api_method, "#{missing} is required") if missing
|
|
67
|
+
|
|
59
68
|
uri = URI("#{@config.base_url}/#{path.sub(%r{\A/+}, '')}")
|
|
60
69
|
uri.query = URI.encode_www_form(platform: [@config.platform, PLATFORM].compact.reject(&:empty?).join(','))
|
|
61
|
-
status, raw = send_request(uri,
|
|
70
|
+
status, raw = send_request(api_method, uri, json)
|
|
62
71
|
status = status.to_i
|
|
63
72
|
raw = raw.to_s
|
|
64
|
-
|
|
65
|
-
data = JSON.parse(raw)
|
|
66
|
-
rescue JSON::ParserError => e
|
|
67
|
-
raise ApiError.new("Invalid JSON in response (HTTP #{status}): #{raw[0, 500]} (#{e.message})",
|
|
68
|
-
http_status: status)
|
|
69
|
-
end
|
|
70
|
-
raise ApiError.new("Unexpected response (HTTP #{status}): #{raw[0, 500]}", http_status: status) unless data.is_a?(Hash)
|
|
73
|
+
data = decode(api_method, status, raw)
|
|
71
74
|
|
|
72
75
|
if status >= 400 || data['status'] == 'error'
|
|
73
|
-
code = data['code']
|
|
74
|
-
raise ApiError.new((data['message']
|
|
75
|
-
http_status: status,
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
code = ErrorInfo.code_of(data['code'])
|
|
77
|
+
raise ApiError.new(ErrorInfo.api_message(api_method, data['message'], status, code),
|
|
78
|
+
http_status: status, error_code: code, response_body: data, api_method: api_method,
|
|
79
|
+
hint: ErrorInfo.hint(code), kind: ApiError::KIND_API,
|
|
80
|
+
api_message: data['message']&.to_s)
|
|
78
81
|
end
|
|
79
82
|
data
|
|
80
83
|
end
|
|
81
84
|
|
|
82
85
|
private
|
|
83
86
|
|
|
84
|
-
def
|
|
87
|
+
def invalid_request(api_method, text)
|
|
88
|
+
ApiError.new("#{api_method}: #{text}", api_method: api_method, kind: ApiError::KIND_INVALID_REQUEST)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def encode(api_method, body)
|
|
92
|
+
JSON.generate(body || {})
|
|
93
|
+
rescue JSON::GeneratorError => e
|
|
94
|
+
raise invalid_request(api_method, "can not encode the request: #{e.message}")
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def decode(api_method, status, raw)
|
|
98
|
+
begin
|
|
99
|
+
data = JSON.parse(raw)
|
|
100
|
+
rescue JSON::ParserError
|
|
101
|
+
raise ApiError.new("#{api_method}: invalid JSON in response (HTTP #{status}): #{ErrorInfo.quote(raw)}",
|
|
102
|
+
http_status: status, api_method: api_method, kind: ApiError::KIND_INVALID_RESPONSE)
|
|
103
|
+
end
|
|
104
|
+
return data if data.is_a?(Hash)
|
|
105
|
+
|
|
106
|
+
raise ApiError.new("#{api_method}: unexpected response (HTTP #{status}): #{ErrorInfo.quote(raw)}",
|
|
107
|
+
http_status: status, api_method: api_method, kind: ApiError::KIND_INVALID_RESPONSE)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def send_request(api_method, uri, body)
|
|
85
111
|
@transport.call('POST', uri, headers, body, @config.timeout)
|
|
86
|
-
rescue ApiError
|
|
87
|
-
|
|
112
|
+
rescue ApiError => e
|
|
113
|
+
# an error of a custom transport: it keeps its data and gets the method; a plain ApiError (kind api, no code,
|
|
114
|
+
# no HTTP status) is a network one
|
|
115
|
+
raise unless e.api_method.to_s.empty?
|
|
116
|
+
|
|
117
|
+
kind = e.kind
|
|
118
|
+
kind = ApiError::KIND_NETWORK if kind == ApiError::KIND_API && e.error_code.nil? && e.http_status.to_i.zero?
|
|
119
|
+
text = e.message.start_with?("#{api_method}: ") ? e.message : "#{api_method}: #{e.message}"
|
|
120
|
+
raise ApiError.new(text, http_status: e.http_status, error_code: e.error_code, response_body: e.response_body,
|
|
121
|
+
api_method: api_method, hint: e.hint, kind: kind, api_message: e.api_message)
|
|
122
|
+
rescue Timeout::Error, Errno::ETIMEDOUT
|
|
123
|
+
# Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout, a connect timeout of the OS
|
|
124
|
+
raise ApiError.new("#{api_method}: HTTP request timed out after #{@config.timeout} s",
|
|
125
|
+
api_method: api_method, kind: ApiError::KIND_TIMEOUT)
|
|
88
126
|
rescue StandardError => e
|
|
89
|
-
|
|
127
|
+
# Faraday::TimeoutError, HTTP::TimeoutError, ... of a custom transport
|
|
128
|
+
if e.class.ancestors.any? { |c| c.is_a?(Class) && c.name.to_s.include?('Timeout') }
|
|
129
|
+
raise ApiError.new("#{api_method}: HTTP request timed out: #{e.class}: #{e.message}",
|
|
130
|
+
api_method: api_method, kind: ApiError::KIND_TIMEOUT)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
raise ApiError.new("#{api_method}: HTTP request failed: #{e.class}: #{e.message}",
|
|
134
|
+
api_method: api_method, kind: ApiError::KIND_NETWORK)
|
|
90
135
|
end
|
|
91
136
|
end
|
|
92
137
|
end
|
|
@@ -2,20 +2,58 @@
|
|
|
2
2
|
|
|
3
3
|
module UniOne
|
|
4
4
|
module Client
|
|
5
|
-
# Raised when the API returns an error or the response can not be processed.
|
|
5
|
+
# Raised when the API returns an error, the request fails or is invalid, or the response can not be processed.
|
|
6
|
+
# The message names the API method and, for an API error, the code, the HTTP status and how to fix it:
|
|
7
|
+
# "email/send: Unknown recipient (API error 204, HTTP 400). Fix: ... See ...#api-errors".
|
|
6
8
|
class ApiError < StandardError
|
|
9
|
+
# The API returned an error (error_code, hint)
|
|
10
|
+
KIND_API = 'api'
|
|
11
|
+
# The request could not be sent or the response could not be received
|
|
12
|
+
KIND_NETWORK = 'network'
|
|
13
|
+
# The request timed out
|
|
14
|
+
KIND_TIMEOUT = 'timeout'
|
|
15
|
+
# The response is not the JSON of the API (a proxy page, a broken body)
|
|
16
|
+
KIND_INVALID_RESPONSE = 'invalid_response'
|
|
17
|
+
# Found before sending (an empty API key, a required field missing): no request was sent
|
|
18
|
+
KIND_INVALID_REQUEST = 'invalid_request'
|
|
19
|
+
|
|
7
20
|
# @return [Integer] HTTP status of the response, 0 when the request was not sent
|
|
8
21
|
attr_reader :http_status
|
|
9
22
|
# @return [Integer, nil] API error code (the "code" field of the error response)
|
|
10
23
|
attr_reader :error_code
|
|
11
24
|
# @return [Hash, nil] decoded error response
|
|
12
25
|
attr_reader :response_body
|
|
26
|
+
# @return [String] API method of the request, e.g. "email/send"; empty when unknown
|
|
27
|
+
# (not "method": that is Object#method)
|
|
28
|
+
attr_reader :api_method
|
|
29
|
+
# @return [String, nil] how to fix the API error (the errors table of the docs), nil when unknown
|
|
30
|
+
attr_reader :hint
|
|
31
|
+
# @return [String] what failed: one of the KIND_* constants
|
|
32
|
+
attr_reader :kind
|
|
33
|
+
# @return [String, nil] the API's own error message (the "message" of the error response) as is, nil when there
|
|
34
|
+
# was no API response; #message is the decorated one
|
|
35
|
+
attr_reader :api_message
|
|
13
36
|
|
|
14
|
-
def initialize(message, http_status: 0, error_code: nil, response_body: nil
|
|
37
|
+
def initialize(message, http_status: 0, error_code: nil, response_body: nil, api_method: '', hint: nil,
|
|
38
|
+
kind: KIND_API, api_message: nil)
|
|
15
39
|
super(message)
|
|
40
|
+
@api_message = api_message
|
|
16
41
|
@http_status = http_status
|
|
17
42
|
@error_code = error_code
|
|
18
43
|
@response_body = response_body
|
|
44
|
+
@api_method = api_method
|
|
45
|
+
@hint = hint
|
|
46
|
+
@kind = kind
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# A repeat of the request may succeed: a network error, a timeout, HTTP 429 or 5xx.
|
|
50
|
+
# @return [Boolean]
|
|
51
|
+
def retryable?
|
|
52
|
+
case @kind
|
|
53
|
+
when KIND_NETWORK, KIND_TIMEOUT then true
|
|
54
|
+
when KIND_API, KIND_INVALID_RESPONSE then @http_status == 429 || @http_status >= 500
|
|
55
|
+
else false
|
|
56
|
+
end
|
|
19
57
|
end
|
|
20
58
|
end
|
|
21
59
|
end
|
|
@@ -36,7 +36,7 @@ module UniOne
|
|
|
36
36
|
# default ones of the same name
|
|
37
37
|
attr_accessor :headers
|
|
38
38
|
|
|
39
|
-
def initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: 30, user_agent: 'unione-ruby-sdk/1.
|
|
39
|
+
def initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: 30, user_agent: 'unione-ruby-sdk/1.3.1', platform: nil, headers: {})
|
|
40
40
|
@api_key = api_key
|
|
41
41
|
@base_url = base_url.sub(%r{/+\z}, '')
|
|
42
42
|
@timeout = timeout
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module UniOne
|
|
6
|
+
module Client
|
|
7
|
+
# Error handling data of the SDK: the hints of the API error codes (the errors table of the docs) and the
|
|
8
|
+
# fields checked before a request is sent.
|
|
9
|
+
# @api private
|
|
10
|
+
module ErrorInfo
|
|
11
|
+
ERRORS_JSON = '{"docs":"https://docs.unione.io/en/web-api-ref#api-errors","hints":{"101":"Make sure you have passed correct X-API-KEY header or \'api_key\' JSON parameter.","102":"Use correct user API-key (https://cp.unione.io/en/user/info/api) or project API key (https://cp.unione.io/en/user/project).","105":"You can turn API on here (https://cp.unione.io/en/user/info/api).","106":"To solve this problem, contact support (https://cp.unione.io/en/support).","111":"Check your JSON request syntax.","114":"Please send the request to the host you have been registered at, us1.unione.io or eu1.unione.io.","118":"Add a DNS verification record to your domain with values returned by domain/get-dns-records.","120":"Please contact support (https://cp.unione.io/en/support).","121":"DNS request has timed out. Please make sure your DNS provider is online and try again later.","122":"Please add to your domain a DKIM record with key returned by domain/get-dns-records method.","123":"You should properly setup DKIM for your domain.","124":"Seems like you have changed the record. While it\'s technically correct, it doesn\'t match the one we expect. You should use DKIM key returned by domain/get-dns-records method.","125":"Please setup the DKIM record for your domain properly.","126":"Please add a DKIM record for your domain with a key returned by domain/get-dns-records API method.","127":"A maximum of 10 DKIM records are allowed per primary domain.","129":"Please check that you have correctly added a DKIM record with key returned by domain/get-dns-records method and try again. Please contact support (https://cp.unione.io/en/support) if the error still occurs.","130":"Please use another domain.","150":"Unknown error occurred. Please try again later or contact support (https://cp.unione.io/en/support).","199":"The data in request should be no more than 10 MB.","201":"Check that your message body is being passed properly.","202":"Enter the sender email.","203":"Enter the subject.","204":"Make sure that the recipient\'s list is passed as an array where each recipient is an array with obligatory \'email\' parameter.","205":"Make sure the parameter is passed properly.","206":"To avoid this error please use an email on a verified domain (https://docs.unione.io/en/web-api-ref#domain).","207":"Check the file and the file type again.","208":"Rename files with the forbidden symbols.","209":"Rename files with the same names.","210":"Rename files with the same names.","211":"Rename files with the forbidden symbols.","212":"The possible maximum substitutions number has been exceeded or substitutions have incorrect filetype (string/integer is allowed).","213":"Please enter a valid \'custom_backend_id\' parameter.","215":"Try contacting support (https://cp.unione.io/en/support).","216":"\'Metadata\' value should be a string or integer. Array for \'metadata\' key is expected.","217":"Maximum 10 metadata fields allowed.","218":"Metadata key length should be less than 64.","219":"Metadata value length should less than 1024.","220":"Make sure the metadata key is passed.","221":"Add the html <body> tag inside the <html> tag. The <body> tag is required to display HTML content.","223":"Add the \'content\' and specify a value for it.","224":"Make sure the substitution name is passed correctly.","227":"Please enter a valid \'smtp_pool_id\' parameter.","228":"Make sure the \'custom_backend_id\' is passed.","229":"Make sure the custom backend domain or tracking domain is passed.","230":"Make sure the default backend or tracking domain is passed.","303":"Verify that the event list is entered correctly.","304":"Make sure the URL is passed correctly.","305":"Make sure the URL is passed correctly.","601":"Check the passed template id.","602":"Check the passed template name.","603":"Use another name for this template.","604":"Delete already created templates to create new ones. The number of templates is limited to 10000.","701":"Invalid reason for blocking email. Please contact support (https://cp.unione.io/en/support).","901":"Each user has a maximum daily limit for sending to prevent abuse. This limit increases automatically in case of good delivery and is reset each night. You can smoothly increase amount of daily sending or you can contact support and ask for manual limit increase.","902":"Wait till next day or switch to paid tariff.","903":"Please confirm ownership (https://docs.unione.io/en/web-api-ref#domain-validate-verification-record) of all domains you want to send emails to or switch tariff to the paid one.","904":"Please try another card or contact support (https://cp.unione.io/en/support) to resolve the situation.","905":"Usually tariff allows paid overage. In this case it\'s prohibited, possibly due to unpaid invoice. Please check that all invoices are paid or contact support (https://cp.unione.io/en/support) to solve the problem.","906":"You have used the entire available block reset limit. Try it tomorrow or contact support (https://cp.unione.io/en/support) to increase the limit.","908":"Please contact support (https://cp.unione.io/en/support).","1000":"Specify the recipient address.","1001":"Please enter a valid recipient address.","1002":"Specify the sender address.","1003":"Verify the sender domain address.","1004":"Please enter a valid sender address.","1005":"The sender name must be shorter. Please correct this value.","1006":"Try to repeat the call the next day.","1007":"Use an address from a confirmed domain.","1100":"Specify the domain to be verified.","1101":"Please enter a valid domain value.","1102":"Please use another domain.","1103":"Use only your own domains.","1104":"Please use another domain.","1200":"Specify the domain for validating.","1201":"Please enter a valid domain value.","1202":"Make a domain/get-dns-records method call.","1203":"Please use another domain.","1301":"Specify the domain to be verified.","1302":"The value of limit must be an integer in the range of 1 - 100.","1303":"The value of offset must be greater than or equal to 0.","1304":"The value of offset must be an integer.","1305":"The value of limit must be an integer.","1306":"Please use another domain.","1350":"Make sure the \'domain\' is passed.","1351":"Please enter a valid domain value.","1352":"Make sure the \'domain\' is passed.","1353":"Please use another domain.","1400":"Specify the domain for validating.","1401":"Either you have mistyped the domain name and should correct it or DNS changes have not yet propagated through DNS and you should wait a couple of hours and try again. There should be A, AAAA or MX record for domain.","1402":"Please call domain/get-dns-records method for domain to be registered.","1403":"Please use another domain.","1506":"Please check your JSON and use object for \'headers\' field, not array or string or whatever.","1507":"Please limit \'headers\' to no more than 50 elements.","1508":"Make sure the template ID is correct.","1510":"Use string type for \'body.html\' parameter.","1511":"Use string type for \'body.plaintext\' parameter.","1512":"Use string type for \'body.amp\' parameter.","1516":"Check the correct substitutions in the \'body.html\' field.","1517":"Check the correct substitutions in the \'body.plaintext\' field.","1518":"Check the correct substitutions in the \'body.amp\' field.","1519":"Use string type for \'reply_to\' parameter.","1520":"Check if the email is correct.","1521":"Use string type for \'from_name\' parameter.","1522":"Use string type for \'subject\' parameter.","1523":"Please pass one of the following values: \'simple\', \'liquid\', \'velocity\' or \'none\'.","1524":"Please limit \'recipients\' to no more than 500 elements.","1525":"Make sure the date and time of the scheduled sending is no more than 24 hours in advance.","1526":"Please pass only one of the following values: \'velocity\', \'liquid\', \'simple\' or \'none\'.","1527":"Please pass only one of \'be\', \'de\', \'en\', \'es\', \'fr\', \'it\', \'kz\', \'ru\', \'pl\', \'pt\', \'ua\' values.","1528":"Please pass only one of \'be\', \'de\', \'en\', \'es\', \'fr\', \'it\', \'kz\', \'ru\', \'pl\', \'pt\', \'ua\' values.","1529":"Make sure the datetime is passed correctly.","1530":"Use string type for \'options.send_at\' parameter. Make sure the datetime is passed correctly.","1532":"Please limit tags to no more than 4 elements.","1533":"Delete already created tags to create new ones. The number of tags is limited to 10000.","1534":"The tags must be shorter. Please correct this value.","1535":"Make sure the tags is passed correctly.","1537":"Set parameter value to 1.","1538":"contact support (https://cp.unione.io/en/support) to set the flag.","1539":"contact support (https://cp.unione.io/en/support) to set the flag.","1540":"Metadata value size should less than 4000 bytes.","1541":"Metadata value size should less than 4000 bytes.","1542":"Metadata value size should less than 4000 bytes.","1543":"Use string type for \'global_metadata.campaign_id\' parameter.","1544":"Use string type for \'metadata.campaign_id\' parameter.","1545":"Make sure the \'metadata.campaign_id\' is passed correctly.","1546":"Check the correct substitutions in the \'subject\' field.","1547":"Check the correct substitutions in the \'from_name\' field.","1548":"Use object type for the \'options.unsubscribe_url\' parameter.","1549":"Make sure the substitution name is passed correctly.","1550":"Use object type for the \'options\' parameter.","1551":"Use string type for \'metadata\' and \'global_metadata\' parameters.","1554":"Make sure that the number of emails in \'headers[\'Cc\']\' is not exceeded.","1555":"Use only emails from the \'recipient\' parameter.","1556":"Make sure that the \'headers[\'To\']\' is passed.","1557":"Make sure that the number of emails in \'headers[\'To\']\' is not exceeded.","1558":"Use only emails from the \'recipient\' parameter.","1559":"BCC may contain only addresses on your verified domain.","1560":"Make sure that the number of emails in \\"To\\" and \\"Cc\\" headers is not exceeded.","1561":"Make sure the headers is passed correctly.","1562":"Make sure the headers is passed correctly.","1565":"Make sure the substitution name is passed correctly.","1566":"Make sure the substitution name is passed correctly.","1567":"Make sure the substitution name is passed correctly.","1568":"Make sure the substitution name is passed correctly.","1569":"Make sure the substitution name is passed correctly.","1570":"Make sure the substitution name is passed correctly.","1571":"Make sure the substitution name is passed correctly.","1572":"The key must be shorter. Please correct this value.","1573":"Make sure the key is correct.","1574":"To avoid this error please use an email on a verified domain.","1575":"Try contacting support (https://cp.unione.io/en/support).","1576":"Enter the subject.","1577":"Check that your message body is being passed properly.","1578":"Add the html <body> tag inside the <html> tag. The <body> tag is required to display HTML content.","1579":"The possible maximum substitutions number has been exceeded.","1580":"Make sure the substitution name is passed correctly.","1581":"This value should be a string or integer.","1582":"Maximum 10 metadata fields allowed.","1583":"\'Metadata\' value should be a string or integer.","1584":"Make sure the metadata key is passed.","1585":"Make sure the metadata key is passed correctly.","1586":"It should have 1024 characters or less.","1587":"Try contacting support (https://cp.unione.io/en/support).","1588":"Try contacting support (https://cp.unione.io/en/support).","1590":"Rename files with the forbidden symbols.","1591":"Rename files with the same names.","1592":"Add the \'content\' and specify a value for it.","1593":"Check the file and the file type again.","1594":"Rename files with the forbidden symbols.","1595":"Rename files with the same names.","1596":"Add the \'content\' and specify a value for it.","1597":"Check the file and the file type again.","1598":"To avoid this error please use only confirmed email addresses.","1599":"Try to repeat the call the next day.","1601":"Correct JSON structure.","1602":"Use non-blank name.","1603":"Use string type for \'name\' parameter.","1604":"Use shorter name.","1605":"Use unique project name or delete existing project first.","1606":"Set parameter value to true or false only.","1607":"Set parameter value to true or false only.","1608":"Please contact support (https://cp.unione.io/en/support).","1609":"Use string type for \'country\' parameter.","1610":"Make sure the ISO-3166 alpha-2 country code is passed correctly.","1611":"Use int type for \'backend_domain_id\' parameter.","1612":"Please enter a valid \'backend_domain_id\' parameter.","1613":"Please enter a valid \'backend_domain_id\' parameter.","1614":"Use int type for \'unsubscribe_page_id\' parameter.","1701":"Correct JSON structure.","1702":"Use non-blank name.","1703":"Use string type for \'name\' parameter.","1704":"Use shorter name.","1705":"Use unique project name or delete existing project first.","1706":"Don\'t forget to send either project_id or project_api_key.","1707":"Use correct project_api_key. You can get a list of projects with their keys with project/list (https://docs.unione.io/en/web-api-ref#project-list) method.","1708":"Use string type for \'project_api_key\'.","1709":"Set parameter value to true or false only.","1710":"Set parameter value to true or false only.","1711":"Please contact support (https://cp.unione.io/en/support).","1712":"Please use string type for project_id field.","1713":"Please pass only one of the two parameters: either project_id or project_api_key, not both.","1714":"Project id is invalid. Maybe it was deleted or never existed?","1715":"Please check that you pass the project_id you have received by calling project/create (https://docs.unione.io/en/web-api-ref#project-create) method.","1716":"Use string type for \'country\' parameter.","1717":"Make sure the ISO-3166 alpha-2 country code is passed correctly.","1718":"Use int type for \'backend_domain_id\' parameter.","1719":"Please enter a valid \'backend_domain_id\' parameter.","1720":"Please enter a valid \'backend_domain_id\' parameter.","1721":"Use int type for \'email_counter\' parameter.","1722":"Set parameter value equal to 0.","1723":"Use int type for \'email_counter_limit\' parameter.","1724":"Set parameter value greater than or equal to 0.","1725":"Set parameter value to default or permanent only.","1726":"Use int type for \'unsubscribe_page_id\' parameter.","1801":"Don\'t forget to send either project_id or project_api_key.","1802":"Use correct project_api_key. You can get a list of projects with their keys with project/list (https://docs.unione.io/en/web-api-ref#project-list) method.","1803":"Use string type for \'project_api_key\'.","1804":"Please pass only one of the two parameters: either project_id or project_api_key, not both.","1805":"Please use string type for project_id field.","1806":"Project id is invalid. Maybe it was deleted or never existed?","1807":"Please check that you pass the project_id you have received by calling project/create (https://docs.unione.io/en/web-api-ref#project-create) method.","1902":"Use string type for \'project_api_key\'.","1904":"Use correct numbers-only format for \'project_id\'.","1905":"Please use string type for project_id field.","2306":"Make sure the \'recipients\' is passed.","2307":"Use string type for \'recipients\' parameter.","2500":"Make sure the \'editor_type\' field is passed \'html\'.","2501":"Make sure the template is created in HTML editor.","2502":"Pass \'html\' in \'editor_type\' field.","2503":"Make sure the template ID is correct.","2504":"Please check your JSON and use object for \'headers\' field, not array or string or whatever.","2505":"Please limit headers to no more than 50 elements.","2514":"Use string type for \'reply_to\' parameter.","2515":"Check if the email is correct.","2516":"Please pass one of the following values: \'simple\', \'liquid\', \'velocity\' or \'none\'.","2517":"Use string type for \'subject\' parameter.","2518":"Check the correct substitutions in the \'subject\' field.","2519":"Use string type for \'from_name\' parameter.","2520":"Check the correct substitutions in the \'from_name\' field.","2521":"Make sure the substitution name is passed correctly.","2522":"Make sure the substitution name is passed correctly.","2523":"Make sure the substitution name is passed correctly.","2524":"Use string type for \'options\' parameter.","2525":"Make sure the substitution name is passed correctly.","2526":"Make sure the substitution name is passed correctly.","2527":"Use object type for the \'options.unsubscribe_url\' parameter.","2528":"Use shorter name.","2529":"Use shorter name.","2530":"Make sure the substitution name is passed correctly.","2531":"Make sure the substitution name is passed correctly.","2532":"Make sure the substitution name is passed correctly.","2533":"Make sure the substitution name is passed correctly.","2534":"Make sure the substitution name is passed correctly.","2535":"Make sure the substitution name is passed correctly.","2536":"Make sure the substitution name is passed correctly.","2600":"Set parameter value greater than or equal to 0.","2601":"Use int type for \'limit\' parameter.","2602":"Set parameter value greater than or equal to 0.","2603":"Use int type for \'offset\' parameter.","2700":"Use string type for \'url\' parameter.","2701":"Make sure the URL is passed.","2702":"Set parameter value greater than or equal to 0.","2703":"Use format \'json_post\' or \'json_post_gzip\' only.","2704":"Set parameter \'delivery_info\' value to 1\\\\0 either true\\\\false.","2705":"Set parameter \'single_event\' value to 1\\\\0 either true\\\\false.","2706":"Use int type for \'maxParallel\' parameter.","2707":"Use a valid value of 5 to 100.","2708":"Use array type for \'events\' parameter.","2709":"Use non-blank events.","2710":"Add \'email_status\' parameter.","2711":"Add valid email statuses in the value.","2712":"Add valid spam blocking events in the value.","2713":"Make sure the domain is passed correctly.","2714":"Set status \'active\' or \'disabled\'.","2715":"Use string type for \'status\' parameter.","2800":"Make sure the \'name\' is passed.","2801":"Use string type for \'name\' parameter.","2802":"Use string type for \'type\' parameter.","2803":"Use string type for \'content\' parameter.","2804":"Reduce the size of the \'content\' field to 7MB (9786710 bytes in base64).","2900":"Make sure the email is passed.","2901":"Use string type for \'email\' parameter.","2902":"Verify that email is entered correctly.","2903":"Set parameter value to true or false only.","3000":"Make sure the email is passed.","3001":"Use string type for \'email\' parameter.","3002":"Verify that email is entered correctly.","3003":"Email was not found in the suppression list.","3004":"Email cannot be removed from the suppression list. Probably because of the severe reason like \'blocked\'.","3100":"Make sure the email is passed.","3101":"Use string type for \'email\' parameter.","3102":"Verify that email is entered correctly.","3103":"Make sure the \'cause\' is passed.","3104":"Use string type for \'country\' parameter.","3105":"Please pass only one of the available values.","3106":"Use string type for \'created\' parameter.","3107":"Make sure the datetime is passed correctly.","3200":"Use string type for \'country\' parameter.","3201":"Please pass only one of the available values.","3202":"Use string type for \'source\' parameter.","3203":"Please pass only one of the available values.","3204":"Use string type for \'start_time\' parameter.","3205":"Make sure the datetime is passed correctly.","3206":"Use string type for \'cursor\' parameter.","3207":"Please pass only one of the available values.","3208":"Use int type for \'limit\' parameter.","3209":"Set parameter value greater than or equal to 0.","3300":"Make sure the \'start_time\' is passed.","3301":"Make sure the datetime is passed correctly.","3302":"Make sure the datetime is passed correctly.","3303":"Set parameter value greater than or equal to 0.","3304":"Use \'csv\' or \'csv_gzip\' format only.","3305":"Use \'delimiter\' \';\' or \',\' only.","3308":"Projects methods are disabled by default. To enable them,contact support (https://cp.unione.io/en/support).","3309":"Delete already created dumps to create new ones. The number of dumps is limited to 10.","3310":"Make sure \'start_time\' is in the allowed range.","3311":"Use int type for \'limit\' parameter.","3312":"Use string type for \'delimiter\' parameter.","3313":"Use string type for \'format\' parameter.","3315":"Use string type for \'aggregate\' parameter.","3316":"Can not use filter with \\"aggregate\\" parameter. Please pass only one of the available parameter.","3317":"Use array type for \'dumpFields\' parameter.","3318":"Please pass only one of the available values.","3319":"Invalid value, aggregate could be only \'day_status\' or empty. Please pass only one of the available values.","3400":"Please check your JSON and use object for \'filter\' field, not array or string or whatever.","3401":"Use string type for \'filter.job_id\' parameter.","3402":"Use string type for \'filter.status\' parameter.","3403":"Please enter a valid \'filter.status\' parameter.","3404":"Use string type for \'filter.email\' parameter.","3405":"Check if the email is correct.","3406":"Use string type for \'filter.email_from\' parameter.","3407":"Check if the email is correct.","3408":"Use string type for \'filter.domain\' parameter.","3409":"Please enter a valid domain value.","3410":"Make sure the domain is passed correctly.","3411":"Use string type for \'filter.delivery_status\' parameter.","3412":"Please enter a valid \'filter.delivery_status\' parameter.","3413":"Use string type for \'filter.campaign_id\' parameter.","3414":"The data in request should be no more than 128 bit.","3500":"Make sure the \'dump_id\' is passed.","3501":"Make sure the \'dump_id\' is passed correctly.","3502":"Dump ID is invalid. Maybe it was deleted or never existed?","3600":"Make sure the \'dump_id\' is passed.","3601":"Make sure the \'dump_id\' is passed correctly.","3602":"Dump ID is invalid. Maybe it was deleted or never existed?","3700":"Make sure the \'tag_id\' is passed.","3701":"Use int type for \'tag_id\' parameter.","3800":"Reduce the the numbers of concurrent requests to 4.","3801":"Make sure the email is passed.","3802":"Use string type for \'email\' parameter.","3803":"Try to repeat the call the next day.","3900":"Use string type for \'idempotence_key\' parameter.","4000":"Use the main account API key.","4001":"Pass an email address.","4002":"Use string type for \'email\' parameter.","4003":"Pass a valid email address.","4004":"Delete it and call confirmed-email/create again to send a new confirmation link.","4005":"No action is required.","4006":"Delete an address before adding a new one.","4100":"Use the main account API key.","4101":"Pass an email address.","4102":"Use string type for \'email\' parameter.","4103":"Pass a valid email address.","4104":"Use confirmed-email/list to get the current list.","4200":"Use the main account API key.","4300":"Use the account API key.","10001":"Use shorter name.","10002":"Use shorter name.","10003":"Make sure the substitution name is passed correctly.","10004":"Make sure the substitution name is passed correctly.","10005":"Make sure the substitution name is passed correctly.","10006":"Make sure the substitution name is passed correctly.","10007":"Make sure the substitution name is passed correctly.","10008":"Make sure the substitution name is passed correctly.","10009":"Make sure the substitution name is passed correctly."},"roots":{"email/send":"EmailSendRequest"},"models":{"EmailSendRequest":[["message",true,"MessageObject","one"]],"MessageObject":[["recipients",true,"MessageObjectRecipientsItem","list"]],"MessageObjectRecipientsItem":[["email",true,null,"one"]]}}'
|
|
12
|
+
# Longest part of a server-provided text (API message, response body) quoted in an error message
|
|
13
|
+
MAX_QUOTE = 500
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
# API method of a path: '/email/send.json' -> 'email/send'
|
|
17
|
+
# @return [String]
|
|
18
|
+
def method_of(path)
|
|
19
|
+
path.to_s.gsub(%r{\A/+|/+\z}, '').sub(/\.json\z/, '')
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# How to fix an API error: the errors table of the docs, nil for an unknown code
|
|
23
|
+
# @return [String, nil]
|
|
24
|
+
def hint(code)
|
|
25
|
+
code.nil? ? nil : data['hints'][code.to_s]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The first required field missing in the body (decoded JSON) of a method, e.g. 'message.recipients[0].email'
|
|
29
|
+
# @return [String, nil]
|
|
30
|
+
def missing(api_method, body)
|
|
31
|
+
root = data['roots'][api_method]
|
|
32
|
+
root ? walk(body, root, '') : nil
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Message of an API error: 'email/send: Unknown recipient (API error 204, HTTP 400). Fix: ... See ...'
|
|
36
|
+
# @return [String]
|
|
37
|
+
def api_message(api_method, message, status, code)
|
|
38
|
+
text = quote(message.to_s.sub(/[. ]+\z/, ''))
|
|
39
|
+
out = +(api_method.empty? ? '' : "#{api_method}: ")
|
|
40
|
+
out << (text.empty? ? 'API error' : text)
|
|
41
|
+
out << (code.nil? ? " (HTTP #{status})." : " (API error #{code}, HTTP #{status}).")
|
|
42
|
+
fix = hint(code)
|
|
43
|
+
out << " Fix: #{fix.sub(/[. ]+\z/, '')}." if fix
|
|
44
|
+
docs = data['docs'].to_s
|
|
45
|
+
out << " See #{docs}" unless docs.empty?
|
|
46
|
+
out
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The text cut to MAX_QUOTE characters, for the error messages
|
|
50
|
+
# @return [String]
|
|
51
|
+
def quote(text)
|
|
52
|
+
text = text.to_s
|
|
53
|
+
text.length <= MAX_QUOTE ? text : "#{text[0, MAX_QUOTE]}... (#{text.length} chars)"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# API error code of the "code" field: an Integer or a String of digits, nil for anything else
|
|
57
|
+
# @return [Integer, nil]
|
|
58
|
+
def code_of(value)
|
|
59
|
+
case value
|
|
60
|
+
when Integer then value >= 0 ? value : nil
|
|
61
|
+
when String then value.match?(/\A[0-9]+\z/) ? value.to_i : nil
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def data
|
|
68
|
+
@data ||= JSON.parse(ERRORS_JSON)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def walk(value, model, path)
|
|
72
|
+
return nil unless value.is_a?(Hash)
|
|
73
|
+
|
|
74
|
+
(data['models'][model] || []).each do |wire, required, nested, container|
|
|
75
|
+
at = path.empty? ? wire : "#{path}.#{wire}"
|
|
76
|
+
field = value[wire]
|
|
77
|
+
# absent, nil, '' or [] counts as missing
|
|
78
|
+
if field.nil? || field == '' || field == []
|
|
79
|
+
return at if required
|
|
80
|
+
|
|
81
|
+
next
|
|
82
|
+
end
|
|
83
|
+
next if nested.nil?
|
|
84
|
+
|
|
85
|
+
found = walk_field(field, nested, container, at)
|
|
86
|
+
return found if found
|
|
87
|
+
end
|
|
88
|
+
nil
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def walk_field(field, nested, container, at)
|
|
92
|
+
case container
|
|
93
|
+
when 'one'
|
|
94
|
+
walk(field, nested, at)
|
|
95
|
+
when 'list'
|
|
96
|
+
return nil unless field.is_a?(Array)
|
|
97
|
+
|
|
98
|
+
field.each_with_index do |item, index|
|
|
99
|
+
found = walk(item, nested, "#{at}[#{index}]")
|
|
100
|
+
return found if found
|
|
101
|
+
end
|
|
102
|
+
nil
|
|
103
|
+
else
|
|
104
|
+
return nil unless field.is_a?(Hash)
|
|
105
|
+
|
|
106
|
+
field.each do |key, item|
|
|
107
|
+
found = walk(item, nested, "#{at}.#{key}")
|
|
108
|
+
return found if found
|
|
109
|
+
end
|
|
110
|
+
nil
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
data/lib/unione/version.rb
CHANGED
data/lib/unione.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: unione
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.3.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- UniOne
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description:
|
|
14
14
|
email:
|
|
@@ -149,6 +149,7 @@ files:
|
|
|
149
149
|
- lib/unione/client/api_error.rb
|
|
150
150
|
- lib/unione/client/configuration.rb
|
|
151
151
|
- lib/unione/client/decode.rb
|
|
152
|
+
- lib/unione/client/error_info.rb
|
|
152
153
|
- lib/unione/common/model/attachments_array_item.rb
|
|
153
154
|
- lib/unione/common/model/body_object.rb
|
|
154
155
|
- lib/unione/common/model/email_counter_mode.rb
|