hubspot-api-client 14.4.0 → 14.5.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: 3229748b2d14e474894aecf5f28d60ae7ace8f5dda38d98daa34a2bbda93e033
4
- data.tar.gz: 2867ef54025f5caa340e9ad28e7fafa940cb88bc8b11142b6f75d45df8e6829a
3
+ metadata.gz: 9767c7c7c1e949bd93690f2c111d3e47141be1ca597f99091402eba1ca3f968e
4
+ data.tar.gz: f435e7e0e1a4bff0d0f9e57a1dc284a9a48293e036bd8c1cfbf0e1b408825f9a
5
5
  SHA512:
6
- metadata.gz: 13932717d618997bff59721afe32a3480b30f579d299837d55f925cd1b47f300a70143245d954fadc14b8ca408587051abe355900310d34543e243ea866129a6
7
- data.tar.gz: 59711b28a7e2b9fe86fe610434d4bcbb1065ece4812c5448e5856ba53fa77ba275eb6df9bfd24299fae494e98858ddfc94dbd5a4f29c51412be942a86d931d8f
6
+ metadata.gz: b383cf4b93fb791e6869120c1b4c7952a2b6242a355c4d958c1de1523ccee00aeac3210c0949ea7e22065f2ff2c67304a31eaeb4c788dcbd0d5e95529f19755a
7
+ data.tar.gz: 344aafe31b45ff09eec055504ab455a0548ed30c29bd51dba84ab33918c68390e4f442e4302eef20075cc6b1c802e846a7dc5008c266bfbdf7f84f4c4243ba53
data/CHANGELOG.md CHANGED
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [14.5.0] - 2022-09-30
9
+ ### Changed
10
+
11
+ - handling ApiError with retries
12
+
8
13
  ## [14.4.0] - 2022-09-27
9
14
  ### Changed
10
15
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hubspot-api-client (14.4.0)
4
+ hubspot-api-client (14.5.0)
5
5
  json (~> 2.1, >= 2.1.0)
6
6
  require_all (~> 3.0.0)
7
7
  typhoeus (~> 1.4.0)
data/README.md CHANGED
@@ -196,6 +196,28 @@ api_client = ::Hubspot::Crm::Companies::ApiClient.new(config)
196
196
  basic_api = ::Hubspot::Crm::Companies::BasicApi.new(api_client)
197
197
  ```
198
198
 
199
+ for new discovery classes since version 14.5
200
+
201
+ Available params:
202
+ - max_retries (maximum number of retries)
203
+ - seconds_delay (pause in seconds between retries)
204
+ - passing a block (block that handles errors occured)
205
+
206
+
207
+ ```ruby
208
+ require 'hubspot-api-client'
209
+
210
+ # Set handlers of statuses you want to handle
211
+ retry_config = {
212
+ 500..530 => { max_retries: 2, seconds_delay: 2 },
213
+ 400 => { max_retries: 3, seconds_delay: 3 }
214
+ }
215
+
216
+ client = Hubspot::Client.new(access_token: 'your_access_token')
217
+
218
+ contacts = client.crm.contacts.basic_api.get_page(retry: retry_config) { |error| error.code }
219
+ ```
220
+
199
221
  ### Sample apps
200
222
 
201
223
  Please, take a look at our [Sample apps](https://github.com/HubSpot/sample-apps-list)
@@ -59,6 +59,32 @@ module Hubspot
59
59
  yield(e)
60
60
  end
61
61
 
62
+ def call_api_with_retry(api_method, params_to_pass, retry_config, &block)
63
+ call_api_with_rescue(api_method, params_to_pass) do |error|
64
+ opts = retry_config.detect{ |k,v| k === error.code }&.last
65
+ retries = opts&.dig(:max_retries) || 5
66
+
67
+ block.call(error) unless block.nil?
68
+
69
+ response = error
70
+
71
+ while retries > 0 && opts
72
+ sleep opts[:seconds_delay] if opts[:seconds_delay]
73
+ response = call_api_with_rescue(api_method, params_to_pass) do |e|
74
+ block.call(e) unless block.nil?
75
+ e
76
+ end
77
+
78
+ return response unless response.respond_to?(:code)
79
+
80
+ opts = retry_config.detect{ |k,v| k === response.code }&.last
81
+ retries -= 1
82
+ end
83
+
84
+ response
85
+ end
86
+ end
87
+
62
88
  def define_methods
63
89
  define_api_methods
64
90
  end
@@ -82,7 +108,7 @@ module Hubspot
82
108
 
83
109
  signature_param_names = signature_params.map { |_, param| param }
84
110
  params_with_defaults.each do |param_name, param_value|
85
- params_with_defaults[:opts][param_name] = param_value if !signature_param_names.include?(param_name) && param_name != :body
111
+ params_with_defaults[:opts][param_name] = param_value if !signature_param_names.include?(param_name) && param_name != :body && param_name != :retry
86
112
  end
87
113
 
88
114
  params_to_pass = signature_params.map do |req, param|
@@ -94,6 +120,7 @@ module Hubspot
94
120
  params_with_defaults[param]
95
121
  end
96
122
 
123
+ return call_api_with_retry(api_method, params_to_pass, params[:retry], &block) unless params[:retry].nil?
97
124
  return call_api_with_rescue(api_method, params_to_pass, &block) unless block.nil?
98
125
  call_api(api_method, params_to_pass)
99
126
  end
@@ -1,3 +1,3 @@
1
1
  module Hubspot
2
- VERSION = '14.4.0'
2
+ VERSION = '14.5.0'
3
3
  end
@@ -29,6 +29,16 @@ describe 'Hubspot::Discovery::BaseApiClient' do
29
29
 
30
30
  def raise_error_with_http_info
31
31
  end
32
+
33
+ def raise_error_on_third_call
34
+ @calls_count ||= 0
35
+ @calls_count += 1
36
+ raise Hubspot::ApiError if @calls_count < 3
37
+ 'ok'
38
+ end
39
+
40
+ def raise_error_on_third_call_with_http_info
41
+ end
32
42
  end
33
43
 
34
44
  class Hubspot::ApiClient
@@ -40,6 +50,10 @@ describe 'Hubspot::Discovery::BaseApiClient' do
40
50
  def message
41
51
  'test error'
42
52
  end
53
+
54
+ def code
55
+ 429
56
+ end
43
57
  end
44
58
 
45
59
  class Hubspot::SimplePublicObjectInput
@@ -127,4 +141,26 @@ describe 'Hubspot::Discovery::BaseApiClient' do
127
141
 
128
142
  it { is_expected.to eq('test error') }
129
143
  end
144
+
145
+ describe '#raise_error_on_third_call' do
146
+ subject(:raise_error_on_third_call) { client.raise_error_on_third_call(retry: retry_config) }
147
+
148
+ context 'with 2 retries' do
149
+ let(:retry_config) { {429 => { max_retries: 2 }} }
150
+
151
+ it { is_expected.to eq('ok') }
152
+
153
+ context 'with range config' do
154
+ let(:retry_config) { {429..442 => { max_retries: 2 }} }
155
+
156
+ it { is_expected.to eq('ok') }
157
+ end
158
+ end
159
+
160
+ context 'with 1 retry' do
161
+ let(:retry_config) { {429 => { max_retries: 1 }} }
162
+
163
+ it { is_expected.to have_attributes(code: 429, message: 'test error') }
164
+ end
165
+ end
130
166
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hubspot-api-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 14.4.0
4
+ version: 14.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - HubSpot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-27 00:00:00.000000000 Z
11
+ date: 2022-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus