amazon-ads 0.3.0 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 01b624dd728e5d50d55c9d199671a1730ce61c260210d0d13c2173241edc297e
4
- data.tar.gz: 6a8cc4aa441d12b1c12905fbec987935c82807b2495a520e0d304830c36ceadc
3
+ metadata.gz: 5e6d25250dfeb3f930a2c3253a1f0ed8da99226ba0d5290948b8e3ca8cfba483
4
+ data.tar.gz: 073acd8350a1446de8ea3a2ec207491506d3c996ffbad5d444b4bec2c77e2b7e
5
5
  SHA512:
6
- metadata.gz: 8afd9e71ec2e8006cf99de7f66bc202cbc8589fbe9809a591d42a7948ed032dfad3d7b1d2d9c536548643de93a15824e6fa94724424e2d0c76c5ace11610387e
7
- data.tar.gz: 6cca951e838bf718be509b0f0882ef54c4e6ee8d2955d25982f0ab9d6a595e08535b4f3ed63f17b84103fef1a3f813c23d282e6e34f253afbbbe688bbcaab061
6
+ metadata.gz: ed62d0a64d560078cd3ca2d3d564e5cac7ae3e1da59ba00766fd4f0701b362d89980e879347e91f2ac5049176769580d9ce8cdc0e796770fdd24180a527023b4
7
+ data.tar.gz: 4e083231e7f90868aa5207b124f8affc757b452b8ce77d03319f83945822fa754e7e7b0286e9fb1b4271db327780b61b59746ef86dae28ba5fe4fcd44b6c91cc
data/README.md CHANGED
@@ -54,6 +54,30 @@ campaigns = AmazonAds::Campaigns.new(
54
54
  campaigns.list_campaigns
55
55
  ```
56
56
 
57
+ ## Error handling
58
+
59
+ Responses with a 4xx or 5xx status raise `AmazonAds::Error`, which carries the full response:
60
+
61
+ ```ruby
62
+ begin
63
+ campaigns.list_campaigns
64
+ rescue AmazonAds::Error => e
65
+ e.status # 400
66
+ e.response.body # Amazon's error document
67
+ end
68
+ ```
69
+
70
+ The error supports pattern matching on status:
71
+
72
+ ```ruby
73
+ case error
74
+ in status: 429 then backoff
75
+ in status: 500..599 then retry
76
+ end
77
+ ```
78
+
79
+ Network failures (`HTTP::ConnectionError`, `HTTP::TimeoutError`) raise as-is, whether or not retries are configured.
80
+
57
81
  ## Development
58
82
 
59
83
  See [AGENTS.md](AGENTS.md).
@@ -49,13 +49,9 @@ module AmazonAds
49
49
  .headers(default_headers)
50
50
  .use(:auto_inflate)
51
51
 
52
- if retries.zero?
53
- client.use(:raise_error)
54
- else
55
- client
56
- .use(raise_error: { ignore: [429] })
57
- .retriable(tries: retries + 1, retry_statuses: [429])
58
- end
52
+ return client if retries.zero?
53
+
54
+ client.retriable(tries: retries + 1, retry_statuses: [429])
59
55
  end
60
56
 
61
57
  private
@@ -64,7 +60,17 @@ module AmazonAds
64
60
  def request(method, path, **options)
65
61
  url = endpoint
66
62
  url.path = path
67
- http.request(method, url.to_s, **options)
63
+ response = http.request(method, url.to_s, **options)
64
+ raise Error.build(response) if response.code >= 400
65
+
66
+ response
67
+ rescue HTTP::OutOfRetriesError => e
68
+ response = e.response
69
+ raise Error.build(response) if response
70
+
71
+ # No response means a transport error; re-raise as if never retried
72
+ cause = e.cause #: Exception
73
+ raise cause
68
74
  end
69
75
 
70
76
  #: () -> Hash[String, String]
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "http"
4
+
5
+ module AmazonAds
6
+ # Raised when the API or LWA responds with a 4xx or 5xx
7
+ class Error < StandardError
8
+ # The failed response
9
+ attr_reader :response #: HTTP::Response?
10
+
11
+ #: (HTTP::Response) -> Error
12
+ def self.build(response)
13
+ response.flush # memoize the body while the connection is still readable
14
+ new(response.status.to_s, response)
15
+ end
16
+
17
+ #: (?String?, ?HTTP::Response?) -> void
18
+ def initialize(msg = nil, response = nil)
19
+ @response = response
20
+ super(msg)
21
+ end
22
+
23
+ #: () -> Integer?
24
+ def status
25
+ response&.status&.code
26
+ end
27
+
28
+ # Supports pattern matching on status
29
+ #
30
+ # case error
31
+ # in status: 429 then backoff
32
+ # in status: 500..599 then retry
33
+ # end
34
+ #
35
+ #: (Array[Symbol]?) -> Hash[Symbol, untyped]
36
+ def deconstruct_keys(keys)
37
+ hash = { status: status }
38
+ keys ? hash.slice(*keys) : hash
39
+ end
40
+ end
41
+ end
@@ -26,16 +26,14 @@ module AmazonAds
26
26
  # Requests a fresh access token from LWA
27
27
  #: () -> Hash[String, untyped]
28
28
  def request
29
- http.post(URL, form: params).parse
29
+ response = @http.post(URL, form: params)
30
+ raise Error.build(response) if response.code >= 400
31
+
32
+ response.parse
30
33
  end
31
34
 
32
35
  private
33
36
 
34
- #: () -> HTTP::Client
35
- def http
36
- @http.use(:raise_error)
37
- end
38
-
39
37
  #: () -> Hash[Symbol, String]
40
38
  def params
41
39
  {
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AmazonAds
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon-ads
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hakan Ensari
@@ -83,6 +83,7 @@ files:
83
83
  - lib/amazon_ads/apis/recommendations.rb
84
84
  - lib/amazon_ads/apis/reporting.rb
85
85
  - lib/amazon_ads/apis/targets.rb
86
+ - lib/amazon_ads/error.rb
86
87
  - lib/amazon_ads/lwa.rb
87
88
  - lib/amazon_ads/version.rb
88
89
  homepage: https://github.com/lineofflight/amazon-ads-ruby