uniqueCalculator 1.1 → 1.2.7
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 +4 -4
- data/README.md +4 -3
- data/lib/apimatic_calculator/api_helper.rb +1 -440
- data/lib/apimatic_calculator/client.rb +9 -2
- data/lib/apimatic_calculator/configuration.rb +14 -40
- data/lib/apimatic_calculator/controllers/base_controller.rb +42 -36
- data/lib/apimatic_calculator/controllers/simple_calculator_controller.rb +13 -27
- data/lib/apimatic_calculator/exceptions/api_exception.rb +1 -11
- data/lib/apimatic_calculator/http/http_call_back.rb +1 -15
- data/lib/apimatic_calculator/http/http_method_enum.rb +1 -4
- data/lib/apimatic_calculator/http/http_request.rb +1 -45
- data/lib/apimatic_calculator/http/http_response.rb +1 -20
- data/lib/apimatic_calculator/models/base_model.rb +1 -1
- data/lib/apimatic_calculator/utilities/date_time_helper.rb +1 -146
- data/lib/apimatic_calculator/utilities/file_wrapper.rb +5 -5
- data/lib/apimatic_calculator.rb +6 -6
- data/test/controllers/controller_test_base.rb +9 -2
- data/test/controllers/test_simple_calculator_controller.rb +3 -2
- metadata +11 -111
- data/lib/apimatic_calculator/exceptions/validation_exception.rb +0 -18
- data/lib/apimatic_calculator/http/faraday_client.rb +0 -98
- data/lib/apimatic_calculator/http/http_client.rb +0 -123
- data/test/test_helper.rb +0 -94
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86aad3f45868b73711243987949b9b43ba27c7bab0036f4c6c4b38f67c1fcb2c
|
4
|
+
data.tar.gz: 857989d73acb6f7dba24d18f8e509b8af53dab01dfc87e40d06acfbd777a8660
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a299700e3332a54d8481c030a413d2f602d25d0331df05e786d3ca78eb7eaf0268e2a661c03cf66fd8faf0468fb8ecca9e3b262b241a493eaf1db59e173745ad
|
7
|
+
data.tar.gz: 878c7c45cf6170115ad5adb36672186ec65277f3c9cb87dae6cf71359ec5572844c75783f78874b26b22e4a9322b7eb2bc951f585dc6043b6ea325654516e820
|
data/README.md
CHANGED
@@ -10,16 +10,16 @@ Simple calculator API hosted on APIMATIC
|
|
10
10
|
Install the gem from the command line:
|
11
11
|
|
12
12
|
```ruby
|
13
|
-
gem install uniqueCalculator -v 1.
|
13
|
+
gem install uniqueCalculator -v 1.2.7
|
14
14
|
```
|
15
15
|
|
16
16
|
Or add the gem to your Gemfile and run `bundle`:
|
17
17
|
|
18
18
|
```ruby
|
19
|
-
gem 'uniqueCalculator', '1.
|
19
|
+
gem 'uniqueCalculator', '1.2.7'
|
20
20
|
```
|
21
21
|
|
22
|
-
For additional gem details, see the [RubyGems page for the uniqueCalculator gem](https://rubygems.org/gems/uniqueCalculator/versions/1.
|
22
|
+
For additional gem details, see the [RubyGems page for the uniqueCalculator gem](https://rubygems.org/gems/uniqueCalculator/versions/1.2.7).
|
23
23
|
|
24
24
|
## Test the SDK
|
25
25
|
|
@@ -46,6 +46,7 @@ The following parameters are configurable for the API Client:
|
|
46
46
|
| `backoff_factor` | `Float` | The amount to multiply each successive retry's interval amount by in order to provide backoff. <br> **Default: 2** |
|
47
47
|
| `retry_statuses` | `Array` | A list of HTTP statuses to retry. <br> **Default: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524]** |
|
48
48
|
| `retry_methods` | `Array` | A list of HTTP methods to retry. <br> **Default: %i[get put]** |
|
49
|
+
| `http_callback` | `HttpCallBack` | The Http CallBack allows defining callables for pre and post API calls. |
|
49
50
|
|
50
51
|
The API client can be initialized as follows:
|
51
52
|
|
@@ -5,445 +5,6 @@
|
|
5
5
|
|
6
6
|
module ApimaticCalculator
|
7
7
|
# API utility class
|
8
|
-
class APIHelper
|
9
|
-
# Serializes an array parameter (creates key value pairs).
|
10
|
-
# @param [String] The name of the parameter.
|
11
|
-
# @param [Array] The value of the parameter.
|
12
|
-
# @param [String] The format of the serialization.
|
13
|
-
def self.serialize_array(key, array, formatting: 'indexed')
|
14
|
-
tuples = []
|
15
|
-
|
16
|
-
tuples += case formatting
|
17
|
-
when 'csv'
|
18
|
-
[[key, array.map { |element| CGI.escape(element.to_s) }.join(',')]]
|
19
|
-
when 'psv'
|
20
|
-
[[key, array.map { |element| CGI.escape(element.to_s) }.join('|')]]
|
21
|
-
when 'tsv'
|
22
|
-
[[key, array.map { |element| CGI.escape(element.to_s) }.join("\t")]]
|
23
|
-
else
|
24
|
-
array.map { |element| [key, element] }
|
25
|
-
end
|
26
|
-
tuples
|
27
|
-
end
|
28
|
-
|
29
|
-
# Replaces template parameters in the given url.
|
30
|
-
# @param [String] The query string builder to replace the template
|
31
|
-
# parameters.
|
32
|
-
# @param [Hash] The parameters to replace in the url.
|
33
|
-
def self.append_url_with_template_parameters(query_builder, parameters)
|
34
|
-
# perform parameter validation
|
35
|
-
unless query_builder.instance_of? String
|
36
|
-
raise ArgumentError, 'Given value for parameter \"query_builder\" is
|
37
|
-
invalid.'
|
38
|
-
end
|
39
|
-
|
40
|
-
# Return if there are no parameters to replace.
|
41
|
-
return query_builder if parameters.nil?
|
42
|
-
|
43
|
-
parameters.each do |key, val|
|
44
|
-
if val.nil?
|
45
|
-
replace_value = ''
|
46
|
-
elsif val['value'].instance_of? Array
|
47
|
-
if val['encode'] == true
|
48
|
-
val['value'].map! { |element| CGI.escape(element.to_s) }
|
49
|
-
else
|
50
|
-
val['value'].map!(&:to_s)
|
51
|
-
end
|
52
|
-
replace_value = val['value'].join('/')
|
53
|
-
else
|
54
|
-
replace_value = if val['encode'] == true
|
55
|
-
CGI.escape(val['value'].to_s)
|
56
|
-
else
|
57
|
-
val['value'].to_s
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
# Find the template parameter and replace it with its value.
|
62
|
-
query_builder = query_builder.gsub("{#{key}}", replace_value)
|
63
|
-
end
|
64
|
-
query_builder
|
65
|
-
end
|
66
|
-
|
67
|
-
# Appends the given set of parameters to the given query string.
|
68
|
-
# @param [String] The query string builder to add the query parameters to.
|
69
|
-
# @param [Hash] The parameters to append.
|
70
|
-
def self.append_url_with_query_parameters(query_builder, parameters)
|
71
|
-
# Perform parameter validation.
|
72
|
-
unless query_builder.instance_of? String
|
73
|
-
raise ArgumentError, 'Given value for parameter \"query_builder\"
|
74
|
-
is invalid.'
|
75
|
-
end
|
76
|
-
|
77
|
-
# Return if there are no parameters to replace.
|
78
|
-
return query_builder if parameters.nil?
|
79
|
-
|
80
|
-
array_serialization = 'indexed'
|
81
|
-
parameters = process_complex_types_parameters(parameters, array_serialization)
|
82
|
-
|
83
|
-
parameters.each do |key, value|
|
84
|
-
seperator = query_builder.include?('?') ? '&' : '?'
|
85
|
-
unless value.nil?
|
86
|
-
if value.instance_of? Array
|
87
|
-
value.compact!
|
88
|
-
APIHelper.serialize_array(
|
89
|
-
key, value, formatting: array_serialization
|
90
|
-
).each do |element|
|
91
|
-
seperator = query_builder.include?('?') ? '&' : '?'
|
92
|
-
query_builder += "#{seperator}#{element[0]}=#{element[1]}"
|
93
|
-
end
|
94
|
-
else
|
95
|
-
query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
query_builder
|
100
|
-
end
|
101
|
-
|
102
|
-
# Validates and processes the given Url.
|
103
|
-
# @param [String] The given Url to process.
|
104
|
-
# @return [String] Pre-processed Url as string.
|
105
|
-
def self.clean_url(url)
|
106
|
-
# Perform parameter validation.
|
107
|
-
raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
|
108
|
-
|
109
|
-
# Ensure that the urls are absolute.
|
110
|
-
matches = url.match(%r{^(https?://[^/]+)})
|
111
|
-
raise ArgumentError, 'Invalid Url format.' if matches.nil?
|
112
|
-
|
113
|
-
# Get the http protocol match.
|
114
|
-
protocol = matches[1]
|
115
|
-
|
116
|
-
# Check if parameters exist.
|
117
|
-
index = url.index('?')
|
118
|
-
|
119
|
-
# Remove redundant forward slashes.
|
120
|
-
query = url[protocol.length...(!index.nil? ? index : url.length)]
|
121
|
-
query.gsub!(%r{//+}, '/')
|
122
|
-
|
123
|
-
# Get the parameters.
|
124
|
-
parameters = !index.nil? ? url[url.index('?')...url.length] : ''
|
125
|
-
|
126
|
-
# Return processed url.
|
127
|
-
protocol + query + parameters
|
128
|
-
end
|
129
|
-
|
130
|
-
# Parses JSON string.
|
131
|
-
# @param [String] A JSON string.
|
132
|
-
def self.json_deserialize(json)
|
133
|
-
JSON.parse(json)
|
134
|
-
rescue StandardError
|
135
|
-
raise TypeError, 'Server responded with invalid JSON.'
|
136
|
-
end
|
137
|
-
|
138
|
-
# Parses JSON string.
|
139
|
-
# @param [object] The object to serialize.
|
140
|
-
def self.json_serialize(obj)
|
141
|
-
serializable_types.map { |x| obj.is_a? x }.any? ? obj.to_s : obj.to_json
|
142
|
-
end
|
143
|
-
|
144
|
-
# Removes elements with empty values from a hash.
|
145
|
-
# @param [Hash] The hash to clean.
|
146
|
-
def self.clean_hash(hash)
|
147
|
-
hash.delete_if { |_key, value| value.to_s.strip.empty? }
|
148
|
-
end
|
149
|
-
|
150
|
-
# Form encodes a hash of parameters.
|
151
|
-
# @param [Hash] The hash of parameters to encode.
|
152
|
-
# @return [Hash] A hash with the same parameters form encoded.
|
153
|
-
def self.form_encode_parameters(form_parameters)
|
154
|
-
array_serialization = 'indexed'
|
155
|
-
encoded = {}
|
156
|
-
form_parameters.each do |key, value|
|
157
|
-
encoded.merge!(APIHelper.form_encode(value, key, formatting:
|
158
|
-
array_serialization))
|
159
|
-
end
|
160
|
-
encoded
|
161
|
-
end
|
162
|
-
|
163
|
-
# Process complex types in query_params.
|
164
|
-
# @param [Hash] The hash of query parameters.
|
165
|
-
# @return [Hash] A hash with the processed query parameters.
|
166
|
-
def self.process_complex_types_parameters(query_parameters, array_serialization)
|
167
|
-
processed_params = {}
|
168
|
-
query_parameters.each do |key, value|
|
169
|
-
processed_params.merge!(APIHelper.form_encode(value, key, formatting:
|
170
|
-
array_serialization))
|
171
|
-
end
|
172
|
-
processed_params
|
173
|
-
end
|
174
|
-
|
175
|
-
def self.custom_merge(a, b)
|
176
|
-
x = {}
|
177
|
-
a.each do |key, value_a|
|
178
|
-
b.each do |k, value_b|
|
179
|
-
next unless key == k
|
180
|
-
|
181
|
-
x[k] = []
|
182
|
-
if value_a.instance_of? Array
|
183
|
-
value_a.each do |v|
|
184
|
-
x[k].push(v)
|
185
|
-
end
|
186
|
-
else
|
187
|
-
x[k].push(value_a)
|
188
|
-
end
|
189
|
-
if value_b.instance_of? Array
|
190
|
-
value_b.each do |v|
|
191
|
-
x[k].push(v)
|
192
|
-
end
|
193
|
-
else
|
194
|
-
x[k].push(value_b)
|
195
|
-
end
|
196
|
-
a.delete(k)
|
197
|
-
b.delete(k)
|
198
|
-
end
|
199
|
-
end
|
200
|
-
x.merge!(a)
|
201
|
-
x.merge!(b)
|
202
|
-
x
|
203
|
-
end
|
204
|
-
|
205
|
-
# Form encodes an object.
|
206
|
-
# @param [Dynamic] An object to form encode.
|
207
|
-
# @param [String] The name of the object.
|
208
|
-
# @return [Hash] A form encoded representation of the object in the form
|
209
|
-
# of a hash.
|
210
|
-
def self.form_encode(obj, instance_name, formatting: 'indexed')
|
211
|
-
retval = {}
|
212
|
-
|
213
|
-
# If this is a structure, resolve it's field names.
|
214
|
-
obj = obj.to_hash if obj.is_a? BaseModel
|
215
|
-
|
216
|
-
# Create a form encoded hash for this object.
|
217
|
-
if obj.nil?
|
218
|
-
nil
|
219
|
-
elsif obj.instance_of? Array
|
220
|
-
if formatting == 'indexed'
|
221
|
-
obj.each_with_index do |value, index|
|
222
|
-
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]"))
|
223
|
-
end
|
224
|
-
elsif APIHelper.serializable_types.map { |x| obj[0].is_a? x }.any?
|
225
|
-
obj.each do |value|
|
226
|
-
abc = if formatting == 'unindexed'
|
227
|
-
APIHelper.form_encode(value, "#{instance_name}[]",
|
228
|
-
formatting: formatting)
|
229
|
-
else
|
230
|
-
APIHelper.form_encode(value, instance_name,
|
231
|
-
formatting: formatting)
|
232
|
-
end
|
233
|
-
retval = APIHelper.custom_merge(retval, abc)
|
234
|
-
end
|
235
|
-
else
|
236
|
-
obj.each_with_index do |value, index|
|
237
|
-
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]",
|
238
|
-
formatting: formatting))
|
239
|
-
end
|
240
|
-
end
|
241
|
-
elsif obj.instance_of? Hash
|
242
|
-
obj.each do |key, value|
|
243
|
-
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{key}]",
|
244
|
-
formatting: formatting))
|
245
|
-
end
|
246
|
-
elsif obj.instance_of? File
|
247
|
-
retval[instance_name] = UploadIO.new(
|
248
|
-
obj, 'application/octet-stream', File.basename(obj.path)
|
249
|
-
)
|
250
|
-
else
|
251
|
-
retval[instance_name] = obj
|
252
|
-
end
|
253
|
-
retval
|
254
|
-
end
|
255
|
-
|
256
|
-
# Retrieves a field from a Hash/Array based on an Array of keys/indexes
|
257
|
-
# @param [Hash, Array] The hash to extract data from
|
258
|
-
# @param [Array<String, Integer>] The keys/indexes to use
|
259
|
-
# @return [Object] The extracted value
|
260
|
-
def self.map_response(obj, keys)
|
261
|
-
val = obj
|
262
|
-
begin
|
263
|
-
keys.each do |key|
|
264
|
-
val = if val.is_a? Array
|
265
|
-
if key.to_i.to_s == key
|
266
|
-
val[key.to_i]
|
267
|
-
else
|
268
|
-
val = nil
|
269
|
-
end
|
270
|
-
else
|
271
|
-
val.fetch(key.to_sym)
|
272
|
-
end
|
273
|
-
end
|
274
|
-
rescue NoMethodError, TypeError, IndexError
|
275
|
-
val = nil
|
276
|
-
end
|
277
|
-
val
|
278
|
-
end
|
279
|
-
|
280
|
-
# Deserialize the value against the template (group of types).
|
281
|
-
# @param [String] The value to be deserialized.
|
282
|
-
# @param [String] The parameter indicates the type-combination
|
283
|
-
# against which the value will be mapped (oneOf(Integer, String)).
|
284
|
-
def self.deserialize(template, value)
|
285
|
-
decoded = APIHelper.json_deserialize(value)
|
286
|
-
map_types(decoded, template)
|
287
|
-
end
|
288
|
-
|
289
|
-
# Validates and processes the value against the template(group of types).
|
290
|
-
# @param [String] The value to be mapped against the template.
|
291
|
-
# @param [String] The parameter indicates the group of types (oneOf(Integer, String)).
|
292
|
-
# @param [String] The parameter indicates the group (oneOf|anyOf).
|
293
|
-
def self.map_types(value, template, group_name: nil)
|
294
|
-
result_value = nil
|
295
|
-
matches = 0
|
296
|
-
types = []
|
297
|
-
group_name = template.partition('(').first if group_name.nil? && template.match?(/anyOf|oneOf/)
|
298
|
-
|
299
|
-
return if value.nil?
|
300
|
-
|
301
|
-
if template.end_with?('{}') || template.end_with?('[]')
|
302
|
-
types = template.split(group_name, 2).last.gsub(/\s+/, '').split
|
303
|
-
else
|
304
|
-
template = template.split(group_name, 2).last.delete_prefix('(').delete_suffix(')')
|
305
|
-
types = template.scan(/(anyOf|oneOf)[(]([^[)]]*)[)]/).flatten.combination(2).map { |a, b| "#{a}(#{b})" }
|
306
|
-
types.each { |t| template = template.gsub(", #{t}", '') }
|
307
|
-
types = template.gsub(/\s+/, '').split(',').push(*types)
|
308
|
-
end
|
309
|
-
types.each do |element|
|
310
|
-
if element.match?(/^(oneOf|anyOf)[(].*$/)
|
311
|
-
begin
|
312
|
-
result_value = map_types(value, element, matches)
|
313
|
-
matches += 1
|
314
|
-
rescue ValidationException
|
315
|
-
next
|
316
|
-
end
|
317
|
-
elsif element.end_with?('{}')
|
318
|
-
result_value, matches = map_hash_type(value, element, group_name, matches)
|
319
|
-
elsif element.end_with?('[]')
|
320
|
-
result_value, matches = map_array_type(value, element, group_name, matches)
|
321
|
-
else
|
322
|
-
begin
|
323
|
-
result_value, matches = map_type(value, element, group_name, matches)
|
324
|
-
rescue StandardError
|
325
|
-
next
|
326
|
-
end
|
327
|
-
end
|
328
|
-
break if group_name == 'anyOf' && matches == 1
|
329
|
-
end
|
330
|
-
raise ValidationException.new(value, template) unless matches == 1
|
331
|
-
|
332
|
-
value = result_value unless result_value.nil?
|
333
|
-
value
|
334
|
-
end
|
335
|
-
|
336
|
-
# Validates and processes the value against the [Hash] type.
|
337
|
-
# @param [String] The value to be mapped against the type.
|
338
|
-
# @param [String] The possible type of the value.
|
339
|
-
# @param [String] The parameter indicates the group (oneOf|anyOf).
|
340
|
-
# @param [Integer] The parameter indicates the number of matches of value against types.
|
341
|
-
def self.map_hash_type(value, type, group_name, matches)
|
342
|
-
if value.instance_of? Hash
|
343
|
-
decoded = {}
|
344
|
-
value.each do |key, val|
|
345
|
-
type = type.chomp('{}').to_s
|
346
|
-
val = map_types(val, type, group_name: group_name)
|
347
|
-
decoded[key] = val unless type.empty?
|
348
|
-
rescue ValidationException
|
349
|
-
next
|
350
|
-
end
|
351
|
-
matches += 1 if decoded.length == value.length
|
352
|
-
value = decoded unless decoded.empty?
|
353
|
-
end
|
354
|
-
[value, matches]
|
355
|
-
end
|
356
|
-
|
357
|
-
# Validates and processes the value against the [Array] type.
|
358
|
-
# @param [String] The value to be mapped against the type.
|
359
|
-
# @param [String] The possible type of the value.
|
360
|
-
# @param [String] The parameter indicates the group (oneOf|anyOf).
|
361
|
-
# @param [Integer] The parameter indicates the number of matches of value against types.
|
362
|
-
def self.map_array_type(value, type, group_name, matches)
|
363
|
-
if value.instance_of? Array
|
364
|
-
decoded = []
|
365
|
-
value.each do |val|
|
366
|
-
type = type.chomp('[]').to_s
|
367
|
-
val = map_types(val, type, group_name: group_name)
|
368
|
-
decoded.append(val) unless type.empty?
|
369
|
-
rescue ValidationException
|
370
|
-
next
|
371
|
-
end
|
372
|
-
matches += 1 if decoded.length == value.length
|
373
|
-
value = decoded unless decoded.empty?
|
374
|
-
end
|
375
|
-
[value, matches]
|
376
|
-
end
|
377
|
-
|
378
|
-
# Validates and processes the value against the type.
|
379
|
-
# @param [String] The value to be mapped against the type.
|
380
|
-
# @param [String] The possible type of the value.
|
381
|
-
# @param [String] The parameter indicates the group (oneOf|anyOf).
|
382
|
-
# @param [Integer] The parameter indicates the number of matches of value against types.
|
383
|
-
def self.map_type(value, type, _group_name, matches)
|
384
|
-
if ApimaticCalculator.constants.select do |c|
|
385
|
-
ApimaticCalculator.const_get(c).to_s == "ApimaticCalculator::#{type}"
|
386
|
-
end.empty?
|
387
|
-
value, matches = map_data_type(value, type, matches)
|
388
|
-
else
|
389
|
-
value, matches = map_complex_type(value, type, matches)
|
390
|
-
end
|
391
|
-
[value, matches]
|
392
|
-
end
|
393
|
-
|
394
|
-
# Validates and processes the value against the complex types.
|
395
|
-
# @param [String] The value to be mapped against the type.
|
396
|
-
# @param [String] The possible type of the value.
|
397
|
-
# @param [Integer] The parameter indicates the number of matches of value against types.
|
398
|
-
def self.map_complex_type(value, type, matches)
|
399
|
-
obj = ApimaticCalculator.const_get(type)
|
400
|
-
value = if obj.respond_to? 'from_hash'
|
401
|
-
obj.send('from_hash', value)
|
402
|
-
else
|
403
|
-
obj.constants.find { |k| obj.const_get(k) == value }
|
404
|
-
end
|
405
|
-
matches += 1 unless value.nil?
|
406
|
-
[value, matches]
|
407
|
-
end
|
408
|
-
|
409
|
-
# Validates and processes the value against the data types.
|
410
|
-
# @param [String] The value to be mapped against the type.
|
411
|
-
# @param [String] The possible type of the value.
|
412
|
-
# @param [Integer] The parameter indicates the number of matches of value against types.
|
413
|
-
def self.map_data_type(value, element, matches)
|
414
|
-
element = element.split('|').map { |x| Object.const_get x }
|
415
|
-
matches += 1 if element.all? { |x| APIHelper.data_types.include?(x) } &&
|
416
|
-
element.any? { |x| (value.instance_of? x) || (value.class.ancestors.include? x) }
|
417
|
-
[value, matches]
|
418
|
-
end
|
419
|
-
|
420
|
-
# Validates the value against the template(group of types).
|
421
|
-
# @param [String] The value to be mapped against the type.
|
422
|
-
# @param [String] The parameter indicates the group of types (oneOf(Integer, String)).
|
423
|
-
def self.validate_types(value, template)
|
424
|
-
map_types(APIHelper.json_deserialize(value.to_json), template)
|
425
|
-
end
|
426
|
-
|
427
|
-
# Get content-type depending on the value
|
428
|
-
def self.get_content_type(value)
|
429
|
-
if serializable_types.map { |x| value.is_a? x }.any?
|
430
|
-
'text/plain; charset=utf-8'
|
431
|
-
else
|
432
|
-
'application/json; charset=utf-8'
|
433
|
-
end
|
434
|
-
end
|
435
|
-
|
436
|
-
# Array of serializable types
|
437
|
-
def self.serializable_types
|
438
|
-
[String, Numeric, TrueClass,
|
439
|
-
FalseClass, Date, DateTime]
|
440
|
-
end
|
441
|
-
|
442
|
-
# Array of supported data types
|
443
|
-
def self.data_types
|
444
|
-
[String, Float, Integer,
|
445
|
-
TrueClass, FalseClass, Date,
|
446
|
-
DateTime, Array, Hash, Object]
|
447
|
-
end
|
8
|
+
class APIHelper < CoreLibrary::ApiHelper
|
448
9
|
end
|
449
10
|
end
|
@@ -11,13 +11,13 @@ module ApimaticCalculator
|
|
11
11
|
# Access to simple_calculator controller.
|
12
12
|
# @return [SimpleCalculatorController] Returns the controller instance.
|
13
13
|
def simple_calculator
|
14
|
-
@simple_calculator ||= SimpleCalculatorController.new
|
14
|
+
@simple_calculator ||= SimpleCalculatorController.new @global_configuration
|
15
15
|
end
|
16
16
|
|
17
17
|
def initialize(connection: nil, adapter: :net_http_persistent, timeout: 60,
|
18
18
|
max_retries: 0, retry_interval: 1, backoff_factor: 2,
|
19
19
|
retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
|
20
|
-
retry_methods: %i[get put],
|
20
|
+
retry_methods: %i[get put], http_callback: nil,
|
21
21
|
environment: Environment::PRODUCTION, config: nil)
|
22
22
|
@config = if config.nil?
|
23
23
|
Configuration.new(connection: connection, adapter: adapter,
|
@@ -26,10 +26,17 @@ module ApimaticCalculator
|
|
26
26
|
backoff_factor: backoff_factor,
|
27
27
|
retry_statuses: retry_statuses,
|
28
28
|
retry_methods: retry_methods,
|
29
|
+
http_callback: http_callback,
|
29
30
|
environment: environment)
|
30
31
|
else
|
31
32
|
config
|
32
33
|
end
|
34
|
+
|
35
|
+
@global_configuration = GlobalConfiguration.new(client_configuration: @config)
|
36
|
+
.base_uri_executor(@config.method(:get_base_uri))
|
37
|
+
.global_errors(BaseController::GLOBAL_ERRORS)
|
38
|
+
.user_agent(BaseController.user_agent)
|
39
|
+
.sdk_module(ApimaticCalculator)
|
33
40
|
end
|
34
41
|
end
|
35
42
|
end
|
@@ -21,10 +21,9 @@ module ApimaticCalculator
|
|
21
21
|
|
22
22
|
# All configuration including auth info and base URI for the API access
|
23
23
|
# are configured in this class.
|
24
|
-
class Configuration
|
24
|
+
class Configuration < CoreLibrary::HttpClientConfiguration
|
25
25
|
# The attribute readers for properties.
|
26
|
-
attr_reader :
|
27
|
-
:backoff_factor, :retry_statuses, :retry_methods, :environment
|
26
|
+
attr_reader :environment
|
28
27
|
|
29
28
|
class << self
|
30
29
|
attr_reader :environments
|
@@ -33,43 +32,25 @@ module ApimaticCalculator
|
|
33
32
|
def initialize(connection: nil, adapter: :net_http_persistent, timeout: 60,
|
34
33
|
max_retries: 0, retry_interval: 1, backoff_factor: 2,
|
35
34
|
retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
|
36
|
-
retry_methods: %i[get put],
|
35
|
+
retry_methods: %i[get put], http_callback: nil,
|
37
36
|
environment: Environment::PRODUCTION)
|
38
|
-
# The Faraday connection object passed by the SDK user for making requests
|
39
|
-
@connection = connection
|
40
37
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
@timeout = timeout
|
46
|
-
|
47
|
-
# The number of times to retry an endpoint call if it fails
|
48
|
-
@max_retries = max_retries
|
49
|
-
|
50
|
-
# Pause in seconds between retries
|
51
|
-
@retry_interval = retry_interval
|
52
|
-
|
53
|
-
# The amount to multiply each successive retry's interval amount
|
54
|
-
# by in order to provide backoff
|
55
|
-
@backoff_factor = backoff_factor
|
56
|
-
|
57
|
-
# A list of HTTP statuses to retry
|
58
|
-
@retry_statuses = retry_statuses
|
59
|
-
|
60
|
-
# A list of HTTP methods to retry
|
61
|
-
@retry_methods = retry_methods
|
38
|
+
super connection: connection, adapter: adapter, timeout: timeout,
|
39
|
+
max_retries: max_retries, retry_interval: retry_interval,
|
40
|
+
backoff_factor: backoff_factor, retry_statuses: retry_statuses,
|
41
|
+
retry_methods: retry_methods, http_callback: http_callback
|
62
42
|
|
63
43
|
# Current API environment
|
64
44
|
@environment = String(environment)
|
65
45
|
|
66
46
|
# The Http Client to use for making requests.
|
67
|
-
|
47
|
+
set_http_client CoreLibrary::FaradayClient.new(self)
|
68
48
|
end
|
69
49
|
|
70
50
|
def clone_with(connection: nil, adapter: nil, timeout: nil,
|
71
51
|
max_retries: nil, retry_interval: nil, backoff_factor: nil,
|
72
|
-
retry_statuses: nil, retry_methods: nil,
|
52
|
+
retry_statuses: nil, retry_methods: nil, http_callback: nil,
|
53
|
+
environment: nil)
|
73
54
|
connection ||= self.connection
|
74
55
|
adapter ||= self.adapter
|
75
56
|
timeout ||= self.timeout
|
@@ -78,6 +59,7 @@ module ApimaticCalculator
|
|
78
59
|
backoff_factor ||= self.backoff_factor
|
79
60
|
retry_statuses ||= self.retry_statuses
|
80
61
|
retry_methods ||= self.retry_methods
|
62
|
+
http_callback ||= self.http_callback
|
81
63
|
environment ||= self.environment
|
82
64
|
|
83
65
|
Configuration.new(connection: connection, adapter: adapter,
|
@@ -85,16 +67,8 @@ module ApimaticCalculator
|
|
85
67
|
retry_interval: retry_interval,
|
86
68
|
backoff_factor: backoff_factor,
|
87
69
|
retry_statuses: retry_statuses,
|
88
|
-
retry_methods: retry_methods,
|
89
|
-
|
90
|
-
|
91
|
-
def create_http_client
|
92
|
-
FaradayClient.new(timeout: timeout, max_retries: max_retries,
|
93
|
-
retry_interval: retry_interval,
|
94
|
-
backoff_factor: backoff_factor,
|
95
|
-
retry_statuses: retry_statuses,
|
96
|
-
retry_methods: retry_methods, connection: connection,
|
97
|
-
adapter: adapter)
|
70
|
+
retry_methods: retry_methods,
|
71
|
+
http_callback: http_callback, environment: environment)
|
98
72
|
end
|
99
73
|
|
100
74
|
# All the environments the SDK can run in.
|
@@ -105,7 +79,7 @@ module ApimaticCalculator
|
|
105
79
|
}.freeze
|
106
80
|
|
107
81
|
# Generates the appropriate base URI for the environment and the server.
|
108
|
-
# @param [Configuration::Server] The server enum for which the base URI is
|
82
|
+
# @param [Configuration::Server] server The server enum for which the base URI is
|
109
83
|
# required.
|
110
84
|
# @return [String] The base URI.
|
111
85
|
def get_base_uri(server = Server::CALCULATOR)
|