groupdocs_parser_cloud 19.11

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 (64) hide show
  1. checksums.yaml +7 -0
  2. data/lib/groupdocs_parser_cloud.rb +91 -0
  3. data/lib/groupdocs_parser_cloud/api/file_api.rb +729 -0
  4. data/lib/groupdocs_parser_cloud/api/folder_api.rb +694 -0
  5. data/lib/groupdocs_parser_cloud/api/info_api.rb +363 -0
  6. data/lib/groupdocs_parser_cloud/api/parse_api.rb +411 -0
  7. data/lib/groupdocs_parser_cloud/api/storage_api.rb +541 -0
  8. data/lib/groupdocs_parser_cloud/api/template_api.rb +410 -0
  9. data/lib/groupdocs_parser_cloud/api_client.rb +380 -0
  10. data/lib/groupdocs_parser_cloud/api_error.rb +64 -0
  11. data/lib/groupdocs_parser_cloud/configuration.rb +95 -0
  12. data/lib/groupdocs_parser_cloud/models/container_item.rb +246 -0
  13. data/lib/groupdocs_parser_cloud/models/container_item_info.rb +224 -0
  14. data/lib/groupdocs_parser_cloud/models/container_options.rb +224 -0
  15. data/lib/groupdocs_parser_cloud/models/container_result.rb +216 -0
  16. data/lib/groupdocs_parser_cloud/models/coordinates.rb +264 -0
  17. data/lib/groupdocs_parser_cloud/models/create_template_options.rb +234 -0
  18. data/lib/groupdocs_parser_cloud/models/detector_parameters.rb +266 -0
  19. data/lib/groupdocs_parser_cloud/models/disc_usage.rb +234 -0
  20. data/lib/groupdocs_parser_cloud/models/error.rb +244 -0
  21. data/lib/groupdocs_parser_cloud/models/error_details.rb +229 -0
  22. data/lib/groupdocs_parser_cloud/models/field.rb +234 -0
  23. data/lib/groupdocs_parser_cloud/models/field_data.rb +249 -0
  24. data/lib/groupdocs_parser_cloud/models/field_position.rb +344 -0
  25. data/lib/groupdocs_parser_cloud/models/file_info.rb +234 -0
  26. data/lib/groupdocs_parser_cloud/models/file_type.rb +224 -0
  27. data/lib/groupdocs_parser_cloud/models/file_version.rb +289 -0
  28. data/lib/groupdocs_parser_cloud/models/file_versions.rb +216 -0
  29. data/lib/groupdocs_parser_cloud/models/files_list.rb +216 -0
  30. data/lib/groupdocs_parser_cloud/models/files_upload_result.rb +228 -0
  31. data/lib/groupdocs_parser_cloud/models/format.rb +224 -0
  32. data/lib/groupdocs_parser_cloud/models/formats_result.rb +216 -0
  33. data/lib/groupdocs_parser_cloud/models/formatted_text_options.rb +214 -0
  34. data/lib/groupdocs_parser_cloud/models/image.rb +224 -0
  35. data/lib/groupdocs_parser_cloud/models/image_page.rb +231 -0
  36. data/lib/groupdocs_parser_cloud/models/images_options.rb +254 -0
  37. data/lib/groupdocs_parser_cloud/models/images_result.rb +228 -0
  38. data/lib/groupdocs_parser_cloud/models/info_options.rb +224 -0
  39. data/lib/groupdocs_parser_cloud/models/info_result.rb +244 -0
  40. data/lib/groupdocs_parser_cloud/models/object_exist.rb +234 -0
  41. data/lib/groupdocs_parser_cloud/models/page.rb +229 -0
  42. data/lib/groupdocs_parser_cloud/models/page_area.rb +244 -0
  43. data/lib/groupdocs_parser_cloud/models/page_table_area.rb +246 -0
  44. data/lib/groupdocs_parser_cloud/models/page_table_area_cell.rb +274 -0
  45. data/lib/groupdocs_parser_cloud/models/page_text_area.rb +251 -0
  46. data/lib/groupdocs_parser_cloud/models/parse_options.rb +244 -0
  47. data/lib/groupdocs_parser_cloud/models/parse_result.rb +231 -0
  48. data/lib/groupdocs_parser_cloud/models/parser_options.rb +224 -0
  49. data/lib/groupdocs_parser_cloud/models/point.rb +234 -0
  50. data/lib/groupdocs_parser_cloud/models/rectangle.rb +234 -0
  51. data/lib/groupdocs_parser_cloud/models/size.rb +234 -0
  52. data/lib/groupdocs_parser_cloud/models/storage_exist.rb +219 -0
  53. data/lib/groupdocs_parser_cloud/models/storage_file.rb +264 -0
  54. data/lib/groupdocs_parser_cloud/models/table.rb +244 -0
  55. data/lib/groupdocs_parser_cloud/models/table_layout.rb +228 -0
  56. data/lib/groupdocs_parser_cloud/models/template.rb +228 -0
  57. data/lib/groupdocs_parser_cloud/models/template_options.rb +224 -0
  58. data/lib/groupdocs_parser_cloud/models/template_result.rb +224 -0
  59. data/lib/groupdocs_parser_cloud/models/text_options.rb +254 -0
  60. data/lib/groupdocs_parser_cloud/models/text_page.rb +229 -0
  61. data/lib/groupdocs_parser_cloud/models/text_result.rb +226 -0
  62. data/lib/groupdocs_parser_cloud/models/text_style.rb +269 -0
  63. data/lib/groupdocs_parser_cloud/version.rb +29 -0
  64. metadata +174 -0
@@ -0,0 +1,410 @@
1
+ # -----------------------------------------------------------------------------------
2
+ # <copyright company="Aspose Pty Ltd" file="template.rb">
3
+ # Copyright (c) 2003-2019 Aspose Pty Ltd
4
+ # </copyright>
5
+ # <summary>
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be included in all
14
+ # copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # SOFTWARE.
23
+ # </summary>
24
+ # -----------------------------------------------------------------------------------
25
+
26
+ require 'uri'
27
+ require 'date'
28
+
29
+ module GroupDocsParserCloud
30
+ #
31
+ # GroupDocs.Parser Cloud API
32
+ #
33
+ class TemplateApi
34
+ attr_accessor :config
35
+
36
+ #make TemplateApi.new private
37
+ private_class_method :new
38
+
39
+ # Initializes new instance of TemplateApi
40
+ #
41
+ # @param [config] Configuration
42
+ # @return [TemplateApi] New instance of TemplateApi
43
+ def initialize(config)
44
+ @config = config
45
+ @api_client = ApiClient.new(config)
46
+ @access_token = nil
47
+ end
48
+
49
+ # Initializes new instance of TemplateApi
50
+ #
51
+ # @param [app_sid] Application identifier (App SID)
52
+ # @param [app_key] Application private key (App Key)
53
+ # @return [TemplateApi] New instance of TemplateApi
54
+ def self.from_keys(app_sid, app_key)
55
+ config = Configuration.new(app_sid, app_key)
56
+ return new(config)
57
+ end
58
+
59
+ # Initializes new instance of TemplateApi
60
+ #
61
+ # @param [config] Configuration
62
+ # @return [TemplateApi] New instance of TemplateApi
63
+ def self.from_config(config)
64
+ return new(config)
65
+ end
66
+
67
+ # Create or update document template.
68
+ #
69
+ # @param request create_template_request
70
+ # @return [TemplateResult]
71
+ def create_template(request)
72
+ data, _status_code, _headers = create_template_with_http_info(request)
73
+ data
74
+ end
75
+
76
+ # Create or update document template.
77
+ #
78
+ # @param request create_template_request
79
+ # @return [Array<(TemplateResult, Fixnum, Hash)>]
80
+ # TemplateResult data, response status code and response headers
81
+ def create_template_with_http_info(request)
82
+ raise ArgumentError, 'Incorrect request type' unless request.is_a? CreateTemplateRequest
83
+
84
+ @api_client.config.logger.debug 'Calling API: TemplateApi.create_template ...' if @api_client.config.debugging
85
+ # verify the required parameter 'options' is set
86
+ raise ArgumentError, 'Missing the required parameter options when calling TemplateApi.create_template' if @api_client.config.client_side_validation && request.options.nil?
87
+ # resource path
88
+ local_var_path = '/parser/template'
89
+
90
+ # query parameters
91
+ query_params = {}
92
+
93
+ # header parameters
94
+ header_params = {}
95
+ # HTTP header 'Accept' (if needed)
96
+ header_params['Accept'] = @api_client.select_header_accept(['application/json'])
97
+ # HTTP header 'Content-Type'
98
+ header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])
99
+
100
+ # form parameters
101
+ form_params = {}
102
+
103
+ # http body (model)
104
+ post_body = @api_client.object_to_http_body(request.options)
105
+ data, status_code, headers = @api_client.call_api(:PUT, local_var_path,
106
+ header_params: header_params,
107
+ query_params: query_params,
108
+ form_params: form_params,
109
+ body: post_body,
110
+ access_token: get_access_token,
111
+ return_type: 'TemplateResult')
112
+ if @api_client.config.debugging
113
+ @api_client.config.logger.debug "API called:
114
+ TemplateApi#create_template\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
115
+ end
116
+ [data, status_code, headers]
117
+ end
118
+
119
+ # Delete document template.
120
+ #
121
+ # @param request delete_template_request
122
+ # @return [nil]
123
+ def delete_template(request)
124
+ delete_template_with_http_info(request)
125
+ nil
126
+ end
127
+
128
+ # Delete document template.
129
+ #
130
+ # @param request delete_template_request
131
+ # @return [Array<(nil, Fixnum, Hash)>]
132
+ # nil, response status code and response headers
133
+ def delete_template_with_http_info(request)
134
+ raise ArgumentError, 'Incorrect request type' unless request.is_a? DeleteTemplateRequest
135
+
136
+ @api_client.config.logger.debug 'Calling API: TemplateApi.delete_template ...' if @api_client.config.debugging
137
+ # verify the required parameter 'options' is set
138
+ raise ArgumentError, 'Missing the required parameter options when calling TemplateApi.delete_template' if @api_client.config.client_side_validation && request.options.nil?
139
+ # resource path
140
+ local_var_path = '/parser/template'
141
+
142
+ # query parameters
143
+ query_params = {}
144
+
145
+ # header parameters
146
+ header_params = {}
147
+ # HTTP header 'Accept' (if needed)
148
+ header_params['Accept'] = @api_client.select_header_accept(['application/json'])
149
+ # HTTP header 'Content-Type'
150
+ header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])
151
+
152
+ # form parameters
153
+ form_params = {}
154
+
155
+ # http body (model)
156
+ post_body = @api_client.object_to_http_body(request.options)
157
+ data, status_code, headers = @api_client.call_api(:DELETE, local_var_path,
158
+ header_params: header_params,
159
+ query_params: query_params,
160
+ form_params: form_params,
161
+ body: post_body,
162
+ access_token: get_access_token)
163
+ if @api_client.config.debugging
164
+ @api_client.config.logger.debug "API called:
165
+ TemplateApi#delete_template\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
166
+ end
167
+ [data, status_code, headers]
168
+ end
169
+
170
+ # Retrieve document template.
171
+ #
172
+ # @param request get_template_request
173
+ # @return [Template]
174
+ def get_template(request)
175
+ data, _status_code, _headers = get_template_with_http_info(request)
176
+ data
177
+ end
178
+
179
+ # Retrieve document template.
180
+ #
181
+ # @param request get_template_request
182
+ # @return [Array<(Template, Fixnum, Hash)>]
183
+ # Template data, response status code and response headers
184
+ def get_template_with_http_info(request)
185
+ raise ArgumentError, 'Incorrect request type' unless request.is_a? GetTemplateRequest
186
+
187
+ @api_client.config.logger.debug 'Calling API: TemplateApi.get_template ...' if @api_client.config.debugging
188
+ # verify the required parameter 'options' is set
189
+ raise ArgumentError, 'Missing the required parameter options when calling TemplateApi.get_template' if @api_client.config.client_side_validation && request.options.nil?
190
+ # resource path
191
+ local_var_path = '/parser/template'
192
+
193
+ # query parameters
194
+ query_params = {}
195
+
196
+ # header parameters
197
+ header_params = {}
198
+ # HTTP header 'Accept' (if needed)
199
+ header_params['Accept'] = @api_client.select_header_accept(['application/json'])
200
+ # HTTP header 'Content-Type'
201
+ header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])
202
+
203
+ # form parameters
204
+ form_params = {}
205
+
206
+ # http body (model)
207
+ post_body = @api_client.object_to_http_body(request.options)
208
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path,
209
+ header_params: header_params,
210
+ query_params: query_params,
211
+ form_params: form_params,
212
+ body: post_body,
213
+ access_token: get_access_token,
214
+ return_type: 'Template')
215
+ if @api_client.config.debugging
216
+ @api_client.config.logger.debug "API called:
217
+ TemplateApi#get_template\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
218
+ end
219
+ [data, status_code, headers]
220
+ end
221
+
222
+ #
223
+ # Helper method to convert first letter to downcase
224
+ #
225
+ private def downcase_first_letter(str)
226
+ value = str[0].downcase + str[1..-1]
227
+ value
228
+ end
229
+
230
+ #
231
+ # Retrieves access token
232
+ #
233
+ private def get_access_token
234
+ if @access_token.nil? then
235
+ request_access_token
236
+ end
237
+
238
+ @access_token
239
+ end
240
+
241
+ #
242
+ # Gets a access token from server
243
+ #
244
+ private def request_access_token
245
+ auth_config = Configuration.new(@config.app_sid, @config.app_key)
246
+ auth_config.api_base_url = @config.api_base_url
247
+ auth_config.debugging = @config.debugging
248
+ auth_config.logger = @config.logger
249
+ auth_config.temp_folder_path = @config.temp_folder_path
250
+ auth_config.client_side_validation = @config.client_side_validation
251
+ auth_config.api_version = ''
252
+
253
+ auth_api_client = ApiClient.new(auth_config)
254
+
255
+ request_url = "/connect/token"
256
+ post_data = "grant_type=client_credentials&client_id=#{@config.app_sid}&client_secret=#{@config.app_key}"
257
+
258
+ data, _status_code, _header = auth_api_client.call_api(:POST, request_url, :body => post_data, :return_type => 'Object')
259
+
260
+ @access_token = data[:access_token]
261
+
262
+ expires_in_seconds = data[:expires_in].to_i - 5 * 60
263
+ expires_in_days = Rational(expires_in_seconds, 60 * 60 * 24)
264
+ @access_token_expires_at = DateTime.now + expires_in_days
265
+ end
266
+
267
+ # requires all files inside a directory from current dir
268
+ # @param _dir can be relative path like '/lib' or "../lib"
269
+ private def require_all(_dir)
270
+ Dir[File.expand_path(File.join(File.dirname(File.absolute_path(__FILE__)), _dir)) + "/*.rb"].each do |file|
271
+ require file
272
+ end
273
+ end
274
+ end
275
+ end
276
+ #
277
+ # --------------------------------------------------------------------------------------------------------------------
278
+ # <copyright company="Aspose Pty Ltd" file="create_template_request.rb">
279
+ # Copyright (c) 2003-2019 Aspose Pty Ltd
280
+ # </copyright>
281
+ # <summary>
282
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
283
+ # of this software and associated documentation files (the "Software"), to deal
284
+ # in the Software without restriction, including without limitation the rights
285
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
286
+ # copies of the Software, and to permit persons to whom the Software is
287
+ # furnished to do so, subject to the following conditions:
288
+ #
289
+ # The above copyright notice and this permission notice shall be included in all
290
+ # copies or substantial portions of the Software.
291
+ #
292
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
293
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
294
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
295
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
296
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
297
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
298
+ # SOFTWARE.
299
+ # </summary>
300
+ # --------------------------------------------------------------------------------------------------------------------
301
+ #
302
+
303
+ module GroupDocsParserCloud
304
+
305
+ #
306
+ # Request model for create_template operation.
307
+ #
308
+ class CreateTemplateRequest
309
+
310
+ # Create template options.
311
+ attr_accessor :options
312
+
313
+ #
314
+ # Initializes a new instance.
315
+ # @param options Create template options.
316
+ def initialize(options)
317
+ self.options = options
318
+ end
319
+ end
320
+ end
321
+ #
322
+ # --------------------------------------------------------------------------------------------------------------------
323
+ # <copyright company="Aspose Pty Ltd" file="delete_template_request.rb">
324
+ # Copyright (c) 2003-2019 Aspose Pty Ltd
325
+ # </copyright>
326
+ # <summary>
327
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
328
+ # of this software and associated documentation files (the "Software"), to deal
329
+ # in the Software without restriction, including without limitation the rights
330
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
331
+ # copies of the Software, and to permit persons to whom the Software is
332
+ # furnished to do so, subject to the following conditions:
333
+ #
334
+ # The above copyright notice and this permission notice shall be included in all
335
+ # copies or substantial portions of the Software.
336
+ #
337
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
338
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
339
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
340
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
341
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
342
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
343
+ # SOFTWARE.
344
+ # </summary>
345
+ # --------------------------------------------------------------------------------------------------------------------
346
+ #
347
+
348
+ module GroupDocsParserCloud
349
+
350
+ #
351
+ # Request model for delete_template operation.
352
+ #
353
+ class DeleteTemplateRequest
354
+
355
+ # Delete template options.
356
+ attr_accessor :options
357
+
358
+ #
359
+ # Initializes a new instance.
360
+ # @param options Delete template options.
361
+ def initialize(options)
362
+ self.options = options
363
+ end
364
+ end
365
+ end
366
+ #
367
+ # --------------------------------------------------------------------------------------------------------------------
368
+ # <copyright company="Aspose Pty Ltd" file="get_template_request.rb">
369
+ # Copyright (c) 2003-2019 Aspose Pty Ltd
370
+ # </copyright>
371
+ # <summary>
372
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
373
+ # of this software and associated documentation files (the "Software"), to deal
374
+ # in the Software without restriction, including without limitation the rights
375
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
376
+ # copies of the Software, and to permit persons to whom the Software is
377
+ # furnished to do so, subject to the following conditions:
378
+ #
379
+ # The above copyright notice and this permission notice shall be included in all
380
+ # copies or substantial portions of the Software.
381
+ #
382
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
383
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
384
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
385
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
386
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
387
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
388
+ # SOFTWARE.
389
+ # </summary>
390
+ # --------------------------------------------------------------------------------------------------------------------
391
+ #
392
+
393
+ module GroupDocsParserCloud
394
+
395
+ #
396
+ # Request model for get_template operation.
397
+ #
398
+ class GetTemplateRequest
399
+
400
+ # Retrieve template options.
401
+ attr_accessor :options
402
+
403
+ #
404
+ # Initializes a new instance.
405
+ # @param options Retrieve template options.
406
+ def initialize(options)
407
+ self.options = options
408
+ end
409
+ end
410
+ end
@@ -0,0 +1,380 @@
1
+ # -----------------------------------------------------------------------------------
2
+ # <copyright company="Aspose Pty Ltd" file="api_client.rb">
3
+ # Copyright (c) 2003-2019 Aspose Pty Ltd
4
+ # </copyright>
5
+ # <summary>
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be included in all
14
+ # copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # SOFTWARE.
23
+ # </summary>
24
+ # -----------------------------------------------------------------------------------
25
+
26
+ require 'date'
27
+ require 'json'
28
+ require 'logger'
29
+ require 'tempfile'
30
+ require 'faraday'
31
+ require 'mimemagic'
32
+ require 'addressable'
33
+ require_relative 'version'
34
+ require_relative 'api_error'
35
+
36
+ module GroupDocsParserCloud
37
+ #
38
+ # api client is mainly responsible for making the HTTP call to the API backend.
39
+ #
40
+ class ApiClient
41
+ # The Configuration object holding settings to be used in the API client.
42
+ attr_accessor :config
43
+
44
+ # Defines the headers to be used in HTTP requests of all API calls by default.
45
+ #
46
+ # @return [Hash]
47
+ attr_accessor :default_headers
48
+
49
+ # Initializes the ApiClient
50
+ # @option config [Configuration] Configuration for initializing the object
51
+ def initialize(config)
52
+ @config = config
53
+ @default_headers = {
54
+ 'Content-Type' => config.api_version == '' ? "application/x-www-form-urlencoded" : "application/json",
55
+ 'x-groupdocs-client' => "ruby sdk",
56
+ 'x-groupdocs-version' => GroupDocsParserCloud::VERSION.to_s
57
+ }
58
+ end
59
+
60
+ # Call an API with given options.
61
+ #
62
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
63
+ # the data deserialized from response body (could be nil), response status code and response headers.
64
+ def call_api(http_method, path, opts = {})
65
+ response = build_request(http_method, path, opts)
66
+ download_file response if opts[:return_type] == 'File'
67
+ if @config.debugging
68
+ @config.logger.debug "'HTTP' response body '~BEGIN~'\n #{response.body}\n'~END~'\n"
69
+ end
70
+
71
+ unless response.success?
72
+ raise ApiError.new(:code => response.status, :response_body => response.body)
73
+ end
74
+
75
+ data = deserialize(response, opts[:return_type]) if opts[:return_type]
76
+ [data, response.status, response.headers]
77
+ end
78
+
79
+ # Builds the HTTP request
80
+ #
81
+ # @param [String] http_method HTTP method/verb (e.g. POST)
82
+ # @param [String] path URL path (e.g. /account/new)
83
+ # @option opts [Hash] :header_params Header parameters
84
+ # @option opts [Hash] :query_params Query parameters
85
+ # @option opts [Hash] :form_params Query parameters
86
+ # @option opts [Object] :body HTTP body (JSON/XML)
87
+ # @return [Faraday::Response] A Faraday Response
88
+ def build_request(http_method, path, opts = {})
89
+ url = build_request_url(path)
90
+ http_method = http_method.to_sym.downcase
91
+
92
+ header_params = @default_headers.merge(opts[:header_params] || {})
93
+ query_params = opts[:query_params] || {}
94
+ form_params = opts[:form_params] || {}
95
+ body = opts[:body] if opts[:body] || nil?
96
+
97
+ update_params_for_oauth! header_params, opts[:access_token]
98
+
99
+ req_opts = {
100
+ :method => http_method,
101
+ :headers => header_params,
102
+ :params => query_params,
103
+ :body => body
104
+ }
105
+
106
+ if [:post, :patch, :put, :delete].include?(http_method)
107
+ req_body = build_request_body(header_params, form_params, opts[:body])
108
+ req_opts.update :body => req_body
109
+ if @config.debugging
110
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
111
+ end
112
+ end
113
+
114
+ conn = Faraday.new url, { :params => query_params, :headers => header_params } do |f|
115
+ f.request :multipart
116
+ f.request :url_encoded
117
+ f.adapter Faraday.default_adapter
118
+ end
119
+
120
+ case http_method
121
+ when :post
122
+ return conn.post url, req_opts[:body]
123
+ when :put
124
+ return conn.put url, req_opts[:body]
125
+ when :get
126
+ return conn.get url, req_opts[:body]
127
+ else
128
+ return conn.delete url do |c|
129
+ c.body = req_opts[:body]
130
+ end
131
+ end
132
+ end
133
+
134
+ # Check if the given MIME is a JSON MIME.
135
+ # JSON MIME examples:
136
+ # application/json
137
+ # application/json; charset=UTF8
138
+ # APPLICATION/JSON
139
+ # */*
140
+ # @param [String] mime MIME
141
+ # @return [Boolean] True if the MIME is application/json
142
+ def json_mime?(mime)
143
+ (mime == "*/*") || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
144
+ end
145
+
146
+ # Deserialize the response to the given return type.
147
+ #
148
+ # @param [Response] response HTTP response
149
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
150
+ def deserialize(response, return_type)
151
+ body = response.body
152
+
153
+ # handle file downloading - return the File instance processed in request callbacks
154
+ # note that response body is empty when the file is written in chunks in request on_body callback
155
+ return @tempfile if return_type == 'File'
156
+
157
+ return nil if body.nil? || body.empty?
158
+
159
+ # return response body directly for String return type
160
+ return body if return_type == 'String'
161
+
162
+ # ensuring a default content type
163
+ content_type = response.headers['Content-Type'] || 'application/json'
164
+
165
+ raise "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
166
+
167
+ begin
168
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
169
+ rescue JSON::ParserError => e
170
+ if %w[String Date DateTime].include?(return_type)
171
+ data = body
172
+ else
173
+ raise e
174
+ end
175
+ end
176
+
177
+ convert_to_type data, return_type
178
+ end
179
+
180
+ # Convert data to the given return type.
181
+ # @param [Object] data Data to be converted
182
+ # @param [String] return_type Return type
183
+ # @return [Mixed] Data in a particular type
184
+ def convert_to_type(data, return_type)
185
+ return nil if data.nil?
186
+ case return_type
187
+ when 'String'
188
+ data.to_s
189
+ when 'Integer'
190
+ data.to_i
191
+ when 'Float'
192
+ data.to_f
193
+ when 'BOOLEAN'
194
+ data == true
195
+ when 'DateTime'
196
+ # parse date time (expecting ISO 8601 format)
197
+ DateTime.parse data
198
+ when 'Date'
199
+ # parse date time (expecting ISO 8601 format)
200
+ Date.parse data
201
+ when 'Object'
202
+ # generic object (usually a Hash), return directly
203
+ data
204
+ when /\AArray<(.+)>\z/
205
+ # e.g. Array<Pet>
206
+ sub_type = $1
207
+ data.map {|item| convert_to_type(item, sub_type) }
208
+ when /\AHash\<String, (.+)\>\z/
209
+ # e.g. Hash<String, Integer>
210
+ sub_type = $1
211
+ {}.tap do |hash|
212
+ data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
213
+ end
214
+ else
215
+ # models, e.g. Pet
216
+ GroupDocsParserCloud.const_get(return_type).new.tap do |model|
217
+ model.build_from_hash data
218
+ end
219
+ end
220
+ end
221
+
222
+ # Save response body into a file in (the defined) temporary folder, using the filename
223
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
224
+ # The response body is written to the file in chunks in order to handle files which
225
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
226
+ # process can use.
227
+ #
228
+ # @see Configuration#temp_folder_path
229
+ def download_file(response)
230
+ tempfile = nil
231
+ encoding = nil
232
+ content_disposition = response.headers['Content-Disposition']
233
+ if content_disposition and content_disposition =~ /filename=/i
234
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
235
+ prefix = sanitize_filename(filename)
236
+ else
237
+ prefix = 'download-'
238
+ end
239
+ prefix += '-' unless prefix.end_with?('-')
240
+ encoding = response.body.encoding
241
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
242
+ @tempfile = tempfile
243
+ tempfile.write(response.body)
244
+ response.on_complete do |resp|
245
+ tempfile.close
246
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
247
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
248
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
249
+ "explicitly with `tempfile.delete`"
250
+ end
251
+ end
252
+
253
+ # Sanitize filename by removing path.
254
+ # e.g. ../../sun.gif becomes sun.gif
255
+ #
256
+ # @param [String] filename the filename to be sanitized
257
+ # @return [String] the sanitized filename
258
+ def sanitize_filename(filename)
259
+ filename.gsub(/.*[\/\\]/, '')
260
+ end
261
+
262
+ def build_request_url(path)
263
+ # Add leading and trailing slashes to path
264
+ path = "/#{path}".gsub(/\/+/, '/')
265
+ Addressable::URI.encode(@config.api_base_url + @config.api_version + path)
266
+ end
267
+
268
+ # Builds the HTTP request body
269
+ #
270
+ # @param [Hash] header_params Header parameters
271
+ # @param [Hash] form_params Query parameters
272
+ # @param [Object] body HTTP body (JSON/XML)
273
+ # @return [String] HTTP body data in the form of string
274
+ def build_request_body(header_params, form_params, body)
275
+ # http form
276
+ if body
277
+ data = body.is_a?(String) ? body : body.to_json
278
+ elsif header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
279
+ header_params['Content-Type'] == 'multipart/form-data'
280
+ data = {}
281
+ form_params.each do |key, value|
282
+ case value
283
+ when ::File
284
+ data[key] = Faraday::UploadIO.new(value.path, MimeMagic.by_magic(value).to_s, key)
285
+ when ::Array, nil
286
+ data[key] = value
287
+ else
288
+ data[key] = value.to_s
289
+ end
290
+ end
291
+ else
292
+ data = nil
293
+ end
294
+ data
295
+ end
296
+
297
+ # Update hearder and query params based on authentication settings.
298
+ #
299
+ # @param [Hash] header_params Header parameters
300
+ # @param [String] access_token OAuth access token
301
+ def update_params_for_oauth!(header_params, access_token)
302
+ header_params["Authorization"] = "Bearer #{access_token}"
303
+ end
304
+
305
+ # Sets user agent in HTTP header
306
+ #
307
+ # @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
308
+ def user_agent=(user_agent)
309
+ @user_agent = user_agent
310
+ @default_headers['User-Agent'] = @user_agent
311
+ end
312
+
313
+ # Return Accept header based on an array of accepts provided.
314
+ # @param [Array] accepts array for Accept
315
+ # @return [String] the Accept header (e.g. application/json)
316
+ def select_header_accept(accepts)
317
+ return nil if accepts.nil? || accepts.empty?
318
+ # use JSON when present, otherwise use all of the provided
319
+ json_accept = accepts.find { |s| json_mime?(s) }
320
+ return json_accept || accepts.join(',')
321
+ end
322
+
323
+ # Return Content-Type header based on an array of content types provided.
324
+ # @param [Array] content_types array for Content-Type
325
+ # @return [String] the Content-Type header (e.g. application/json)
326
+ def select_header_content_type(content_types)
327
+ # use application/json by default
328
+ return 'application/json' if content_types.nil? || content_types.empty?
329
+ # use JSON when present, otherwise use the first one
330
+ json_content_type = content_types.find { |s| json_mime?(s) }
331
+ return json_content_type || content_types.first
332
+ end
333
+
334
+ # Convert object (array, hash, object, etc) to JSON string.
335
+ # @param [Object] model object to be converted into JSON string
336
+ # @return [String] JSON string representation of the object
337
+ def object_to_http_body(model)
338
+ return '"' + model + '"' if model.is_a?(String)
339
+ return model if model.nil?
340
+ local_body = nil
341
+ if model.is_a?(Array)
342
+ local_body = model.map { |m| object_to_hash(m) }
343
+ else
344
+ local_body = object_to_hash(model)
345
+ end
346
+ local_body.to_json
347
+ end
348
+
349
+ # Convert object(non-array) to hash.
350
+ # @param [Object] obj object to be converted into JSON string
351
+ # @return [String] JSON string representation of the object
352
+ def object_to_hash(obj)
353
+ if obj.respond_to?(:to_hash)
354
+ obj.to_hash
355
+ else
356
+ obj
357
+ end
358
+ end
359
+
360
+ # Build parameter value according to the given collection format.
361
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
362
+ def build_collection_param(param, collection_format)
363
+ case collection_format
364
+ when :csv
365
+ param.join(',')
366
+ when :ssv
367
+ param.join(' ')
368
+ when :tsv
369
+ param.join("\t")
370
+ when :pipes
371
+ param.join('|')
372
+ when :multi
373
+ # return the array directly as faraday will handle it as expected
374
+ param
375
+ else
376
+ raise "unknown collection format: #{collection_format.inspect}"
377
+ end
378
+ end
379
+ end
380
+ end