square.rb 13.0.0.20210721 → 16.0.0.20211117
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +38 -21
- data/lib/square/api/apple_pay_api.rb +1 -1
- data/lib/square/api/base_api.rb +5 -9
- data/lib/square/api/bookings_api.rb +64 -4
- data/lib/square/api/cards_api.rb +1 -1
- data/lib/square/api/catalog_api.rb +10 -10
- data/lib/square/api/checkout_api.rb +1 -1
- data/lib/square/api/customer_groups_api.rb +12 -4
- data/lib/square/api/customer_segments_api.rb +10 -2
- data/lib/square/api/customers_api.rb +15 -7
- data/lib/square/api/devices_api.rb +1 -1
- data/lib/square/api/disputes_api.rb +1 -1
- data/lib/square/api/gift_card_activities_api.rb +26 -20
- data/lib/square/api/gift_cards_api.rb +32 -26
- data/lib/square/api/inventory_api.rb +6 -6
- data/lib/square/api/invoices_api.rb +5 -5
- data/lib/square/api/labor_api.rb +70 -68
- data/lib/square/api/locations_api.rb +2 -2
- data/lib/square/api/loyalty_api.rb +9 -9
- data/lib/square/api/mobile_authorization_api.rb +1 -1
- data/lib/square/api/o_auth_api.rb +22 -15
- data/lib/square/api/orders_api.rb +43 -6
- data/lib/square/api/payments_api.rb +12 -6
- data/lib/square/api/refunds_api.rb +1 -1
- data/lib/square/api/sites_api.rb +2 -1
- data/lib/square/api/snippets_api.rb +1 -1
- data/lib/square/api/subscriptions_api.rb +159 -16
- data/lib/square/api/team_api.rb +6 -6
- data/lib/square/api/terminal_api.rb +4 -4
- data/lib/square/api/transactions_api.rb +0 -188
- data/lib/square/api/v1_transactions_api.rb +2 -2
- data/lib/square/api_helper.rb +19 -17
- data/lib/square/client.rb +6 -11
- data/lib/square/configuration.rb +19 -20
- data/lib/square/http/api_response.rb +1 -1
- data/lib/square/http/faraday_client.rb +21 -2
- data/lib/square/utilities/date_time_helper.rb +2 -2
- data/lib/square/utilities/file_wrapper.rb +1 -2
- data/lib/square.rb +48 -49
- metadata +11 -9
- data/lib/square/api/v1_employees_api.rb +0 -362
@@ -21,7 +21,7 @@ module Square
|
|
21
21
|
# Prepare headers.
|
22
22
|
_headers = {
|
23
23
|
'accept' => 'application/json',
|
24
|
-
'
|
24
|
+
'Content-Type' => 'application/json'
|
25
25
|
}
|
26
26
|
|
27
27
|
# Prepare and execute HttpRequest.
|
@@ -56,7 +56,7 @@ module Square
|
|
56
56
|
# Prepare headers.
|
57
57
|
_headers = {
|
58
58
|
'accept' => 'application/json',
|
59
|
-
'
|
59
|
+
'Content-Type' => 'application/json'
|
60
60
|
}
|
61
61
|
|
62
62
|
# Prepare and execute HttpRequest.
|
@@ -162,7 +162,7 @@ module Square
|
|
162
162
|
# Prepare headers.
|
163
163
|
_headers = {
|
164
164
|
'accept' => 'application/json',
|
165
|
-
'
|
165
|
+
'Content-Type' => 'application/json'
|
166
166
|
}
|
167
167
|
|
168
168
|
# Prepare and execute HttpRequest.
|
@@ -197,7 +197,7 @@ module Square
|
|
197
197
|
# Prepare headers.
|
198
198
|
_headers = {
|
199
199
|
'accept' => 'application/json',
|
200
|
-
'
|
200
|
+
'Content-Type' => 'application/json'
|
201
201
|
}
|
202
202
|
|
203
203
|
# Prepare and execute HttpRequest.
|
@@ -5,80 +5,6 @@ module Square
|
|
5
5
|
super(config, http_call_back: http_call_back)
|
6
6
|
end
|
7
7
|
|
8
|
-
# Lists refunds for one of a business's locations.
|
9
|
-
# In addition to full or partial tender refunds processed through Square
|
10
|
-
# APIs,
|
11
|
-
# refunds may result from itemized returns or exchanges through Square's
|
12
|
-
# Point of Sale applications.
|
13
|
-
# Refunds with a `status` of `PENDING` are not currently included in this
|
14
|
-
# endpoint's response.
|
15
|
-
# Max results per
|
16
|
-
# [page](https://developer.squareup.com/docs/working-with-apis/pagination):
|
17
|
-
# 50
|
18
|
-
# @param [String] location_id Required parameter: The ID of the location to
|
19
|
-
# list refunds for.
|
20
|
-
# @param [String] begin_time Optional parameter: The beginning of the
|
21
|
-
# requested reporting period, in RFC 3339 format. See [Date
|
22
|
-
# ranges](https://developer.squareup.com/docs/build-basics/working-with-date
|
23
|
-
# s) for details on date inclusivity/exclusivity. Default value: The
|
24
|
-
# current time minus one year.
|
25
|
-
# @param [String] end_time Optional parameter: The end of the requested
|
26
|
-
# reporting period, in RFC 3339 format. See [Date
|
27
|
-
# ranges](https://developer.squareup.com/docs/build-basics/working-with-date
|
28
|
-
# s) for details on date inclusivity/exclusivity. Default value: The
|
29
|
-
# current time.
|
30
|
-
# @param [SortOrder] sort_order Optional parameter: The order in which
|
31
|
-
# results are listed in the response (`ASC` for oldest first, `DESC` for
|
32
|
-
# newest first). Default value: `DESC`
|
33
|
-
# @param [String] cursor Optional parameter: A pagination cursor returned by
|
34
|
-
# a previous call to this endpoint. Provide this to retrieve the next set of
|
35
|
-
# results for your original query. See [Paginating
|
36
|
-
# results](https://developer.squareup.com/docs/working-with-apis/pagination)
|
37
|
-
# for more information.
|
38
|
-
# @return [ListRefundsResponse Hash] response from the API call
|
39
|
-
def list_refunds(location_id:,
|
40
|
-
begin_time: nil,
|
41
|
-
end_time: nil,
|
42
|
-
sort_order: nil,
|
43
|
-
cursor: nil)
|
44
|
-
warn 'Endpoint list_refunds in TransactionsApi is deprecated'
|
45
|
-
# Prepare query url.
|
46
|
-
_query_builder = config.get_base_uri
|
47
|
-
_query_builder << '/v2/locations/{location_id}/refunds'
|
48
|
-
_query_builder = APIHelper.append_url_with_template_parameters(
|
49
|
-
_query_builder,
|
50
|
-
'location_id' => { 'value' => location_id, 'encode' => true }
|
51
|
-
)
|
52
|
-
_query_builder = APIHelper.append_url_with_query_parameters(
|
53
|
-
_query_builder,
|
54
|
-
'begin_time' => begin_time,
|
55
|
-
'end_time' => end_time,
|
56
|
-
'sort_order' => sort_order,
|
57
|
-
'cursor' => cursor
|
58
|
-
)
|
59
|
-
_query_url = APIHelper.clean_url _query_builder
|
60
|
-
|
61
|
-
# Prepare headers.
|
62
|
-
_headers = {
|
63
|
-
'accept' => 'application/json'
|
64
|
-
}
|
65
|
-
|
66
|
-
# Prepare and execute HttpRequest.
|
67
|
-
_request = config.http_client.get(
|
68
|
-
_query_url,
|
69
|
-
headers: _headers
|
70
|
-
)
|
71
|
-
OAuth2.apply(config, _request)
|
72
|
-
_response = execute_request(_request)
|
73
|
-
|
74
|
-
# Return appropriate response type.
|
75
|
-
decoded = APIHelper.json_deserialize(_response.raw_body)
|
76
|
-
_errors = APIHelper.map_response(decoded, ['errors'])
|
77
|
-
ApiResponse.new(
|
78
|
-
_response, data: decoded, errors: _errors
|
79
|
-
)
|
80
|
-
end
|
81
|
-
|
82
8
|
# Lists transactions for a particular location.
|
83
9
|
# Transactions include payment information from sales and exchanges and
|
84
10
|
# refund
|
@@ -150,67 +76,6 @@ module Square
|
|
150
76
|
)
|
151
77
|
end
|
152
78
|
|
153
|
-
# Charges a card represented by a card nonce or a customer's card on file.
|
154
|
-
# Your request to this endpoint must include _either_:
|
155
|
-
# - A value for the `card_nonce` parameter (to charge a card payment token
|
156
|
-
# generated
|
157
|
-
# with the Web Payments SDK)
|
158
|
-
# - Values for the `customer_card_id` and `customer_id` parameters (to
|
159
|
-
# charge
|
160
|
-
# a customer's card on file)
|
161
|
-
# In order for an eCommerce payment to potentially qualify for
|
162
|
-
# [Square chargeback protection](https://squareup.com/help/article/5394),
|
163
|
-
# you
|
164
|
-
# _must_ provide values for the following parameters in your request:
|
165
|
-
# - `buyer_email_address`
|
166
|
-
# - At least one of `billing_address` or `shipping_address`
|
167
|
-
# When this response is returned, the amount of Square's processing fee
|
168
|
-
# might not yet be
|
169
|
-
# calculated. To obtain the processing fee, wait about ten seconds and call
|
170
|
-
# [RetrieveTransaction]($e/Transactions/RetrieveTransaction). See the
|
171
|
-
# `processing_fee_money`
|
172
|
-
# field of each [Tender included]($m/Tender) in the transaction.
|
173
|
-
# @param [String] location_id Required parameter: The ID of the location to
|
174
|
-
# associate the created transaction with.
|
175
|
-
# @param [ChargeRequest] body Required parameter: An object containing the
|
176
|
-
# fields to POST for the request. See the corresponding object definition
|
177
|
-
# for field details.
|
178
|
-
# @return [ChargeResponse Hash] response from the API call
|
179
|
-
def charge(location_id:,
|
180
|
-
body:)
|
181
|
-
warn 'Endpoint charge in TransactionsApi is deprecated'
|
182
|
-
# Prepare query url.
|
183
|
-
_query_builder = config.get_base_uri
|
184
|
-
_query_builder << '/v2/locations/{location_id}/transactions'
|
185
|
-
_query_builder = APIHelper.append_url_with_template_parameters(
|
186
|
-
_query_builder,
|
187
|
-
'location_id' => { 'value' => location_id, 'encode' => true }
|
188
|
-
)
|
189
|
-
_query_url = APIHelper.clean_url _query_builder
|
190
|
-
|
191
|
-
# Prepare headers.
|
192
|
-
_headers = {
|
193
|
-
'accept' => 'application/json',
|
194
|
-
'content-type' => 'application/json; charset=utf-8'
|
195
|
-
}
|
196
|
-
|
197
|
-
# Prepare and execute HttpRequest.
|
198
|
-
_request = config.http_client.post(
|
199
|
-
_query_url,
|
200
|
-
headers: _headers,
|
201
|
-
parameters: body.to_json
|
202
|
-
)
|
203
|
-
OAuth2.apply(config, _request)
|
204
|
-
_response = execute_request(_request)
|
205
|
-
|
206
|
-
# Return appropriate response type.
|
207
|
-
decoded = APIHelper.json_deserialize(_response.raw_body)
|
208
|
-
_errors = APIHelper.map_response(decoded, ['errors'])
|
209
|
-
ApiResponse.new(
|
210
|
-
_response, data: decoded, errors: _errors
|
211
|
-
)
|
212
|
-
end
|
213
|
-
|
214
79
|
# Retrieves details for a single transaction.
|
215
80
|
# @param [String] location_id Required parameter: The ID of the
|
216
81
|
# transaction's associated location.
|
@@ -295,59 +160,6 @@ module Square
|
|
295
160
|
)
|
296
161
|
end
|
297
162
|
|
298
|
-
# Initiates a refund for a previously charged tender.
|
299
|
-
# You must issue a refund within 120 days of the associated payment. See
|
300
|
-
# [this article](https://squareup.com/help/us/en/article/5060) for more
|
301
|
-
# information
|
302
|
-
# on refund behavior.
|
303
|
-
# NOTE: Card-present transactions with Interac credit cards **cannot be
|
304
|
-
# refunded using the Connect API**. Interac transactions must refunded
|
305
|
-
# in-person (e.g., dipping the card using POS app).
|
306
|
-
# @param [String] location_id Required parameter: The ID of the original
|
307
|
-
# transaction's associated location.
|
308
|
-
# @param [String] transaction_id Required parameter: The ID of the original
|
309
|
-
# transaction that includes the tender to refund.
|
310
|
-
# @param [CreateRefundRequest] body Required parameter: An object containing
|
311
|
-
# the fields to POST for the request. See the corresponding object
|
312
|
-
# definition for field details.
|
313
|
-
# @return [CreateRefundResponse Hash] response from the API call
|
314
|
-
def create_refund(location_id:,
|
315
|
-
transaction_id:,
|
316
|
-
body:)
|
317
|
-
warn 'Endpoint create_refund in TransactionsApi is deprecated'
|
318
|
-
# Prepare query url.
|
319
|
-
_query_builder = config.get_base_uri
|
320
|
-
_query_builder << '/v2/locations/{location_id}/transactions/{transaction_id}/refund'
|
321
|
-
_query_builder = APIHelper.append_url_with_template_parameters(
|
322
|
-
_query_builder,
|
323
|
-
'location_id' => { 'value' => location_id, 'encode' => true },
|
324
|
-
'transaction_id' => { 'value' => transaction_id, 'encode' => true }
|
325
|
-
)
|
326
|
-
_query_url = APIHelper.clean_url _query_builder
|
327
|
-
|
328
|
-
# Prepare headers.
|
329
|
-
_headers = {
|
330
|
-
'accept' => 'application/json',
|
331
|
-
'content-type' => 'application/json; charset=utf-8'
|
332
|
-
}
|
333
|
-
|
334
|
-
# Prepare and execute HttpRequest.
|
335
|
-
_request = config.http_client.post(
|
336
|
-
_query_url,
|
337
|
-
headers: _headers,
|
338
|
-
parameters: body.to_json
|
339
|
-
)
|
340
|
-
OAuth2.apply(config, _request)
|
341
|
-
_response = execute_request(_request)
|
342
|
-
|
343
|
-
# Return appropriate response type.
|
344
|
-
decoded = APIHelper.json_deserialize(_response.raw_body)
|
345
|
-
_errors = APIHelper.map_response(decoded, ['errors'])
|
346
|
-
ApiResponse.new(
|
347
|
-
_response, data: decoded, errors: _errors
|
348
|
-
)
|
349
|
-
end
|
350
|
-
|
351
163
|
# Cancels a transaction that was created with the
|
352
164
|
# [Charge]($e/Transactions/Charge)
|
353
165
|
# endpoint with a `delay_capture` value of `true`.
|
@@ -127,7 +127,7 @@ module Square
|
|
127
127
|
# Prepare headers.
|
128
128
|
_headers = {
|
129
129
|
'accept' => 'application/json',
|
130
|
-
'
|
130
|
+
'Content-Type' => 'application/json'
|
131
131
|
}
|
132
132
|
|
133
133
|
# Prepare and execute HttpRequest.
|
@@ -369,7 +369,7 @@ module Square
|
|
369
369
|
# Prepare headers.
|
370
370
|
_headers = {
|
371
371
|
'accept' => 'application/json',
|
372
|
-
'
|
372
|
+
'Content-Type' => 'application/json'
|
373
373
|
}
|
374
374
|
|
375
375
|
# Prepare and execute HttpRequest.
|
data/lib/square/api_helper.rb
CHANGED
@@ -8,13 +8,14 @@ module Square
|
|
8
8
|
def self.serialize_array(key, array, formatting: 'indexed')
|
9
9
|
tuples = []
|
10
10
|
|
11
|
-
|
11
|
+
case formatting
|
12
|
+
when 'unindexed'
|
12
13
|
tuples += array.map { |element| ["#{key}[]", element] }
|
13
|
-
|
14
|
+
when 'indexed'
|
14
15
|
tuples += array.map.with_index do |element, index|
|
15
16
|
["#{key}[#{index}]", element]
|
16
17
|
end
|
17
|
-
|
18
|
+
when 'plain'
|
18
19
|
tuples += array.map { |element| [key, element] }
|
19
20
|
else
|
20
21
|
raise ArgumentError, 'Invalid format provided.'
|
@@ -55,7 +56,7 @@ module Square
|
|
55
56
|
end
|
56
57
|
|
57
58
|
# Find the template parameter and replace it with its value.
|
58
|
-
query_builder = query_builder.gsub(
|
59
|
+
query_builder = query_builder.gsub("{#{key}}", replace_value)
|
59
60
|
end
|
60
61
|
query_builder
|
61
62
|
end
|
@@ -80,15 +81,16 @@ module Square
|
|
80
81
|
unless value.nil?
|
81
82
|
if value.instance_of? Array
|
82
83
|
value.compact!
|
83
|
-
query_builder +=
|
84
|
+
query_builder += case array_serialization
|
85
|
+
when 'csv'
|
84
86
|
"#{seperator}#{key}=#{value.map do |element|
|
85
87
|
CGI.escape(element.to_s)
|
86
88
|
end.join(',')}"
|
87
|
-
|
89
|
+
when 'psv'
|
88
90
|
"#{seperator}#{key}=#{value.map do |element|
|
89
91
|
CGI.escape(element.to_s)
|
90
92
|
end.join('|')}"
|
91
|
-
|
93
|
+
when 'tsv'
|
92
94
|
"#{seperator}#{key}=#{value.map do |element|
|
93
95
|
CGI.escape(element.to_s)
|
94
96
|
end.join("\t")}"
|
@@ -114,7 +116,7 @@ module Square
|
|
114
116
|
raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
|
115
117
|
|
116
118
|
# Ensure that the urls are absolute.
|
117
|
-
matches = url.match(%r{^(https
|
119
|
+
matches = url.match(%r{^(https?://[^/]+)})
|
118
120
|
raise ArgumentError, 'Invalid Url format.' if matches.nil?
|
119
121
|
|
120
122
|
# Get the http protocol match.
|
@@ -125,7 +127,7 @@ module Square
|
|
125
127
|
|
126
128
|
# Remove redundant forward slashes.
|
127
129
|
query = url[protocol.length...(!index.nil? ? index : url.length)]
|
128
|
-
query.gsub!(%r{
|
130
|
+
query.gsub!(%r{//+}, '/')
|
129
131
|
|
130
132
|
# Get the parameters.
|
131
133
|
parameters = !index.nil? ? url[url.index('?')...url.length] : ''
|
@@ -137,7 +139,7 @@ module Square
|
|
137
139
|
# Parses JSON string.
|
138
140
|
# @param [String] A JSON string.
|
139
141
|
def self.json_deserialize(json)
|
140
|
-
|
142
|
+
JSON.parse(json, symbolize_names: true)
|
141
143
|
rescue StandardError
|
142
144
|
raise TypeError, 'Server responded with invalid JSON.'
|
143
145
|
end
|
@@ -166,6 +168,7 @@ module Square
|
|
166
168
|
a.each do |key, value_a|
|
167
169
|
b.each do |k, value_b|
|
168
170
|
next unless key == k
|
171
|
+
|
169
172
|
x[k] = []
|
170
173
|
if value_a.instance_of? Array
|
171
174
|
value_a.each do |v|
|
@@ -207,13 +210,12 @@ module Square
|
|
207
210
|
elsif obj.instance_of? Array
|
208
211
|
if formatting == 'indexed'
|
209
212
|
obj.each_with_index do |value, index|
|
210
|
-
retval.merge!(APIHelper.form_encode(value, instance_name
|
211
|
-
index.to_s + ']'))
|
213
|
+
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]"))
|
212
214
|
end
|
213
215
|
elsif serializable_types.map { |x| obj[0].is_a? x }.any?
|
214
216
|
obj.each do |value|
|
215
217
|
abc = if formatting == 'unindexed'
|
216
|
-
APIHelper.form_encode(value, instance_name
|
218
|
+
APIHelper.form_encode(value, "#{instance_name}[]",
|
217
219
|
formatting: formatting)
|
218
220
|
else
|
219
221
|
APIHelper.form_encode(value, instance_name,
|
@@ -223,14 +225,14 @@ module Square
|
|
223
225
|
end
|
224
226
|
else
|
225
227
|
obj.each_with_index do |value, index|
|
226
|
-
retval.merge!(APIHelper.form_encode(value, instance_name
|
227
|
-
|
228
|
+
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]",
|
229
|
+
formatting: formatting))
|
228
230
|
end
|
229
231
|
end
|
230
232
|
elsif obj.instance_of? Hash
|
231
233
|
obj.each do |key, value|
|
232
|
-
retval.merge!(APIHelper.form_encode(value, instance_name
|
233
|
-
|
234
|
+
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{key}]",
|
235
|
+
formatting: formatting))
|
234
236
|
end
|
235
237
|
elsif obj.instance_of? File
|
236
238
|
retval[instance_name] = UploadIO.new(
|
data/lib/square/client.rb
CHANGED
@@ -4,7 +4,7 @@ module Square
|
|
4
4
|
attr_reader :config
|
5
5
|
|
6
6
|
def sdk_version
|
7
|
-
'
|
7
|
+
'16.0.0.20211117'
|
8
8
|
end
|
9
9
|
|
10
10
|
def square_version
|
@@ -23,12 +23,6 @@ module Square
|
|
23
23
|
@o_auth ||= OAuthApi.new config
|
24
24
|
end
|
25
25
|
|
26
|
-
# Access to v1_employees controller.
|
27
|
-
# @return [V1EmployeesApi] Returns the controller instance.
|
28
|
-
def v1_employees
|
29
|
-
@v1_employees ||= V1EmployeesApi.new config
|
30
|
-
end
|
31
|
-
|
32
26
|
# Access to v1_transactions controller.
|
33
27
|
# @return [V1TransactionsApi] Returns the controller instance.
|
34
28
|
def v1_transactions
|
@@ -215,15 +209,16 @@ module Square
|
|
215
209
|
@terminal ||= TerminalApi.new config
|
216
210
|
end
|
217
211
|
|
218
|
-
def initialize(timeout: 60, max_retries: 0,
|
219
|
-
backoff_factor: 2,
|
212
|
+
def initialize(http_client_instance: nil, timeout: 60, max_retries: 0,
|
213
|
+
retry_interval: 1, backoff_factor: 2,
|
220
214
|
retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
|
221
215
|
retry_methods: %i[get put], environment: 'production',
|
222
216
|
custom_url: 'https://connect.squareup.com',
|
223
|
-
square_version: '2021-
|
217
|
+
square_version: '2021-11-17', access_token: '',
|
224
218
|
additional_headers: {}, config: nil)
|
225
219
|
@config = if config.nil?
|
226
|
-
Configuration.new(
|
220
|
+
Configuration.new(http_client_instance: http_client_instance,
|
221
|
+
timeout: timeout, max_retries: max_retries,
|
227
222
|
retry_interval: retry_interval,
|
228
223
|
backoff_factor: backoff_factor,
|
229
224
|
retry_statuses: retry_statuses,
|
data/lib/square/configuration.rb
CHANGED
@@ -3,17 +3,9 @@ module Square
|
|
3
3
|
# are configured in this class.
|
4
4
|
class Configuration
|
5
5
|
# The attribute readers for properties.
|
6
|
-
attr_reader :http_client
|
7
|
-
|
8
|
-
|
9
|
-
attr_reader :retry_interval
|
10
|
-
attr_reader :backoff_factor
|
11
|
-
attr_reader :retry_statuses
|
12
|
-
attr_reader :retry_methods
|
13
|
-
attr_reader :environment
|
14
|
-
attr_reader :custom_url
|
15
|
-
attr_reader :square_version
|
16
|
-
attr_reader :access_token
|
6
|
+
attr_reader :http_client, :http_client_instance, :timeout, :max_retries, :retry_interval,
|
7
|
+
:backoff_factor, :retry_statuses, :retry_methods, :environment, :custom_url,
|
8
|
+
:square_version, :access_token
|
17
9
|
|
18
10
|
def additional_headers
|
19
11
|
@additional_headers.clone
|
@@ -23,13 +15,16 @@ module Square
|
|
23
15
|
attr_reader :environments
|
24
16
|
end
|
25
17
|
|
26
|
-
def initialize(timeout: 60, max_retries: 0,
|
27
|
-
backoff_factor: 2,
|
18
|
+
def initialize(http_client_instance: nil, timeout: 60, max_retries: 0,
|
19
|
+
retry_interval: 1, backoff_factor: 2,
|
28
20
|
retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
|
29
21
|
retry_methods: %i[get put], environment: 'production',
|
30
22
|
custom_url: 'https://connect.squareup.com',
|
31
|
-
square_version: '2021-
|
23
|
+
square_version: '2021-11-17', access_token: '',
|
32
24
|
additional_headers: {})
|
25
|
+
# The Http Client passed from the sdk user for making requests
|
26
|
+
@http_client_instance = http_client_instance
|
27
|
+
|
33
28
|
# The value to use for connection timeout
|
34
29
|
@timeout = timeout
|
35
30
|
|
@@ -68,10 +63,12 @@ module Square
|
|
68
63
|
@http_client = create_http_client
|
69
64
|
end
|
70
65
|
|
71
|
-
def clone_with(
|
72
|
-
|
73
|
-
|
74
|
-
|
66
|
+
def clone_with(http_client_instance: nil, timeout: nil, max_retries: nil,
|
67
|
+
retry_interval: nil, backoff_factor: nil,
|
68
|
+
retry_statuses: nil, retry_methods: nil, environment: nil,
|
69
|
+
custom_url: nil, square_version: nil, access_token: nil,
|
70
|
+
additional_headers: nil)
|
71
|
+
http_client_instance ||= self.http_client_instance
|
75
72
|
timeout ||= self.timeout
|
76
73
|
max_retries ||= self.max_retries
|
77
74
|
retry_interval ||= self.retry_interval
|
@@ -84,7 +81,8 @@ module Square
|
|
84
81
|
access_token ||= self.access_token
|
85
82
|
additional_headers ||= self.additional_headers
|
86
83
|
|
87
|
-
Configuration.new(
|
84
|
+
Configuration.new(http_client_instance: http_client_instance,
|
85
|
+
timeout: timeout, max_retries: max_retries,
|
88
86
|
retry_interval: retry_interval,
|
89
87
|
backoff_factor: backoff_factor,
|
90
88
|
retry_statuses: retry_statuses,
|
@@ -99,7 +97,8 @@ module Square
|
|
99
97
|
retry_interval: retry_interval,
|
100
98
|
backoff_factor: backoff_factor,
|
101
99
|
retry_statuses: retry_statuses,
|
102
|
-
retry_methods: retry_methods
|
100
|
+
retry_methods: retry_methods,
|
101
|
+
http_client_instance: http_client_instance)
|
103
102
|
end
|
104
103
|
|
105
104
|
# All the environments the SDK can run in.
|
@@ -25,7 +25,7 @@ module Square
|
|
25
25
|
end.new(*data.values)
|
26
26
|
|
27
27
|
@cursor = data.fetch(:cursor, nil)
|
28
|
-
data.reject! { |k|
|
28
|
+
data.reject! { |k| %i[cursor errors].include?(k) }
|
29
29
|
@data = Struct.new(*data.keys).new(*data.values) if data.keys.any?
|
30
30
|
end
|
31
31
|
else
|
@@ -7,7 +7,25 @@ module Square
|
|
7
7
|
# The constructor.
|
8
8
|
def initialize(timeout:, max_retries:, retry_interval:,
|
9
9
|
backoff_factor:, retry_statuses:, retry_methods:,
|
10
|
-
cache: false, verify: true)
|
10
|
+
http_client_instance: nil, cache: false, verify: true)
|
11
|
+
if http_client_instance.nil?
|
12
|
+
create_connection(timeout: timeout, max_retries: max_retries,
|
13
|
+
retry_interval: retry_interval, backoff_factor: backoff_factor,
|
14
|
+
retry_statuses: retry_statuses, retry_methods: retry_methods,
|
15
|
+
cache: cache, verify: verify)
|
16
|
+
else
|
17
|
+
if http_client_instance.instance_variable_get('@connection').nil?
|
18
|
+
raise ArgumentError,
|
19
|
+
"`connection` cannot be nil in `#{self.class}`. Please specify a valid value."
|
20
|
+
end
|
21
|
+
@connection = http_client_instance.instance_variable_get('@connection')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Method to initialize connection.
|
26
|
+
def create_connection(timeout:, max_retries:, retry_interval:,
|
27
|
+
backoff_factor:, retry_statuses:, retry_methods:,
|
28
|
+
cache: false, verify: true)
|
11
29
|
@connection = Faraday.new do |faraday|
|
12
30
|
faraday.use Faraday::HttpCache, serializer: Marshal if cache
|
13
31
|
faraday.use FaradayMiddleware::FollowRedirects
|
@@ -22,8 +40,9 @@ module Square
|
|
22
40
|
methods: retry_methods
|
23
41
|
faraday.adapter Faraday.default_adapter
|
24
42
|
faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
|
25
|
-
faraday.options[:timeout] = timeout if timeout
|
43
|
+
faraday.options[:timeout] = timeout if timeout.positive?
|
26
44
|
end
|
45
|
+
@connection
|
27
46
|
end
|
28
47
|
|
29
48
|
# Method overridden from HttpClient.
|
@@ -6,7 +6,7 @@ module Square
|
|
6
6
|
# @param [DateTime] The DateTime object
|
7
7
|
# @return [String] The rfc1123 formatted datetime string
|
8
8
|
def self.to_rfc1123(date_time)
|
9
|
-
date_time
|
9
|
+
date_time&.httpdate
|
10
10
|
end
|
11
11
|
|
12
12
|
# Safely converts a map of DateTime objects into a map of rfc1123 format string
|
@@ -86,7 +86,7 @@ module Square
|
|
86
86
|
# @param [DateTime] The DateTime object
|
87
87
|
# @return [String] The rfc3339 formatted datetime string
|
88
88
|
def self.to_rfc3339(date_time)
|
89
|
-
date_time
|
89
|
+
date_time&.rfc3339
|
90
90
|
end
|
91
91
|
|
92
92
|
# Safely converts a map of DateTime objects into a map of rfc1123 format string
|