minty 1.0.0 → 1.1.0

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.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -16
  3. data/Gemfile.lock +28 -208
  4. data/README.md +58 -57
  5. data/Rakefile +4 -27
  6. data/docs/DefaultApi.md +217 -0
  7. data/docs/RedirectUrl.md +18 -0
  8. data/git_push.sh +57 -0
  9. data/lib/minty/api/default_api.rb +210 -0
  10. data/lib/minty/api_client.rb +388 -0
  11. data/lib/minty/api_error.rb +53 -0
  12. data/lib/minty/configuration.rb +275 -0
  13. data/lib/minty/models/redirect_url.rb +216 -0
  14. data/lib/minty/version.rb +9 -3
  15. data/lib/minty.rb +33 -7
  16. data/minty.gemspec +19 -32
  17. data/pkg/minty-1.1.0.gem +0 -0
  18. data/publish_rubygem.sh +1 -1
  19. data/spec/api/default_api_spec.rb +65 -0
  20. data/spec/api_client_spec.rb +222 -0
  21. data/spec/configuration_spec.rb +38 -0
  22. data/spec/models/redirect_url_spec.rb +28 -0
  23. data/spec/spec_helper.rb +95 -63
  24. metadata +37 -292
  25. data/.bundle/config +0 -4
  26. data/.devcontainer/Dockerfile +0 -19
  27. data/.devcontainer/devcontainer.json +0 -37
  28. data/.env.example +0 -2
  29. data/.gemrelease +0 -2
  30. data/.github/PULL_REQUEST_TEMPLATE.md +0 -33
  31. data/.github/dependabot.yml +0 -10
  32. data/.github/stale.yml +0 -20
  33. data/.gitignore +0 -18
  34. data/.rspec +0 -3
  35. data/.rubocop.yml +0 -9
  36. data/CODE_OF_CONDUCT.md +0 -3
  37. data/DEPLOYMENT.md +0 -61
  38. data/DEVELOPMENT.md +0 -35
  39. data/EXAMPLES.md +0 -195
  40. data/Guardfile +0 -39
  41. data/LICENSE +0 -21
  42. data/Makefile +0 -5
  43. data/RUBYGEM.md +0 -9
  44. data/codecov.yml +0 -22
  45. data/lib/minty/algorithm.rb +0 -7
  46. data/lib/minty/api/authentication_endpoints.rb +0 -30
  47. data/lib/minty/api/v2.rb +0 -8
  48. data/lib/minty/client.rb +0 -7
  49. data/lib/minty/exception.rb +0 -50
  50. data/lib/minty/mixins/api_token_struct.rb +0 -4
  51. data/lib/minty/mixins/headers.rb +0 -19
  52. data/lib/minty/mixins/httpproxy.rb +0 -125
  53. data/lib/minty/mixins/initializer.rb +0 -38
  54. data/lib/minty/mixins/validation.rb +0 -113
  55. data/lib/minty/mixins.rb +0 -23
  56. data/lib/minty_client.rb +0 -4
  57. data/spec/integration/lib/minty/api/api_authentication_spec.rb +0 -122
  58. data/spec/integration/lib/minty/minty_client_spec.rb +0 -92
  59. data/spec/lib/minty/client_spec.rb +0 -223
  60. data/spec/lib/minty/mixins/httpproxy_spec.rb +0 -658
  61. data/spec/lib/minty/mixins/initializer_spec.rb +0 -121
  62. data/spec/lib/minty/mixins/token_management_spec.rb +0 -129
  63. data/spec/lib/minty/mixins/validation_spec.rb +0 -559
  64. data/spec/support/credentials.rb +0 -14
  65. data/spec/support/dummy_class.rb +0 -20
  66. data/spec/support/dummy_class_for_proxy.rb +0 -6
  67. data/spec/support/dummy_class_for_restclient.rb +0 -4
  68. data/spec/support/dummy_class_for_tokens.rb +0 -18
  69. data/spec/support/import_users.json +0 -13
  70. data/spec/support/stub_response.rb +0 -3
@@ -0,0 +1,388 @@
1
+ =begin
2
+ #Minty API
3
+
4
+ #Minty API
5
+
6
+
7
+ =end
8
+
9
+ require 'date'
10
+ require 'json'
11
+ require 'logger'
12
+ require 'tempfile'
13
+ require 'time'
14
+ require 'typhoeus'
15
+
16
+ module MintyApi
17
+ class ApiClient
18
+ # The Configuration object holding settings to be used in the API client.
19
+ attr_accessor :config
20
+
21
+ # Defines the headers to be used in HTTP requests of all API calls by default.
22
+ #
23
+ # @return [Hash]
24
+ attr_accessor :default_headers
25
+
26
+ # Initializes the ApiClient
27
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
28
+ def initialize(config = Configuration.default)
29
+ @config = config
30
+ @user_agent = "Minty/#{VERSION}/ruby"
31
+ @default_headers = {
32
+ 'Content-Type' => 'application/json',
33
+ 'User-Agent' => @user_agent
34
+ }
35
+ end
36
+
37
+ def self.default
38
+ @@default ||= ApiClient.new
39
+ end
40
+
41
+ # Call an API with given options.
42
+ #
43
+ # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
44
+ # the data deserialized from response body (could be nil), response status code and response headers.
45
+ def call_api(http_method, path, opts = {})
46
+ request = build_request(http_method, path, opts)
47
+ response = request.run
48
+
49
+ if @config.debugging
50
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
51
+ end
52
+
53
+ unless response.success?
54
+ if response.timed_out?
55
+ fail ApiError.new('Connection timed out')
56
+ elsif response.code == 0
57
+ # Errors from libcurl will be made visible here
58
+ fail ApiError.new(:code => 0,
59
+ :message => response.return_message)
60
+ else
61
+ fail ApiError.new(:code => response.code,
62
+ :response_headers => response.headers,
63
+ :response_body => response.body),
64
+ response.status_message
65
+ end
66
+ end
67
+
68
+ if opts[:return_type]
69
+ data = deserialize(response, opts[:return_type])
70
+ else
71
+ data = nil
72
+ end
73
+ return data, response.code, response.headers
74
+ end
75
+
76
+ # Builds the HTTP request
77
+ #
78
+ # @param [String] http_method HTTP method/verb (e.g. POST)
79
+ # @param [String] path URL path (e.g. /account/new)
80
+ # @option opts [Hash] :header_params Header parameters
81
+ # @option opts [Hash] :query_params Query parameters
82
+ # @option opts [Hash] :form_params Query parameters
83
+ # @option opts [Object] :body HTTP body (JSON/XML)
84
+ # @return [Typhoeus::Request] A Typhoeus Request
85
+ def build_request(http_method, path, opts = {})
86
+ url = build_request_url(path, opts)
87
+ http_method = http_method.to_sym.downcase
88
+
89
+ header_params = @default_headers.merge(opts[:header_params] || {})
90
+ query_params = opts[:query_params] || {}
91
+ form_params = opts[:form_params] || {}
92
+ follow_location = opts[:follow_location] || true
93
+
94
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
95
+
96
+ # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
97
+ _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
98
+
99
+ req_opts = {
100
+ :method => http_method,
101
+ :headers => header_params,
102
+ :params => query_params,
103
+ :params_encoding => @config.params_encoding,
104
+ :timeout => @config.timeout,
105
+ :ssl_verifypeer => @config.verify_ssl,
106
+ :ssl_verifyhost => _verify_ssl_host,
107
+ :sslcert => @config.cert_file,
108
+ :sslkey => @config.key_file,
109
+ :verbose => @config.debugging,
110
+ :followlocation => follow_location
111
+ }
112
+
113
+ # set custom cert, if provided
114
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
115
+
116
+ if [:post, :patch, :put, :delete].include?(http_method)
117
+ req_body = build_request_body(header_params, form_params, opts[:body])
118
+ req_opts.update :body => req_body
119
+ if @config.debugging
120
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
121
+ end
122
+ end
123
+
124
+ request = Typhoeus::Request.new(url, req_opts)
125
+ download_file(request) if opts[:return_type] == 'File'
126
+ request
127
+ end
128
+
129
+ # Builds the HTTP request body
130
+ #
131
+ # @param [Hash] header_params Header parameters
132
+ # @param [Hash] form_params Query parameters
133
+ # @param [Object] body HTTP body (JSON/XML)
134
+ # @return [String] HTTP body data in the form of string
135
+ def build_request_body(header_params, form_params, body)
136
+ # http form
137
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
138
+ header_params['Content-Type'] == 'multipart/form-data'
139
+ data = {}
140
+ form_params.each do |key, value|
141
+ case value
142
+ when ::File, ::Array, nil
143
+ # let typhoeus handle File, Array and nil parameters
144
+ data[key] = value
145
+ else
146
+ data[key] = value.to_s
147
+ end
148
+ end
149
+ elsif body
150
+ data = body.is_a?(String) ? body : body.to_json
151
+ else
152
+ data = nil
153
+ end
154
+ data
155
+ end
156
+
157
+ # Save response body into a file in (the defined) temporary folder, using the filename
158
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
159
+ # The response body is written to the file in chunks in order to handle files which
160
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
161
+ # process can use.
162
+ #
163
+ # @see Configuration#temp_folder_path
164
+ def download_file(request)
165
+ tempfile = nil
166
+ encoding = nil
167
+ request.on_headers do |response|
168
+ content_disposition = response.headers['Content-Disposition']
169
+ if content_disposition && content_disposition =~ /filename=/i
170
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
171
+ prefix = sanitize_filename(filename)
172
+ else
173
+ prefix = 'download-'
174
+ end
175
+ prefix = prefix + '-' unless prefix.end_with?('-')
176
+ encoding = response.body.encoding
177
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
178
+ @tempfile = tempfile
179
+ end
180
+ request.on_body do |chunk|
181
+ chunk.force_encoding(encoding)
182
+ tempfile.write(chunk)
183
+ end
184
+ request.on_complete do |response|
185
+ if tempfile
186
+ tempfile.close
187
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
188
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
189
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
190
+ "explicitly with `tempfile.delete`"
191
+ end
192
+ end
193
+ end
194
+
195
+ # Check if the given MIME is a JSON MIME.
196
+ # JSON MIME examples:
197
+ # application/json
198
+ # application/json; charset=UTF8
199
+ # APPLICATION/JSON
200
+ # */*
201
+ # @param [String] mime MIME
202
+ # @return [Boolean] True if the MIME is application/json
203
+ def json_mime?(mime)
204
+ (mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
205
+ end
206
+
207
+ # Deserialize the response to the given return type.
208
+ #
209
+ # @param [Response] response HTTP response
210
+ # @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
211
+ def deserialize(response, return_type)
212
+ body = response.body
213
+
214
+ # handle file downloading - return the File instance processed in request callbacks
215
+ # note that response body is empty when the file is written in chunks in request on_body callback
216
+ return @tempfile if return_type == 'File'
217
+
218
+ return nil if body.nil? || body.empty?
219
+
220
+ # return response body directly for String return type
221
+ return body if return_type == 'String'
222
+
223
+ # ensuring a default content type
224
+ content_type = response.headers['Content-Type'] || 'application/json'
225
+
226
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
227
+
228
+ begin
229
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
230
+ rescue JSON::ParserError => e
231
+ if %w(String Date Time).include?(return_type)
232
+ data = body
233
+ else
234
+ raise e
235
+ end
236
+ end
237
+
238
+ convert_to_type data, return_type
239
+ end
240
+
241
+ # Convert data to the given return type.
242
+ # @param [Object] data Data to be converted
243
+ # @param [String] return_type Return type
244
+ # @return [Mixed] Data in a particular type
245
+ def convert_to_type(data, return_type)
246
+ return nil if data.nil?
247
+ case return_type
248
+ when 'String'
249
+ data.to_s
250
+ when 'Integer'
251
+ data.to_i
252
+ when 'Float'
253
+ data.to_f
254
+ when 'Boolean'
255
+ data == true
256
+ when 'Time'
257
+ # parse date time (expecting ISO 8601 format)
258
+ Time.parse data
259
+ when 'Date'
260
+ # parse date time (expecting ISO 8601 format)
261
+ Date.parse data
262
+ when 'Object'
263
+ # generic object (usually a Hash), return directly
264
+ data
265
+ when /\AArray<(.+)>\z/
266
+ # e.g. Array<Pet>
267
+ sub_type = $1
268
+ data.map { |item| convert_to_type(item, sub_type) }
269
+ when /\AHash\<String, (.+)\>\z/
270
+ # e.g. Hash<String, Integer>
271
+ sub_type = $1
272
+ {}.tap do |hash|
273
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
274
+ end
275
+ else
276
+ # models (e.g. Pet) or oneOf
277
+ klass = MintyApi.const_get(return_type)
278
+ klass.respond_to?(:openapi_one_of) ? klass.build(data) : klass.build_from_hash(data)
279
+ end
280
+ end
281
+
282
+ # Sanitize filename by removing path.
283
+ # e.g. ../../sun.gif becomes sun.gif
284
+ #
285
+ # @param [String] filename the filename to be sanitized
286
+ # @return [String] the sanitized filename
287
+ def sanitize_filename(filename)
288
+ filename.gsub(/.*[\/\\]/, '')
289
+ end
290
+
291
+ def build_request_url(path, opts = {})
292
+ # Add leading and trailing slashes to path
293
+ path = "/#{path}".gsub(/\/+/, '/')
294
+ @config.base_url(opts[:operation]) + path
295
+ end
296
+
297
+ # Update header and query params based on authentication settings.
298
+ #
299
+ # @param [Hash] header_params Header parameters
300
+ # @param [Hash] query_params Query parameters
301
+ # @param [String] auth_names Authentication scheme name
302
+ def update_params_for_auth!(header_params, query_params, auth_names)
303
+ Array(auth_names).each do |auth_name|
304
+ auth_setting = @config.auth_settings[auth_name]
305
+ next unless auth_setting
306
+ case auth_setting[:in]
307
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
308
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
309
+ else fail ArgumentError, 'Authentication token must be in `query` or `header`'
310
+ end
311
+ end
312
+ end
313
+
314
+ # Sets user agent in HTTP header
315
+ #
316
+ # @param [String] user_agent User agent (e.g. minty/ruby/1.0.0)
317
+ def user_agent=(user_agent)
318
+ @user_agent = user_agent
319
+ @default_headers['User-Agent'] = @user_agent
320
+ end
321
+
322
+ # Return Accept header based on an array of accepts provided.
323
+ # @param [Array] accepts array for Accept
324
+ # @return [String] the Accept header (e.g. application/json)
325
+ def select_header_accept(accepts)
326
+ return nil if accepts.nil? || accepts.empty?
327
+ # use JSON when present, otherwise use all of the provided
328
+ json_accept = accepts.find { |s| json_mime?(s) }
329
+ json_accept || accepts.join(',')
330
+ end
331
+
332
+ # Return Content-Type header based on an array of content types provided.
333
+ # @param [Array] content_types array for Content-Type
334
+ # @return [String] the Content-Type header (e.g. application/json)
335
+ def select_header_content_type(content_types)
336
+ # return nil by default
337
+ return if content_types.nil? || content_types.empty?
338
+ # use JSON when present, otherwise use the first one
339
+ json_content_type = content_types.find { |s| json_mime?(s) }
340
+ json_content_type || content_types.first
341
+ end
342
+
343
+ # Convert object (array, hash, object, etc) to JSON string.
344
+ # @param [Object] model object to be converted into JSON string
345
+ # @return [String] JSON string representation of the object
346
+ def object_to_http_body(model)
347
+ return model if model.nil? || model.is_a?(String)
348
+ local_body = nil
349
+ if model.is_a?(Array)
350
+ local_body = model.map { |m| object_to_hash(m) }
351
+ else
352
+ local_body = object_to_hash(model)
353
+ end
354
+ local_body.to_json
355
+ end
356
+
357
+ # Convert object(non-array) to hash.
358
+ # @param [Object] obj object to be converted into JSON string
359
+ # @return [String] JSON string representation of the object
360
+ def object_to_hash(obj)
361
+ if obj.respond_to?(:to_hash)
362
+ obj.to_hash
363
+ else
364
+ obj
365
+ end
366
+ end
367
+
368
+ # Build parameter value according to the given collection format.
369
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
370
+ def build_collection_param(param, collection_format)
371
+ case collection_format
372
+ when :csv
373
+ param.join(',')
374
+ when :ssv
375
+ param.join(' ')
376
+ when :tsv
377
+ param.join("\t")
378
+ when :pipes
379
+ param.join('|')
380
+ when :multi
381
+ # return the array directly as typhoeus will handle it as expected
382
+ param
383
+ else
384
+ fail "unknown collection format: #{collection_format.inspect}"
385
+ end
386
+ end
387
+ end
388
+ end
@@ -0,0 +1,53 @@
1
+ =begin
2
+ #Minty API
3
+
4
+ #Minty API
5
+
6
+
7
+ =end
8
+
9
+ module MintyApi
10
+ class ApiError < StandardError
11
+ attr_reader :code, :response_headers, :response_body
12
+
13
+ # Usage examples:
14
+ # ApiError.new
15
+ # ApiError.new("message")
16
+ # ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
17
+ # ApiError.new(:code => 404, :message => "Not Found")
18
+ def initialize(arg = nil)
19
+ if arg.is_a? Hash
20
+ if arg.key?(:message) || arg.key?('message')
21
+ super(arg[:message] || arg['message'])
22
+ else
23
+ super arg
24
+ end
25
+
26
+ arg.each do |k, v|
27
+ instance_variable_set "@#{k}", v
28
+ end
29
+ else
30
+ super arg
31
+ end
32
+ end
33
+
34
+ # Override to_s to display a friendly error message
35
+ def to_s
36
+ message
37
+ end
38
+
39
+ def message
40
+ if @message.nil?
41
+ msg = "Error message: the server returns an error"
42
+ else
43
+ msg = @message
44
+ end
45
+
46
+ msg += "\nHTTP status code: #{code}" if code
47
+ msg += "\nResponse headers: #{response_headers}" if response_headers
48
+ msg += "\nResponse body: #{response_body}" if response_body
49
+
50
+ msg
51
+ end
52
+ end
53
+ end