hubspot-api-client 10.0.1 → 10.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/Gemfile.lock +3 -3
  4. data/lib/hubspot/codegen/communication_preferences/api/definition_api.rb +80 -0
  5. data/lib/hubspot/codegen/communication_preferences/api/status_api.rb +214 -0
  6. data/lib/hubspot/codegen/communication_preferences/api_client.rb +404 -0
  7. data/lib/hubspot/codegen/communication_preferences/api_error.rb +59 -0
  8. data/lib/hubspot/codegen/communication_preferences/configuration.rb +254 -0
  9. data/lib/hubspot/codegen/communication_preferences/models/error.rb +290 -0
  10. data/lib/hubspot/codegen/communication_preferences/models/error_detail.rb +256 -0
  11. data/lib/hubspot/codegen/communication_preferences/models/public_subscription_status.rb +372 -0
  12. data/lib/hubspot/codegen/communication_preferences/models/public_subscription_statuses_response.rb +231 -0
  13. data/lib/hubspot/codegen/communication_preferences/models/public_update_subscription_status_request.rb +283 -0
  14. data/lib/hubspot/codegen/communication_preferences/models/subscription_definition.rb +339 -0
  15. data/lib/hubspot/codegen/communication_preferences/models/subscription_definitions_response.rb +216 -0
  16. data/lib/hubspot/version.rb +1 -1
  17. data/spec/codegen/communication_preferences/api/definition_api_spec.rb +46 -0
  18. data/spec/codegen/communication_preferences/api/status_api_spec.rb +71 -0
  19. data/spec/codegen/communication_preferences/models/error_detail_spec.rb +65 -0
  20. data/spec/codegen/communication_preferences/models/error_spec.rb +77 -0
  21. data/spec/codegen/communication_preferences/models/public_subscription_status_spec.rb +101 -0
  22. data/spec/codegen/communication_preferences/models/public_subscription_statuses_response_spec.rb +47 -0
  23. data/spec/codegen/communication_preferences/models/public_update_subscription_status_request_spec.rb +63 -0
  24. data/spec/codegen/communication_preferences/models/subscription_definition_spec.rb +95 -0
  25. data/spec/codegen/communication_preferences/models/subscription_definitions_response_spec.rb +41 -0
  26. data/spec/codegen/crm/extensions/calling/api/settings_api_spec.rb +2 -2
  27. data/spec/codegen/crm/extensions/calling/models/settings_patch_request_spec.rb +6 -0
  28. data/spec/codegen/crm/extensions/calling/models/settings_request_spec.rb +6 -0
  29. data/spec/codegen/crm/extensions/calling/models/settings_response_spec.rb +6 -0
  30. data/spec/codegen/crm/extensions/cards/api/cards_api_spec.rb +2 -2
  31. data/spec/features/communication_preferences/definition_api_spec.rb +7 -0
  32. data/spec/features/communication_preferences/status_api_spec.rb +9 -0
  33. metadata +36 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7e237972f3e1843e28d56c91e154cce8f756920bd9822b227a9f4818178830e
4
- data.tar.gz: 39d168f6d899f60de7a7cfd679e90c446e18daeb7218c1bdaf71fd0c63738bd6
3
+ metadata.gz: 2787414b7f2e6c1d26e04d8f805bc7168356111769727f4c6a3655c9d7e2867c
4
+ data.tar.gz: 505bd93ff873742b03a0fa10544c0e10248e5519dc5c214416fa18a942379ee9
5
5
  SHA512:
6
- metadata.gz: acf8984f3b41b74b03c977181a872224d738890f8abb4d1920b2b8c82e24ae556b3e3cd16054df865a716caf7f3ad935623183ca4a72d30ecebc7e406ebf7222
7
- data.tar.gz: f47ad82c7bcbfe5ddfcf8d0e88c322219f795c86a9d3f1bd1c065fbba5b87d8745607f589ff8027997d3b1468dd24f7e523afadda59ca37b715e83b9959063e2
6
+ metadata.gz: c80436fba7cf19029f818879b2d224462b5739cd628fb4339c89c9ff4b02512fa2c3e82173d7f5aa560bdb706cc8c9c182d6cc40f8d6c4f01344a0b9b375730b
7
+ data.tar.gz: 0b35c9198e760914a4a209c5b2eedd29985af14f3307aba58db3d20ff0892c2193b2c231d6d79fa0c83321496d10237a4bc91b1357c315ca3b6040f1e687ac26
data/CHANGELOG.md CHANGED
@@ -5,6 +5,10 @@ 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
+ ## [10.1.0] - 2021-06-08
9
+
10
+ - added CommunicationPreferences api module
11
+
8
12
  ## [10.0.0] - 2021-04-15
9
13
 
10
14
  - retry fix
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hubspot-api-client (10.0.1)
4
+ hubspot-api-client (10.1.0)
5
5
  json (~> 2.1, >= 2.1.0)
6
6
  require_all (~> 3.0.0)
7
7
  typhoeus (~> 1.4.0)
@@ -18,9 +18,9 @@ GEM
18
18
  autotest-rails-pure (4.1.2)
19
19
  crack (0.4.4)
20
20
  diff-lcs (1.4.4)
21
- ethon (0.13.0)
21
+ ethon (0.14.0)
22
22
  ffi (>= 1.15.0)
23
- ffi (1.15.0)
23
+ ffi (1.15.1)
24
24
  hashdiff (1.0.1)
25
25
  json (2.5.1)
26
26
  public_suffix (4.0.6)
@@ -0,0 +1,80 @@
1
+ =begin
2
+ #Subscriptions
3
+
4
+ #Subscriptions allow contacts to control what forms of communications they receive. Contacts can decide whether they want to receive communication pertaining to a specific topic, brand, or an entire HubSpot account.
5
+
6
+ The version of the OpenAPI document: v3
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ OpenAPI Generator version: 4.3.1
10
+
11
+ =end
12
+
13
+ require 'cgi'
14
+
15
+ module Hubspot
16
+ module CommunicationPreferences
17
+ class DefinitionApi
18
+ attr_accessor :api_client
19
+
20
+ def initialize(api_client = ApiClient.default)
21
+ @api_client = api_client
22
+ end
23
+ # Get subscription definitions
24
+ # Get a list of all subscription definitions for the portal
25
+ # @param [Hash] opts the optional parameters
26
+ # @return [SubscriptionDefinitionsResponse]
27
+ def get_page(opts = {})
28
+ data, _status_code, _headers = get_page_with_http_info(opts)
29
+ data
30
+ end
31
+
32
+ # Get subscription definitions
33
+ # Get a list of all subscription definitions for the portal
34
+ # @param [Hash] opts the optional parameters
35
+ # @return [Array<(SubscriptionDefinitionsResponse, Integer, Hash)>] SubscriptionDefinitionsResponse data, response status code and response headers
36
+ def get_page_with_http_info(opts = {})
37
+ if @api_client.config.debugging
38
+ @api_client.config.logger.debug 'Calling API: DefinitionApi.get_page ...'
39
+ end
40
+ # resource path
41
+ local_var_path = '/communication-preferences/v3/definitions'
42
+
43
+ # query parameters
44
+ query_params = opts[:query_params] || {}
45
+
46
+ # header parameters
47
+ header_params = opts[:header_params] || {}
48
+ # HTTP header 'Accept' (if needed)
49
+ header_params['Accept'] = @api_client.select_header_accept(['application/json', '*/*'])
50
+
51
+ # form parameters
52
+ form_params = opts[:form_params] || {}
53
+
54
+ # http body (model)
55
+ post_body = opts[:body]
56
+
57
+ # return_type
58
+ return_type = opts[:return_type] || 'SubscriptionDefinitionsResponse'
59
+
60
+ # auth_names
61
+ auth_names = opts[:auth_names] || ['hapikey']
62
+
63
+ new_options = opts.merge(
64
+ :header_params => header_params,
65
+ :query_params => query_params,
66
+ :form_params => form_params,
67
+ :body => post_body,
68
+ :auth_names => auth_names,
69
+ :return_type => return_type
70
+ )
71
+
72
+ data, status_code, headers = @api_client.call_api(:GET, local_var_path, new_options)
73
+ if @api_client.config.debugging
74
+ @api_client.config.logger.debug "API called: DefinitionApi#get_page\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
75
+ end
76
+ return data, status_code, headers
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,214 @@
1
+ =begin
2
+ #Subscriptions
3
+
4
+ #Subscriptions allow contacts to control what forms of communications they receive. Contacts can decide whether they want to receive communication pertaining to a specific topic, brand, or an entire HubSpot account.
5
+
6
+ The version of the OpenAPI document: v3
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ OpenAPI Generator version: 4.3.1
10
+
11
+ =end
12
+
13
+ require 'cgi'
14
+
15
+ module Hubspot
16
+ module CommunicationPreferences
17
+ class StatusApi
18
+ attr_accessor :api_client
19
+
20
+ def initialize(api_client = ApiClient.default)
21
+ @api_client = api_client
22
+ end
23
+ # Get subscription statuses for a contact
24
+ # Returns a list of subscriptions and their status for a given contact.
25
+ # @param email_address [String]
26
+ # @param [Hash] opts the optional parameters
27
+ # @return [PublicSubscriptionStatusesResponse]
28
+ def get_email_status(email_address, opts = {})
29
+ data, _status_code, _headers = get_email_status_with_http_info(email_address, opts)
30
+ data
31
+ end
32
+
33
+ # Get subscription statuses for a contact
34
+ # Returns a list of subscriptions and their status for a given contact.
35
+ # @param email_address [String]
36
+ # @param [Hash] opts the optional parameters
37
+ # @return [Array<(PublicSubscriptionStatusesResponse, Integer, Hash)>] PublicSubscriptionStatusesResponse data, response status code and response headers
38
+ def get_email_status_with_http_info(email_address, opts = {})
39
+ if @api_client.config.debugging
40
+ @api_client.config.logger.debug 'Calling API: StatusApi.get_email_status ...'
41
+ end
42
+ # verify the required parameter 'email_address' is set
43
+ if @api_client.config.client_side_validation && email_address.nil?
44
+ fail ArgumentError, "Missing the required parameter 'email_address' when calling StatusApi.get_email_status"
45
+ end
46
+ # resource path
47
+ local_var_path = '/communication-preferences/v3/status/email/{emailAddress}'.sub('{' + 'emailAddress' + '}', CGI.escape(email_address.to_s))
48
+
49
+ # query parameters
50
+ query_params = opts[:query_params] || {}
51
+
52
+ # header parameters
53
+ header_params = opts[:header_params] || {}
54
+ # HTTP header 'Accept' (if needed)
55
+ header_params['Accept'] = @api_client.select_header_accept(['application/json', '*/*'])
56
+
57
+ # form parameters
58
+ form_params = opts[:form_params] || {}
59
+
60
+ # http body (model)
61
+ post_body = opts[:body]
62
+
63
+ # return_type
64
+ return_type = opts[:return_type] || 'PublicSubscriptionStatusesResponse'
65
+
66
+ # auth_names
67
+ auth_names = opts[:auth_names] || ['hapikey']
68
+
69
+ new_options = opts.merge(
70
+ :header_params => header_params,
71
+ :query_params => query_params,
72
+ :form_params => form_params,
73
+ :body => post_body,
74
+ :auth_names => auth_names,
75
+ :return_type => return_type
76
+ )
77
+
78
+ data, status_code, headers = @api_client.call_api(:GET, local_var_path, new_options)
79
+ if @api_client.config.debugging
80
+ @api_client.config.logger.debug "API called: StatusApi#get_email_status\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
81
+ end
82
+ return data, status_code, headers
83
+ end
84
+
85
+ # Subscribe a contact
86
+ # Subscribes a contact to the given subscription type. This API is not valid to use for subscribing a contact at a brand or portal level and will return an error.
87
+ # @param public_update_subscription_status_request [PublicUpdateSubscriptionStatusRequest]
88
+ # @param [Hash] opts the optional parameters
89
+ # @return [PublicSubscriptionStatus]
90
+ def subscribe(public_update_subscription_status_request, opts = {})
91
+ data, _status_code, _headers = subscribe_with_http_info(public_update_subscription_status_request, opts)
92
+ data
93
+ end
94
+
95
+ # Subscribe a contact
96
+ # Subscribes a contact to the given subscription type. This API is not valid to use for subscribing a contact at a brand or portal level and will return an error.
97
+ # @param public_update_subscription_status_request [PublicUpdateSubscriptionStatusRequest]
98
+ # @param [Hash] opts the optional parameters
99
+ # @return [Array<(PublicSubscriptionStatus, Integer, Hash)>] PublicSubscriptionStatus data, response status code and response headers
100
+ def subscribe_with_http_info(public_update_subscription_status_request, opts = {})
101
+ if @api_client.config.debugging
102
+ @api_client.config.logger.debug 'Calling API: StatusApi.subscribe ...'
103
+ end
104
+ # verify the required parameter 'public_update_subscription_status_request' is set
105
+ if @api_client.config.client_side_validation && public_update_subscription_status_request.nil?
106
+ fail ArgumentError, "Missing the required parameter 'public_update_subscription_status_request' when calling StatusApi.subscribe"
107
+ end
108
+ # resource path
109
+ local_var_path = '/communication-preferences/v3/subscribe'
110
+
111
+ # query parameters
112
+ query_params = opts[:query_params] || {}
113
+
114
+ # header parameters
115
+ header_params = opts[:header_params] || {}
116
+ # HTTP header 'Accept' (if needed)
117
+ header_params['Accept'] = @api_client.select_header_accept(['application/json', '*/*'])
118
+ # HTTP header 'Content-Type'
119
+ header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])
120
+
121
+ # form parameters
122
+ form_params = opts[:form_params] || {}
123
+
124
+ # http body (model)
125
+ post_body = opts[:body] || @api_client.object_to_http_body(public_update_subscription_status_request)
126
+
127
+ # return_type
128
+ return_type = opts[:return_type] || 'PublicSubscriptionStatus'
129
+
130
+ # auth_names
131
+ auth_names = opts[:auth_names] || ['hapikey']
132
+
133
+ new_options = opts.merge(
134
+ :header_params => header_params,
135
+ :query_params => query_params,
136
+ :form_params => form_params,
137
+ :body => post_body,
138
+ :auth_names => auth_names,
139
+ :return_type => return_type
140
+ )
141
+
142
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path, new_options)
143
+ if @api_client.config.debugging
144
+ @api_client.config.logger.debug "API called: StatusApi#subscribe\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
145
+ end
146
+ return data, status_code, headers
147
+ end
148
+
149
+ # Unsubscribe a contact
150
+ # Unsubscribes a contact from the given subscription type. This API is not valid to use for unsubscribing a contact at a brand or portal level and will return an error.
151
+ # @param public_update_subscription_status_request [PublicUpdateSubscriptionStatusRequest]
152
+ # @param [Hash] opts the optional parameters
153
+ # @return [PublicSubscriptionStatus]
154
+ def unsubscribe(public_update_subscription_status_request, opts = {})
155
+ data, _status_code, _headers = unsubscribe_with_http_info(public_update_subscription_status_request, opts)
156
+ data
157
+ end
158
+
159
+ # Unsubscribe a contact
160
+ # Unsubscribes a contact from the given subscription type. This API is not valid to use for unsubscribing a contact at a brand or portal level and will return an error.
161
+ # @param public_update_subscription_status_request [PublicUpdateSubscriptionStatusRequest]
162
+ # @param [Hash] opts the optional parameters
163
+ # @return [Array<(PublicSubscriptionStatus, Integer, Hash)>] PublicSubscriptionStatus data, response status code and response headers
164
+ def unsubscribe_with_http_info(public_update_subscription_status_request, opts = {})
165
+ if @api_client.config.debugging
166
+ @api_client.config.logger.debug 'Calling API: StatusApi.unsubscribe ...'
167
+ end
168
+ # verify the required parameter 'public_update_subscription_status_request' is set
169
+ if @api_client.config.client_side_validation && public_update_subscription_status_request.nil?
170
+ fail ArgumentError, "Missing the required parameter 'public_update_subscription_status_request' when calling StatusApi.unsubscribe"
171
+ end
172
+ # resource path
173
+ local_var_path = '/communication-preferences/v3/unsubscribe'
174
+
175
+ # query parameters
176
+ query_params = opts[:query_params] || {}
177
+
178
+ # header parameters
179
+ header_params = opts[:header_params] || {}
180
+ # HTTP header 'Accept' (if needed)
181
+ header_params['Accept'] = @api_client.select_header_accept(['application/json', '*/*'])
182
+ # HTTP header 'Content-Type'
183
+ header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])
184
+
185
+ # form parameters
186
+ form_params = opts[:form_params] || {}
187
+
188
+ # http body (model)
189
+ post_body = opts[:body] || @api_client.object_to_http_body(public_update_subscription_status_request)
190
+
191
+ # return_type
192
+ return_type = opts[:return_type] || 'PublicSubscriptionStatus'
193
+
194
+ # auth_names
195
+ auth_names = opts[:auth_names] || ['hapikey']
196
+
197
+ new_options = opts.merge(
198
+ :header_params => header_params,
199
+ :query_params => query_params,
200
+ :form_params => form_params,
201
+ :body => post_body,
202
+ :auth_names => auth_names,
203
+ :return_type => return_type
204
+ )
205
+
206
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path, new_options)
207
+ if @api_client.config.debugging
208
+ @api_client.config.logger.debug "API called: StatusApi#unsubscribe\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
209
+ end
210
+ return data, status_code, headers
211
+ end
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,404 @@
1
+ =begin
2
+ #Subscriptions
3
+
4
+ #Subscriptions allow contacts to control what forms of communications they receive. Contacts can decide whether they want to receive communication pertaining to a specific topic, brand, or an entire HubSpot account.
5
+
6
+ The version of the OpenAPI document: v3
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ OpenAPI Generator version: 4.3.1
10
+
11
+ =end
12
+
13
+ require 'date'
14
+ require 'json'
15
+ require 'logger'
16
+ require 'tempfile'
17
+ require 'typhoeus'
18
+
19
+ module Hubspot
20
+ module CommunicationPreferences
21
+ class ApiClient
22
+ # The Configuration object holding settings to be used in the API client.
23
+ attr_accessor :config
24
+
25
+ # Defines the headers to be used in HTTP requests of all API calls by default.
26
+ #
27
+ # @return [Hash]
28
+ attr_accessor :default_headers
29
+
30
+ # Initializes the ApiClient
31
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
32
+ def initialize(config = Configuration.default)
33
+ @config = config
34
+ @user_agent = "hubspot-api-client-ruby; #{VERSION}"
35
+ @default_headers = {
36
+ 'Content-Type' => 'application/json',
37
+ 'User-Agent' => @user_agent
38
+ }
39
+ end
40
+
41
+ def self.default
42
+ @@default ||= ApiClient.new
43
+ end
44
+
45
+ # Call an API with given options.
46
+ #
47
+ # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
48
+ # the data deserialized from response body (could be nil), response status code and response headers.
49
+ def call_api(http_method, path, opts = {})
50
+ request = build_request(http_method, path, opts)
51
+ response = request.run
52
+
53
+ if @config.debugging
54
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
55
+ end
56
+
57
+ if !response.success? && config.error_handler.any?
58
+ config.error_handler.each do |statuses, opts|
59
+ statuses = statuses.is_a?(Integer) ? [statuses] : statuses
60
+
61
+ retries = opts[:max_retries] || 5
62
+ while retries > 0 && statuses.include?(response.code)
63
+ sleep opts[:seconds_delay] if opts[:seconds_delay]
64
+ response = request.run
65
+ opts[:retry_block].call if opts[:retry_block]
66
+ retries -= 1
67
+ end
68
+ end
69
+ end
70
+
71
+ unless response.success?
72
+ if response.timed_out?
73
+ fail ApiError.new('Connection timed out')
74
+ elsif response.code == 0
75
+ # Errors from libcurl will be made visible here
76
+ fail ApiError.new(:code => 0,
77
+ :message => response.return_message)
78
+ else
79
+ fail ApiError.new(:code => response.code,
80
+ :response_headers => response.headers,
81
+ :response_body => response.body),
82
+ response.status_message
83
+ end
84
+ end
85
+
86
+ if opts[:return_type]
87
+ data = deserialize(response, opts[:return_type])
88
+ else
89
+ data = nil
90
+ end
91
+ return data, response.code, response.headers
92
+ end
93
+
94
+ # Builds the HTTP request
95
+ #
96
+ # @param [String] http_method HTTP method/verb (e.g. POST)
97
+ # @param [String] path URL path (e.g. /account/new)
98
+ # @option opts [Hash] :header_params Header parameters
99
+ # @option opts [Hash] :query_params Query parameters
100
+ # @option opts [Hash] :form_params Query parameters
101
+ # @option opts [Object] :body HTTP body (JSON/XML)
102
+ # @return [Typhoeus::Request] A Typhoeus Request
103
+ def build_request(http_method, path, opts = {})
104
+ url = build_request_url(path)
105
+ http_method = http_method.to_sym.downcase
106
+
107
+ header_params = @default_headers.merge(opts[:header_params] || {})
108
+ query_params = opts[:query_params] || {}
109
+ form_params = opts[:form_params] || {}
110
+
111
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
112
+
113
+ # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
114
+ _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
115
+
116
+ req_opts = {
117
+ :method => http_method,
118
+ :headers => header_params,
119
+ :params => query_params,
120
+ :params_encoding => @config.params_encoding,
121
+ :timeout => @config.timeout,
122
+ :ssl_verifypeer => @config.verify_ssl,
123
+ :ssl_verifyhost => _verify_ssl_host,
124
+ :sslcert => @config.cert_file,
125
+ :sslkey => @config.key_file,
126
+ :verbose => @config.debugging
127
+ }
128
+
129
+ # set custom cert, if provided
130
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
131
+
132
+ if [:post, :patch, :put, :delete].include?(http_method)
133
+ req_body = build_request_body(header_params, form_params, opts[:body])
134
+ req_opts.update :body => req_body
135
+ if @config.debugging
136
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
137
+ end
138
+ end
139
+
140
+ request = Typhoeus::Request.new(url, req_opts)
141
+ download_file(request) if opts[:return_type] == 'File'
142
+ request
143
+ end
144
+
145
+ # Builds the HTTP request body
146
+ #
147
+ # @param [Hash] header_params Header parameters
148
+ # @param [Hash] form_params Query parameters
149
+ # @param [Object] body HTTP body (JSON/XML)
150
+ # @return [String] HTTP body data in the form of string
151
+ def build_request_body(header_params, form_params, body)
152
+ # http form
153
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
154
+ header_params['Content-Type'] == 'multipart/form-data'
155
+ data = {}
156
+ form_params.each do |key, value|
157
+ case value
158
+ when ::File, ::Array, nil
159
+ # let typhoeus handle File, Array and nil parameters
160
+ data[key] = value
161
+ else
162
+ data[key] = value.to_s
163
+ end
164
+ end
165
+ elsif body
166
+ data = body.is_a?(String) ? body : body.to_json
167
+ else
168
+ data = nil
169
+ end
170
+ data
171
+ end
172
+
173
+ # Check if the given MIME is a JSON MIME.
174
+ # JSON MIME examples:
175
+ # application/json
176
+ # application/json; charset=UTF8
177
+ # APPLICATION/JSON
178
+ # */*
179
+ # @param [String] mime MIME
180
+ # @return [Boolean] True if the MIME is application/json
181
+ def json_mime?(mime)
182
+ (mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
183
+ end
184
+
185
+ # Deserialize the response to the given return type.
186
+ #
187
+ # @param [Response] response HTTP response
188
+ # @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
189
+ def deserialize(response, return_type)
190
+ body = response.body
191
+
192
+ # handle file downloading - return the File instance processed in request callbacks
193
+ # note that response body is empty when the file is written in chunks in request on_body callback
194
+ return @tempfile if return_type == 'File'
195
+
196
+ return nil if body.nil? || body.empty?
197
+
198
+ # return response body directly for String return type
199
+ return body if return_type == 'String'
200
+
201
+ # ensuring a default content type
202
+ content_type = response.headers['Content-Type'] || 'application/json'
203
+
204
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
205
+
206
+ begin
207
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
208
+ rescue JSON::ParserError => e
209
+ if %w(String Date DateTime).include?(return_type)
210
+ data = body
211
+ else
212
+ raise e
213
+ end
214
+ end
215
+
216
+ convert_to_type data, return_type
217
+ end
218
+
219
+ # Convert data to the given return type.
220
+ # @param [Object] data Data to be converted
221
+ # @param [String] return_type Return type
222
+ # @return [Mixed] Data in a particular type
223
+ def convert_to_type(data, return_type)
224
+ return nil if data.nil?
225
+ case return_type
226
+ when 'String'
227
+ data.to_s
228
+ when 'Integer'
229
+ data.to_i
230
+ when 'Float'
231
+ data.to_f
232
+ when 'Boolean'
233
+ data == true
234
+ when 'DateTime'
235
+ # parse date time (expecting ISO 8601 format)
236
+ DateTime.parse data
237
+ when 'Date'
238
+ # parse date time (expecting ISO 8601 format)
239
+ Date.parse data
240
+ when 'Object'
241
+ # generic object (usually a Hash), return directly
242
+ data
243
+ when /\AArray<(.+)>\z/
244
+ # e.g. Array<Pet>
245
+ sub_type = $1
246
+ data.map { |item| convert_to_type(item, sub_type) }
247
+ when /\AHash\<String, (.+)\>\z/
248
+ # e.g. Hash<String, Integer>
249
+ sub_type = $1
250
+ {}.tap do |hash|
251
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
252
+ end
253
+ else
254
+ # models, e.g. Pet
255
+ Hubspot::CommunicationPreferences.const_get(return_type).build_from_hash(data)
256
+ end
257
+ end
258
+
259
+ # Save response body into a file in (the defined) temporary folder, using the filename
260
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
261
+ # The response body is written to the file in chunks in order to handle files which
262
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
263
+ # process can use.
264
+ #
265
+ # @see Configuration#temp_folder_path
266
+ def download_file(request)
267
+ tempfile = nil
268
+ encoding = nil
269
+ request.on_headers do |response|
270
+ content_disposition = response.headers['Content-Disposition']
271
+ if content_disposition && content_disposition =~ /filename=/i
272
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
273
+ prefix = sanitize_filename(filename)
274
+ else
275
+ prefix = 'download-'
276
+ end
277
+ prefix = prefix + '-' unless prefix.end_with?('-')
278
+ encoding = response.body.encoding
279
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
280
+ @tempfile = tempfile
281
+ end
282
+ request.on_body do |chunk|
283
+ chunk.force_encoding(encoding)
284
+ tempfile.write(chunk)
285
+ end
286
+ request.on_complete do |response|
287
+ if tempfile
288
+ tempfile.close
289
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
290
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
291
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
292
+ "explicitly with `tempfile.delete`"
293
+ end
294
+ end
295
+ end
296
+
297
+ # Sanitize filename by removing path.
298
+ # e.g. ../../sun.gif becomes sun.gif
299
+ #
300
+ # @param [String] filename the filename to be sanitized
301
+ # @return [String] the sanitized filename
302
+ def sanitize_filename(filename)
303
+ filename.gsub(/.*[\/\\]/, '')
304
+ end
305
+
306
+ def build_request_url(path)
307
+ # Add leading and trailing slashes to path
308
+ path = "/#{path}".gsub(/\/+/, '/')
309
+ @config.base_url + path
310
+ end
311
+
312
+ # Update hearder and query params based on authentication settings.
313
+ #
314
+ # @param [Hash] header_params Header parameters
315
+ # @param [Hash] query_params Query parameters
316
+ # @param [String] auth_names Authentication scheme name
317
+ def update_params_for_auth!(header_params, query_params, auth_names)
318
+ Array(auth_names).each do |auth_name|
319
+ auth_setting = @config.auth_settings[auth_name]
320
+ next unless auth_setting
321
+ case auth_setting[:in]
322
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
323
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
324
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
325
+ end
326
+ end
327
+ end
328
+
329
+ # Sets user agent in HTTP header
330
+ #
331
+ # @param [String] user_agent User agent (e.g. openapi-generator/ruby/1.0.0)
332
+ def user_agent=(user_agent)
333
+ @user_agent = user_agent
334
+ @default_headers['User-Agent'] = @user_agent
335
+ end
336
+
337
+ # Return Accept header based on an array of accepts provided.
338
+ # @param [Array] accepts array for Accept
339
+ # @return [String] the Accept header (e.g. application/json)
340
+ def select_header_accept(accepts)
341
+ return nil if accepts.nil? || accepts.empty?
342
+ # use JSON when present, otherwise use all of the provided
343
+ json_accept = accepts.find { |s| json_mime?(s) }
344
+ json_accept || accepts.join(',')
345
+ end
346
+
347
+ # Return Content-Type header based on an array of content types provided.
348
+ # @param [Array] content_types array for Content-Type
349
+ # @return [String] the Content-Type header (e.g. application/json)
350
+ def select_header_content_type(content_types)
351
+ # use application/json by default
352
+ return 'application/json' if content_types.nil? || content_types.empty?
353
+ # use JSON when present, otherwise use the first one
354
+ json_content_type = content_types.find { |s| json_mime?(s) }
355
+ json_content_type || content_types.first
356
+ end
357
+
358
+ # Convert object (array, hash, object, etc) to JSON string.
359
+ # @param [Object] model object to be converted into JSON string
360
+ # @return [String] JSON string representation of the object
361
+ def object_to_http_body(model)
362
+ return model if model.nil? || model.is_a?(String)
363
+ local_body = nil
364
+ if model.is_a?(Array)
365
+ local_body = model.map { |m| object_to_hash(m) }
366
+ else
367
+ local_body = object_to_hash(model)
368
+ end
369
+ local_body.to_json
370
+ end
371
+
372
+ # Convert object(non-array) to hash.
373
+ # @param [Object] obj object to be converted into JSON string
374
+ # @return [String] JSON string representation of the object
375
+ def object_to_hash(obj)
376
+ if obj.respond_to?(:to_hash)
377
+ obj.to_hash
378
+ else
379
+ obj
380
+ end
381
+ end
382
+
383
+ # Build parameter value according to the given collection format.
384
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
385
+ def build_collection_param(param, collection_format)
386
+ case collection_format
387
+ when :csv
388
+ param.join(',')
389
+ when :ssv
390
+ param.join(' ')
391
+ when :tsv
392
+ param.join("\t")
393
+ when :pipes
394
+ param.join('|')
395
+ when :multi
396
+ # return the array directly as typhoeus will handle it as expected
397
+ param
398
+ else
399
+ fail "unknown collection format: #{collection_format.inspect}"
400
+ end
401
+ end
402
+ end
403
+ end
404
+ end