hubspot-api-client 14.3.0 → 14.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bab00d972a316aadaced5d712c7abe5d86ddab1236caa123a8b44d0b9cfcfeac
4
- data.tar.gz: 9ed639a810e45d0601188e0b56b102a71a51312580aa7f12ceca3a0ba5ea291d
3
+ metadata.gz: 9767c7c7c1e949bd93690f2c111d3e47141be1ca597f99091402eba1ca3f968e
4
+ data.tar.gz: f435e7e0e1a4bff0d0f9e57a1dc284a9a48293e036bd8c1cfbf0e1b408825f9a
5
5
  SHA512:
6
- metadata.gz: d478dd41e3b787640f72f395b6a7c79e7e05819c2ed507fe03a2594b190345b0c31b57299a15ceac472bd1b6ad0ce065c40cd57c41fc040f8e981aae0b7c67df
7
- data.tar.gz: 469b1fe3d0660baf67e66e44fbcbb0968292038663bd1918312bcb123de055bcaf24178e0fe95b1a29885f06ae34a88310251669d7d9efa6649049822c26de91
6
+ metadata.gz: b383cf4b93fb791e6869120c1b4c7952a2b6242a355c4d958c1de1523ccee00aeac3210c0949ea7e22065f2ff2c67304a31eaeb4c788dcbd0d5e95529f19755a
7
+ data.tar.gz: 344aafe31b45ff09eec055504ab455a0548ed30c29bd51dba84ab33918c68390e4f442e4302eef20075cc6b1c802e846a7dc5008c266bfbdf7f84f4c4243ba53
data/CHANGELOG.md CHANGED
@@ -5,6 +5,16 @@ 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
+
13
+ ## [14.4.0] - 2022-09-27
14
+ ### Changed
15
+
16
+ - handling ApiError by passing a block
17
+
8
18
  ## [14.3.0] - 2022-08-31
9
19
  ### Changed
10
20
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hubspot-api-client (14.3.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)
@@ -41,13 +41,13 @@ GEM
41
41
  rspec-mocks (~> 3.11.0)
42
42
  rspec-core (3.11.0)
43
43
  rspec-support (~> 3.11.0)
44
- rspec-expectations (3.11.0)
44
+ rspec-expectations (3.11.1)
45
45
  diff-lcs (>= 1.2.0, < 2.0)
46
46
  rspec-support (~> 3.11.0)
47
47
  rspec-mocks (3.11.1)
48
48
  diff-lcs (>= 1.2.0, < 2.0)
49
49
  rspec-support (~> 3.11.0)
50
- rspec-support (3.11.0)
50
+ rspec-support (3.11.1)
51
51
  typhoeus (1.4.0)
52
52
  ethon (>= 0.9.0)
53
53
  vcr (3.0.3)
data/README.md CHANGED
@@ -160,6 +160,18 @@ api_response = client.crm.schemas.core_api.create(body: body)
160
160
 
161
161
  ### Error handling
162
162
 
163
+ for new discovery classes since version 14.4
164
+
165
+ #### You can rescue an ApiError by passing a block to the method
166
+
167
+ ```ruby
168
+ require 'hubspot-api-client'
169
+
170
+ client = Hubspot::Client.new(access_token: 'your_access_token')
171
+
172
+ contacts = client.crm.contacts.basic_api.get_page { |error| error.message }
173
+ ```
174
+
163
175
  #### You can set number of retry attempts and delay in seconds before retry on specific status code of response.
164
176
 
165
177
  Available params:
@@ -184,6 +196,28 @@ api_client = ::Hubspot::Crm::Companies::ApiClient.new(config)
184
196
  basic_api = ::Hubspot::Crm::Companies::BasicApi.new(api_client)
185
197
  ```
186
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
+
187
221
  ### Sample apps
188
222
 
189
223
  Please, take a look at our [Sample apps](https://github.com/HubSpot/sample-apps-list)
@@ -48,13 +48,50 @@ module Hubspot
48
48
  codegen_api_class.gsub(/(.*)::.*/, '\1')
49
49
  end
50
50
 
51
+ def call_api(api_method, params_to_pass)
52
+ api.public_send(api_method, *params_to_pass)
53
+ end
54
+
55
+ def call_api_with_rescue(api_method, params_to_pass)
56
+ error = Kernel.const_get("#{codegen_module_name}::ApiError")
57
+ call_api(api_method, params_to_pass)
58
+ rescue error => e
59
+ yield(e)
60
+ end
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
+
51
88
  def define_methods
52
89
  define_api_methods
53
90
  end
54
91
 
55
92
  def define_api_methods
56
93
  api_methods.each do |api_method|
57
- self.class.define_method(api_method) do |params = {}|
94
+ self.class.define_method(api_method) do |params = {}, &block|
58
95
  params_with_defaults = params
59
96
  params_with_defaults[:opts] ||= {}
60
97
  params_with_defaults[:opts][:auth_names] = if base_params[:access_token]
@@ -71,7 +108,7 @@ module Hubspot
71
108
 
72
109
  signature_param_names = signature_params.map { |_, param| param }
73
110
  params_with_defaults.each do |param_name, param_value|
74
- 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
75
112
  end
76
113
 
77
114
  params_to_pass = signature_params.map do |req, param|
@@ -83,7 +120,9 @@ module Hubspot
83
120
  params_with_defaults[param]
84
121
  end
85
122
 
86
- api.public_send(api_method, *params_to_pass)
123
+ return call_api_with_retry(api_method, params_to_pass, params[:retry], &block) unless params[:retry].nil?
124
+ return call_api_with_rescue(api_method, params_to_pass, &block) unless block.nil?
125
+ call_api(api_method, params_to_pass)
87
126
  end
88
127
  end
89
128
  end
@@ -1,3 +1,3 @@
1
1
  module Hubspot
2
- VERSION = '14.3.0'
2
+ VERSION = '14.5.0'
3
3
  end
@@ -22,6 +22,23 @@ describe 'Hubspot::Discovery::BaseApiClient' do
22
22
 
23
23
  def update_with_http_info
24
24
  end
25
+
26
+ def raise_error
27
+ raise Hubspot::ApiError
28
+ end
29
+
30
+ def raise_error_with_http_info
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
25
42
  end
26
43
 
27
44
  class Hubspot::ApiClient
@@ -29,6 +46,16 @@ describe 'Hubspot::Discovery::BaseApiClient' do
29
46
  end
30
47
  end
31
48
 
49
+ class Hubspot::ApiError < ::StandardError
50
+ def message
51
+ 'test error'
52
+ end
53
+
54
+ def code
55
+ 429
56
+ end
57
+ end
58
+
32
59
  class Hubspot::SimplePublicObjectInput
33
60
  attr_reader :name, :email
34
61
 
@@ -65,6 +92,13 @@ describe 'Hubspot::Discovery::BaseApiClient' do
65
92
 
66
93
  it { is_expected.to eq('got test_id: test_id_value, opts: {:auth_names=>"oauth2", :limit=>5}') }
67
94
  end
95
+
96
+ context 'with error handle block' do
97
+ subject(:get) { client.get(params) { |e| e.message } }
98
+ let(:params) { {test_id: 'test_id_value', limit: 10} }
99
+
100
+ it { is_expected.to eq('got test_id: test_id_value, opts: {:auth_names=>"oauth2", :limit=>10}') }
101
+ end
68
102
  end
69
103
 
70
104
  describe '#update' do
@@ -93,5 +127,40 @@ describe 'Hubspot::Discovery::BaseApiClient' do
93
127
 
94
128
  it { is_expected.to eq('updated test_id: test_id_value, name: test_name, email: test_email, opts: {:auth_names=>"oauth2", :limit=>10}') }
95
129
  end
130
+
131
+ context 'with block' do
132
+ subject(:update) { client.update(params) { |e| e.message } }
133
+ let(:params) { {test_id: 'test_id_value', body: body, limit: 10} }
134
+
135
+ it { is_expected.to eq('updated test_id: test_id_value, name: test_name, email: test_email, opts: {:auth_names=>"oauth2", :limit=>10}') }
136
+ end
137
+ end
138
+
139
+ describe '#raise_error' do
140
+ subject(:raise_error) { client.raise_error { |e| e.message } }
141
+
142
+ it { is_expected.to eq('test error') }
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
96
165
  end
97
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.3.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-08-31 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