zapsmtp 0.1.0.beta3

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.
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZapSMTP
4
+ # An error answer of the API, or no answer at all (ConnectionError). Every
5
+ # error the library raises is a ZapSMTP::Error.
6
+ class Error < StandardError
7
+ # @return [Integer] the HTTP status; 0 when there was no answer
8
+ attr_reader :status
9
+ # @return [String] the API's error code, e.g. "invalid_parameter"; "" when there is none
10
+ attr_reader :code
11
+ # @return [String] the error type, e.g. "invalid_request_error"; "" when there is none
12
+ attr_reader :type
13
+ # @return [String, nil] X-Request-Id: quote it to support
14
+ attr_reader :request_id
15
+ # @return [Hash{String => String}, nil] the response headers, names in lower case
16
+ attr_reader :headers
17
+ # @return [Object] the parsed error body (a Hash with symbol keys), or its text
18
+ attr_reader :body
19
+
20
+ def initialize(message = nil, status: 0, code: "", type: "", request_id: nil, headers: nil, body: nil)
21
+ super(message)
22
+ @status = status
23
+ @code = code
24
+ @type = type
25
+ @request_id = request_id
26
+ @headers = headers
27
+ @body = body
28
+ end
29
+
30
+ # The error class for an error answer.
31
+ #
32
+ # @api private
33
+ def self.from_response(status, body, headers)
34
+ hash = body.is_a?(Hash) ? body : {}
35
+ error = hash[:error].is_a?(Hash) ? hash[:error] : {}
36
+ code = error[:code] || hash[:name] || ""
37
+ message = error[:message] || hash[:message] || "HTTP #{status}"
38
+ class_for(status).new(message.to_s, status: status, code: code.to_s, type: (error[:type] || "").to_s,
39
+ request_id: headers["x-request-id"], headers: headers, body: body)
40
+ end
41
+
42
+ # @api private
43
+ def self.class_for(status)
44
+ case status
45
+ when 401 then AuthenticationError
46
+ when 403 then PermissionError
47
+ when 404 then NotFoundError
48
+ when 409 then ConflictError
49
+ when 400, 413, 422 then ValidationError
50
+ when 429 then RateLimitError
51
+ when 500.. then ServerError
52
+ else Error
53
+ end
54
+ end
55
+ end
56
+
57
+ # 401: the API key is missing or invalid.
58
+ class AuthenticationError < Error; end
59
+
60
+ # 403: the key may not do this, or the domain is not verified.
61
+ class PermissionError < Error; end
62
+
63
+ # 404: no such resource.
64
+ class NotFoundError < Error; end
65
+
66
+ # 409: a conflict, e.g. the same Idempotency-Key with another request.
67
+ class ConflictError < Error; end
68
+
69
+ # 400, 413, 422: the request is wrong.
70
+ class ValidationError < Error; end
71
+
72
+ # 429: too many requests, or a quota is used up.
73
+ class RateLimitError < Error
74
+ # @return [Float, nil] seconds the API asked to wait (Retry-After), when it said
75
+ def retry_after
76
+ Retry.retry_after(headers)
77
+ end
78
+ end
79
+
80
+ # 5xx: the API failed.
81
+ class ServerError < Error; end
82
+
83
+ # No answer: a network failure or a timeout. #cause holds the underlying error.
84
+ class ConnectionError < Error; end
85
+
86
+ # A webhook request whose signature is missing, wrong or too old, or whose body is not JSON.
87
+ class WebhookVerificationError < Error; end
88
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Generated by sdks/codegen. Do not edit: run node sdks/codegen/generate.mjs.
4
+
5
+ module ZapSMTP
6
+ # The ZapSMTP API client.
7
+ #
8
+ # zap = ZapSMTP::Client.new(api_key: "zap_...") # or set ZAP_API_KEY
9
+ # zap.emails.send(from: "Acme <hello@acme.com>", to: "jane@example.org", subject: "Hi", html: "<p>Hi</p>")
10
+ class Client < BaseClient
11
+ # @return [Resources::Emails]
12
+ attr_reader :emails
13
+
14
+ # @return [Resources::Threads]
15
+ attr_reader :threads
16
+
17
+ # @return [Resources::Inbound]
18
+ attr_reader :inbound
19
+
20
+ # @return [Resources::Simulator]
21
+ attr_reader :simulator
22
+
23
+ # @return [Resources::Stats]
24
+ attr_reader :stats
25
+
26
+ # @return [Resources::Domains]
27
+ attr_reader :domains
28
+
29
+ # @return [Resources::ApiKeys]
30
+ attr_reader :api_keys
31
+
32
+ # @return [Resources::SmtpCredentials]
33
+ attr_reader :smtp_credentials
34
+
35
+ # @return [Resources::Webhooks]
36
+ attr_reader :webhooks
37
+
38
+ # @return [Resources::Templates]
39
+ attr_reader :templates
40
+
41
+ # @return [Resources::Suppressions]
42
+ attr_reader :suppressions
43
+
44
+ # @return [Resources::DedicatedIps]
45
+ attr_reader :dedicated_ips
46
+
47
+ # @return [Resources::Logs]
48
+ attr_reader :logs
49
+
50
+ # @return [Resources::Usage]
51
+ attr_reader :usage
52
+
53
+ # @return [Resources::TemplateModules]
54
+ attr_reader :template_modules
55
+
56
+ # @return [Resources::TemplateAssets]
57
+ attr_reader :template_assets
58
+
59
+ # @return [Resources::TemplateTimers]
60
+ attr_reader :template_timers
61
+
62
+ # @return [Resources::Actions]
63
+ attr_reader :actions
64
+
65
+ # @return [Resources::Variables]
66
+ attr_reader :variables
67
+
68
+ private
69
+
70
+ def build_resources
71
+ @emails = Resources::Emails.new(self)
72
+ @threads = Resources::Threads.new(self)
73
+ @inbound = Resources::Inbound.new(self)
74
+ @simulator = Resources::Simulator.new(self)
75
+ @stats = Resources::Stats.new(self)
76
+ @domains = Resources::Domains.new(self)
77
+ @api_keys = Resources::ApiKeys.new(self)
78
+ @smtp_credentials = Resources::SmtpCredentials.new(self)
79
+ @webhooks = Resources::Webhooks.new(self)
80
+ @templates = Resources::Templates.new(self)
81
+ @suppressions = Resources::Suppressions.new(self)
82
+ @dedicated_ips = Resources::DedicatedIps.new(self)
83
+ @logs = Resources::Logs.new(self)
84
+ @usage = Resources::Usage.new(self)
85
+ @template_modules = Resources::TemplateModules.new(self)
86
+ @template_assets = Resources::TemplateAssets.new(self)
87
+ @template_timers = Resources::TemplateTimers.new(self)
88
+ @actions = Resources::Actions.new(self)
89
+ @variables = Resources::Variables.new(self)
90
+ end
91
+ end
92
+ end