hubspot-api-client 14.1.0 → 14.5.2

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: 258c55c06d2bb92967ba618919065c527f886d5066f771fae9cd40bde1f7999e
4
- data.tar.gz: deb1a80f49da296b0ea65e5bbdd8736f8568167808974db91dbfbbb4b62f8e5e
3
+ metadata.gz: 8a4cd52d4916355ef186e0e1c115027bbabc111b46c537eee81ee9eed1a4758e
4
+ data.tar.gz: 4915341310b70bd5ad5b5476b9a1a70d86fecb0f6f9b0a0a4581d68353b78a91
5
5
  SHA512:
6
- metadata.gz: 1551a766599640bfef6efc93bb4709c9c0af1d96949787e539b02c130aa744cf4c03d09600668faccc6da656faa324eb5422f5deb71dc5d97f71a5de143cfab6
7
- data.tar.gz: c111b2335a6e5b938d3ca0844f4185d2c1637c9b36856ee49e42bb2d6131521e758ecaf2ffac6e91232f692cb540e21a86579f4c57fbc5d161d4623ee5621238
6
+ metadata.gz: 989b451ae98b489c0df15533ab91ac351b11d864ce6a48b2f2d464cd1654234a14ae9ab4f828e93ae5e5fcf636f0948fd4f155dbf1ceec759afbef6094ddc1fd
7
+ data.tar.gz: 6050d81e977c5b24a5844eaf8087cdb78d846f592ad4cd6ea004653c81a05dec6f7b8f8e5051f99d55e7cfcaa2e9aeb9218389cad396092cca1272b38427ee70
data/CHANGELOG.md CHANGED
@@ -5,6 +5,36 @@ 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.2] - 2022-10-05
9
+ ### Fixed
10
+
11
+ - passing params with existing model
12
+
13
+ ## [14.5.1] - 2022-09-30
14
+ ### Changed
15
+
16
+ - with_http methods were added to the discovery classes
17
+
18
+ ## [14.5.0] - 2022-09-30
19
+ ### Changed
20
+
21
+ - handling ApiError with retries
22
+
23
+ ## [14.4.0] - 2022-09-27
24
+ ### Changed
25
+
26
+ - handling ApiError by passing a block
27
+
28
+ ## [14.3.0] - 2022-08-31
29
+ ### Changed
30
+
31
+ - using hash instead of model object for Hubspot::Client with body param
32
+
33
+ ## [14.2.0] - 2022-08-25
34
+ ### Changed
35
+
36
+ - using new separate configurations for Hubspot::Client
37
+
8
38
  ## [14.1.0] - 2022-08-23
9
39
  ### Changed
10
40
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hubspot-api-client (14.1.0)
4
+ hubspot-api-client (14.5.2)
5
5
  json (~> 2.1, >= 2.1.0)
6
6
  require_all (~> 3.0.0)
7
7
  typhoeus (~> 1.4.0)
@@ -10,8 +10,8 @@ GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
12
  ZenTest (4.12.1)
13
- addressable (2.8.0)
14
- public_suffix (>= 2.0.2, < 5.0)
13
+ addressable (2.8.1)
14
+ public_suffix (>= 2.0.2, < 6.0)
15
15
  autotest (4.4.6)
16
16
  ZenTest (>= 4.4.1)
17
17
  autotest-growl (0.2.16)
@@ -29,7 +29,7 @@ GEM
29
29
  pry (0.14.1)
30
30
  coderay (~> 1.1)
31
31
  method_source (~> 1.0)
32
- public_suffix (4.0.7)
32
+ public_suffix (5.0.0)
33
33
  rake (12.3.3)
34
34
  rake-release (1.3.0)
35
35
  bundler (>= 1.11, < 3)
@@ -41,17 +41,17 @@ 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)
54
- webmock (3.17.1)
54
+ webmock (3.18.1)
55
55
  addressable (>= 2.8.0)
56
56
  crack (>= 0.3.2)
57
57
  hashdiff (>= 0.4.0, < 2.0.0)
data/README.md CHANGED
@@ -116,9 +116,62 @@ object_schema_egg = ::Hubspot::Crm::Schemas::ObjectSchemaEgg.new(
116
116
  api_response = api.create(object_schema_egg)
117
117
  ```
118
118
 
119
+ for new discovery classes since version 14.3
120
+ ```ruby
121
+ client = Hubspot::Client.new(access_token: 'your_oauth2_access_token')
122
+
123
+ labels = {
124
+ singular: 'My object',
125
+ plural: 'My objects'
126
+ }
127
+
128
+ option = {
129
+ label: 'Option A',
130
+ value: 'A',
131
+ description: 'Choice number one',
132
+ display_order: 1,
133
+ hidden: false
134
+ }
135
+
136
+ property = {
137
+ name: 'property001',
138
+ label: 'My object property',
139
+ group_name: 'my_object_information',
140
+ options: [option],
141
+ display_order: 2,
142
+ type: 'enumeration',
143
+ field_type: 'select'
144
+ }
145
+
146
+ body = {
147
+ labels: labels,
148
+ required_properties: ['property001'],
149
+ searchable_properties: [],
150
+ primary_display_property: 'property001',
151
+ secondary_display_properties: [],
152
+ properties: [property],
153
+ associated_objects: ['CONTACT'],
154
+ name: 'my_object'
155
+ }
156
+
157
+ api_response = client.crm.schemas.core_api.create(body: body)
158
+ ```
159
+
119
160
 
120
161
  ### Error handling
121
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
+
122
175
  #### You can set number of retry attempts and delay in seconds before retry on specific status code of response.
123
176
 
124
177
  Available params:
@@ -143,6 +196,28 @@ api_client = ::Hubspot::Crm::Companies::ApiClient.new(config)
143
196
  basic_api = ::Hubspot::Crm::Companies::BasicApi.new(api_client)
144
197
  ```
145
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
+
146
221
  ### Sample apps
147
222
 
148
223
  Please, take a look at our [Sample apps](https://github.com/HubSpot/sample-apps-list)
@@ -2,8 +2,6 @@ require_rel 'discovery'
2
2
 
3
3
  module Hubspot
4
4
  class Client
5
- attr_reader :api_key, :access_token
6
-
7
5
  def self.api_modules
8
6
  %i[
9
7
  automation
@@ -28,25 +26,9 @@ module Hubspot
28
26
  'Hubspot::Discovery'
29
27
  end
30
28
 
31
- def configure_api_key
32
- Hubspot.configure do |config|
33
- config.api_key['hapikey'] = api_key
34
- end
35
- end
36
-
37
- def configure_access_token
38
- Hubspot.configure do |config|
39
- config.access_token = access_token
40
- end
41
- end
42
-
43
29
  def initialize(params)
44
30
  raise 'Please, pass :api_key or :access_token' if params[:api_key].nil? && params[:developer_api_key].nil? && params[:access_token].nil?
45
- @api_key = params[:api_key] || params[:developer_api_key]
46
- @access_token = params[:access_token]
47
31
  @params = params
48
- configure_api_key if @api_key
49
- configure_access_token if @access_token
50
32
  end
51
33
  end
52
34
  end
@@ -1,3 +1,5 @@
1
+ require_rel '../helpers/camel_case'
2
+
1
3
  module Hubspot
2
4
  module Discovery
3
5
  module BaseApiClient
@@ -9,15 +11,22 @@ module Hubspot
9
11
  end
10
12
 
11
13
  def api_methods
12
- api.methods.grep(/with_http_info/).map {|elem| elem.to_s.gsub('_with_http_info', '').to_sym }
14
+ api.methods.grep(/with_http_info/).inject([]) do |methods, method|
15
+ methods << method
16
+ methods << method.to_s.gsub('_with_http_info', '').to_sym
17
+ end
18
+ end
19
+
20
+ def config
21
+ @config ||= new_config
13
22
  end
14
23
 
15
24
  def api_client
16
- api&.api_client
25
+ @api_client ||= Kernel.const_get("#{codegen_module_name}::ApiClient").new(config)
17
26
  end
18
27
 
19
28
  def api
20
- @api ||= Kernel.const_get(codegen_api_class).new
29
+ @api ||= Kernel.const_get(codegen_api_class).new(api_client)
21
30
  end
22
31
 
23
32
  def get_all(params = {})
@@ -26,17 +35,66 @@ module Hubspot
26
35
 
27
36
  private
28
37
 
38
+ def new_config
39
+ config = Kernel.const_get("#{self.class.name.gsub('Discovery::', '').gsub(/(.*)::.*/, '\1')}::Configuration").new
40
+ config.access_token = base_params[:access_token] if base_params[:access_token]
41
+ config.api_key['hapikey'] = base_params[:api_key] if base_params[:api_key]
42
+ config.api_key['hapikey'] = base_params[:developer_api_key] if base_params[:developer_api_key]
43
+ config
44
+ end
45
+
29
46
  def codegen_api_class
30
47
  self.class.name.gsub('Discovery::', '')
31
48
  end
32
49
 
50
+ def codegen_module_name
51
+ codegen_api_class.gsub(/(.*)::.*/, '\1')
52
+ end
53
+
54
+ def call_api(api_method, params_to_pass)
55
+ api.public_send(api_method, *params_to_pass)
56
+ end
57
+
58
+ def call_api_with_rescue(api_method, params_to_pass)
59
+ error = Kernel.const_get("#{codegen_module_name}::ApiError")
60
+ call_api(api_method, params_to_pass)
61
+ rescue error => e
62
+ yield(e)
63
+ end
64
+
65
+ def call_api_with_retry(api_method, params_to_pass, retry_config, &block)
66
+ call_api_with_rescue(api_method, params_to_pass) do |error|
67
+ opts = retry_config.detect{ |k,v| k === error.code }&.last
68
+ retries = opts&.dig(:max_retries) || 5
69
+
70
+ block.call(error) unless block.nil?
71
+
72
+ response = error
73
+
74
+ while retries > 0 && opts
75
+ sleep opts[:seconds_delay] if opts[:seconds_delay]
76
+ response = call_api_with_rescue(api_method, params_to_pass) do |e|
77
+ block.call(e) unless block.nil?
78
+ e
79
+ end
80
+
81
+ return response unless response.respond_to?(:code)
82
+
83
+ opts = retry_config.detect{ |k,v| k === response.code }&.last
84
+ retries -= 1
85
+ end
86
+
87
+ response
88
+ end
89
+ end
90
+
33
91
  def define_methods
34
92
  define_api_methods
35
93
  end
36
94
 
37
95
  def define_api_methods
38
96
  api_methods.each do |api_method|
39
- self.class.define_method(api_method) do |params = {}|
97
+ self.class.define_method(api_method) do |params = {}, &block|
40
98
  params_with_defaults = params
41
99
  params_with_defaults[:opts] ||= {}
42
100
  params_with_defaults[:opts][:auth_names] = if base_params[:access_token]
@@ -53,15 +111,23 @@ module Hubspot
53
111
 
54
112
  signature_param_names = signature_params.map { |_, param| param }
55
113
  params_with_defaults.each do |param_name, param_value|
56
- params_with_defaults[:opts][param_name] = param_value unless signature_param_names.include?(param_name)
114
+ params_with_defaults[:opts][param_name] = param_value if !signature_param_names.include?(param_name) && param_name != :body && param_name != :retry
57
115
  end
58
116
 
59
117
  params_to_pass = signature_params.map do |req, param|
60
- raise "Param #{param} is required for #{api.class}\##{api_method} method" if req == :req && params_with_defaults[param].nil?
61
- params_with_defaults[param]
118
+ if params_with_defaults[param].nil?
119
+ model_name = Hubspot::Helpers::CamelCase.new.format(param.to_s)
120
+ Kernel.const_get("#{codegen_module_name}::#{model_name}").build_from_hash(params_with_defaults[:body])
121
+ else
122
+ params_with_defaults[param]
123
+ end
124
+ rescue NameError
125
+ raise "Param #{param} is required for #{api.class}\##{api_method} method" if req == :req
62
126
  end
63
127
 
64
- api.public_send(api_method, *params_to_pass)
128
+ return call_api_with_retry(api_method, params_to_pass, params[:retry], &block) unless params[:retry].nil?
129
+ return call_api_with_rescue(api_method, params_to_pass, &block) unless block.nil?
130
+ call_api(api_method, params_to_pass)
65
131
  end
66
132
  end
67
133
  end
@@ -1,3 +1,3 @@
1
1
  module Hubspot
2
- VERSION = '14.1.0'
2
+ VERSION = '14.5.2'
3
3
  end
@@ -6,6 +6,9 @@ describe 'Hubspot::Discovery::BaseApiClient' do
6
6
  end
7
7
 
8
8
  class Hubspot::SomeApiClass
9
+ def initialize(api_client)
10
+ end
11
+
9
12
  def get(test_id, opts = {})
10
13
  "got test_id: #{test_id}, opts: #{opts}"
11
14
  end
@@ -14,20 +17,66 @@ describe 'Hubspot::Discovery::BaseApiClient' do
14
17
  end
15
18
 
16
19
  def update(test_id, simple_public_object_input, opts = {})
17
- "updated test_id: #{test_id}, simple_public_object_input: #{simple_public_object_input}, opts: #{opts}"
20
+ "updated test_id: #{test_id}, name: #{simple_public_object_input.name}, email: #{simple_public_object_input.email}, opts: #{opts}"
18
21
  end
19
22
 
20
23
  def update_with_http_info
21
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
42
+ end
43
+
44
+ class Hubspot::ApiClient
45
+ def initialize(config)
46
+ end
47
+ end
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
+
59
+ class Hubspot::SimplePublicObjectInput
60
+ attr_reader :name, :email
61
+
62
+ def initialize(params)
63
+ @name = params[:name]
64
+ @email = params[:email]
65
+ end
66
+
67
+ def self.build_from_hash(params)
68
+ new(params)
69
+ end
22
70
  end
23
71
 
24
72
  subject(:client) { Hubspot::Discovery::SomeApiClass.new(access_token: 'test') }
25
73
  let(:api) { client.api }
74
+ let(:body) { {name: 'test_name', email: 'test_email'} }
26
75
 
27
76
  it { is_expected.to respond_to(:get) }
28
77
  it { is_expected.to respond_to(:update) }
29
- it { is_expected.not_to respond_to(:get_with_http_info) }
30
- it { is_expected.not_to respond_to(:update_with_http_info) }
78
+ it { is_expected.to respond_to(:get_with_http_info) }
79
+ it { is_expected.to respond_to(:update_with_http_info) }
31
80
 
32
81
  describe '#get' do
33
82
  subject(:get) { client.get(params) }
@@ -43,27 +92,75 @@ describe 'Hubspot::Discovery::BaseApiClient' do
43
92
 
44
93
  it { is_expected.to eq('got test_id: test_id_value, opts: {:auth_names=>"oauth2", :limit=>5}') }
45
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
46
102
  end
47
103
 
48
104
  describe '#update' do
49
105
  subject(:update) { client.update(params) }
50
106
 
51
107
  context 'with default params order' do
52
- let(:params) { {test_id: 'test_id_value', simple_public_object_input: 'simple_public_object_input_value', limit: 10} }
108
+ let(:params) { {test_id: 'test_id_value', simple_public_object_input: Hubspot::SimplePublicObjectInput.new(body), limit: 10} }
53
109
 
54
- it { is_expected.to eq('updated test_id: test_id_value, simple_public_object_input: simple_public_object_input_value, opts: {:auth_names=>"oauth2", :limit=>10}') }
110
+ it { is_expected.to eq('updated test_id: test_id_value, name: test_name, email: test_email, opts: {:auth_names=>"oauth2", :limit=>10}') }
55
111
  end
56
112
 
57
113
  context 'with reversed params order' do
58
- let(:params) { {limit: 5, simple_public_object_input: 'simple_public_object_input_value', test_id: 'test_id_value'} }
114
+ let(:params) { {limit: 5, simple_public_object_input: Hubspot::SimplePublicObjectInput.new(body), test_id: 'test_id_value'} }
59
115
 
60
- it { is_expected.to eq('updated test_id: test_id_value, simple_public_object_input: simple_public_object_input_value, opts: {:auth_names=>"oauth2", :limit=>5}') }
116
+ it { is_expected.to eq('updated test_id: test_id_value, name: test_name, email: test_email, opts: {:auth_names=>"oauth2", :limit=>5}') }
61
117
  end
62
118
 
63
119
  context 'with shuffled params order' do
64
- let(:params) { {simple_public_object_input: 'simple_public_object_input_value', limit: 7, test_id: 'test_id_value'} }
120
+ let(:params) { {simple_public_object_input: Hubspot::SimplePublicObjectInput.new(body), limit: 7, test_id: 'test_id_value'} }
65
121
 
66
- it { is_expected.to eq('updated test_id: test_id_value, simple_public_object_input: simple_public_object_input_value, opts: {:auth_names=>"oauth2", :limit=>7}') }
122
+ it { is_expected.to eq('updated test_id: test_id_value, name: test_name, email: test_email, opts: {:auth_names=>"oauth2", :limit=>7}') }
123
+ end
124
+
125
+ context 'with body' do
126
+ let(:params) { {test_id: 'test_id_value', body: body, limit: 10} }
127
+
128
+ it { is_expected.to eq('updated test_id: test_id_value, name: test_name, email: test_email, opts: {:auth_names=>"oauth2", :limit=>10}') }
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') }
67
164
  end
68
165
  end
69
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.1.0
4
+ version: 14.5.2
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-23 00:00:00.000000000 Z
11
+ date: 2022-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus