groupdocs_assembly_cloud 20.6.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 (34) hide show
  1. checksums.yaml +7 -0
  2. data/lib/groupdocs_assembly_cloud.rb +68 -0
  3. data/lib/groupdocs_assembly_cloud/api/assembly_api.rb +1074 -0
  4. data/lib/groupdocs_assembly_cloud/api_client.rb +405 -0
  5. data/lib/groupdocs_assembly_cloud/api_error.rb +54 -0
  6. data/lib/groupdocs_assembly_cloud/configuration.rb +182 -0
  7. data/lib/groupdocs_assembly_cloud/models/assemble_options.rb +237 -0
  8. data/lib/groupdocs_assembly_cloud/models/assembly_api_error_response.rb +217 -0
  9. data/lib/groupdocs_assembly_cloud/models/assembly_response.rb +207 -0
  10. data/lib/groupdocs_assembly_cloud/models/error.rb +237 -0
  11. data/lib/groupdocs_assembly_cloud/models/error_details.rb +222 -0
  12. data/lib/groupdocs_assembly_cloud/models/file_formats_response.rb +237 -0
  13. data/lib/groupdocs_assembly_cloud/models/file_response.rb +197 -0
  14. data/lib/groupdocs_assembly_cloud/models/files_list.rb +209 -0
  15. data/lib/groupdocs_assembly_cloud/models/files_upload_result.rb +221 -0
  16. data/lib/groupdocs_assembly_cloud/models/format.rb +217 -0
  17. data/lib/groupdocs_assembly_cloud/models/format_collection.rb +209 -0
  18. data/lib/groupdocs_assembly_cloud/models/requests/AssembleDocumentRequest.rb +45 -0
  19. data/lib/groupdocs_assembly_cloud/models/requests/CopyFileRequest.rb +61 -0
  20. data/lib/groupdocs_assembly_cloud/models/requests/CopyFolderRequest.rb +57 -0
  21. data/lib/groupdocs_assembly_cloud/models/requests/CreateFolderRequest.rb +49 -0
  22. data/lib/groupdocs_assembly_cloud/models/requests/DeleteFileRequest.rb +53 -0
  23. data/lib/groupdocs_assembly_cloud/models/requests/DeleteFolderRequest.rb +53 -0
  24. data/lib/groupdocs_assembly_cloud/models/requests/DownloadFileRequest.rb +53 -0
  25. data/lib/groupdocs_assembly_cloud/models/requests/GetFilesListRequest.rb +49 -0
  26. data/lib/groupdocs_assembly_cloud/models/requests/GetSupportedFileFormatsRequest.rb +36 -0
  27. data/lib/groupdocs_assembly_cloud/models/requests/MoveFileRequest.rb +61 -0
  28. data/lib/groupdocs_assembly_cloud/models/requests/MoveFolderRequest.rb +57 -0
  29. data/lib/groupdocs_assembly_cloud/models/requests/UploadFileRequest.rb +53 -0
  30. data/lib/groupdocs_assembly_cloud/models/response_error.rb +247 -0
  31. data/lib/groupdocs_assembly_cloud/models/storage_file.rb +257 -0
  32. data/lib/groupdocs_assembly_cloud/models/template_file_info.rb +237 -0
  33. data/lib/groupdocs_assembly_cloud/version.rb +29 -0
  34. metadata +124 -0
@@ -0,0 +1,405 @@
1
+ # -----------------------------------------------------------------------------------
2
+ # <copyright company="Aspose" file="api_client.rb">
3
+ # Copyright (c) 2020 GroupDocs.Assembly for Cloud
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 'uri'
31
+ require 'faraday'
32
+ require 'mimemagic'
33
+ require_relative 'version'
34
+ require_relative 'api_error'
35
+
36
+ module GroupDocsAssemblyCloud
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, default to Configuration.default
51
+ def initialize(config = Configuration.default)
52
+ @config = config
53
+ @default_headers = {
54
+ 'Content-Type' => "application/json",
55
+ 'x-aspose-client' => "ruby sdk",
56
+ 'x-aspose-version' => GroupDocsAssemblyCloud::VERSION.to_s
57
+ }
58
+ end
59
+
60
+ def self.default
61
+ @@default ||= ApiClient.new
62
+ end
63
+
64
+ # Call an API with given options.
65
+ #
66
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
67
+ # the data deserialized from response body (could be nil), response status code and response headers.
68
+ def call_api(http_method, path, opts = {})
69
+ response = build_request(http_method, path, opts)
70
+ download_file response if opts[:return_type] == 'File'
71
+ if @config.debugging
72
+ @config.logger.debug "'HTTP' response body '~BEGIN~'\n #{response.body}\n'~END~'\n"
73
+ end
74
+
75
+ unless response.success?
76
+ if response.status == 0
77
+ # Errors from libcurl will be made visible here
78
+ raise ApiError.new(:code => 0,
79
+ :message => response.reason_phrase)
80
+ else
81
+ raise ApiError.new(:code => response.status,
82
+ :response_headers => response.headers,
83
+ :response_body => response.body),
84
+ response.reason_phrase
85
+ end
86
+ end
87
+
88
+
89
+ data = deserialize(response, opts[:return_type]) if opts[:return_type]
90
+ [data, response.status, response.headers]
91
+ end
92
+
93
+ # Builds the HTTP request
94
+ #
95
+ # @param [String] http_method HTTP method/verb (e.g. POST)
96
+ # @param [String] path URL path (e.g. /account/new)
97
+ # @option opts [Hash] :header_params Header parameters
98
+ # @option opts [Hash] :query_params Query parameters
99
+ # @option opts [Hash] :form_params Query parameters
100
+ # @option opts [Object] :body HTTP body (JSON/XML)
101
+ # @return [Faraday::Response] A Faraday Response
102
+ def build_request(http_method, path, opts = {})
103
+ url = build_request_url(path)
104
+ http_method = http_method.to_sym.downcase
105
+
106
+ header_params = @default_headers.merge(opts[:header_params] || {})
107
+ query_params = opts[:query_params] || {}
108
+ form_params = opts[:form_params] || {}
109
+ body = opts[:body] if opts[:body] || nil?
110
+
111
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
112
+
113
+ req_opts = {
114
+ :method => http_method,
115
+ :headers => header_params,
116
+ :params => query_params,
117
+ :body => body
118
+ }
119
+
120
+ if [:post, :patch, :put, :delete].include?(http_method)
121
+ req_body = build_request_body(header_params, form_params, opts[:body])
122
+ req_opts.update :body => req_body
123
+ if @config.debugging
124
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
125
+ end
126
+ end
127
+
128
+ conn = Faraday.new url, { :params => query_params, :headers => header_params } do |f|
129
+ f.request :multipart
130
+ f.request :url_encoded
131
+ f.adapter Faraday.default_adapter
132
+ end
133
+
134
+ case http_method
135
+ when :post
136
+ return conn.post url, req_opts[:body]
137
+ when :put
138
+ return conn.put url, req_opts[:body]
139
+ when :get
140
+ return conn.get url, req_opts[:body]
141
+ else
142
+ return conn.delete url do |c|
143
+ c.body = req_opts[:body]
144
+ end
145
+ end
146
+ end
147
+
148
+ # Check if the given MIME is a JSON MIME.
149
+ # JSON MIME examples:
150
+ # application/json
151
+ # application/json; charset=UTF8
152
+ # APPLICATION/JSON
153
+ # */*
154
+ # @param [String] mime MIME
155
+ # @return [Boolean] True if the MIME is application/json
156
+ def json_mime?(mime)
157
+ (mime == "*/*") || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
158
+ end
159
+
160
+ # Deserialize the response to the given return type.
161
+ #
162
+ # @param [Response] response HTTP response
163
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
164
+ def deserialize(response, return_type)
165
+ body = response.body
166
+
167
+ # handle file downloading - return the File instance processed in request callbacks
168
+ # note that response body is empty when the file is written in chunks in request on_body callback
169
+ return @tempfile if return_type == 'File'
170
+
171
+ return nil if body.nil? || body.empty?
172
+
173
+ # return response body directly for String return type
174
+ return body if return_type == 'String'
175
+
176
+ # ensuring a default content type
177
+ content_type = response.headers['Content-Type'] || 'application/json'
178
+
179
+ raise "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
180
+
181
+ begin
182
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
183
+ rescue JSON::ParserError => e
184
+ if %w[String Date DateTime].include?(return_type)
185
+ data = body
186
+ else
187
+ raise e
188
+ end
189
+ end
190
+
191
+ convert_to_type data, return_type
192
+ end
193
+
194
+ # Convert data to the given return type.
195
+ # @param [Object] data Data to be converted
196
+ # @param [String] return_type Return type
197
+ # @return [Mixed] Data in a particular type
198
+ def convert_to_type(data, return_type)
199
+ return nil if data.nil?
200
+ case return_type
201
+ when 'String'
202
+ data.to_s
203
+ when 'Integer'
204
+ data.to_i
205
+ when 'Float'
206
+ data.to_f
207
+ when 'BOOLEAN'
208
+ data == true
209
+ when 'DateTime'
210
+ # parse date time (expecting ISO 8601 format)
211
+ DateTime.parse data
212
+ when 'Date'
213
+ # parse date time (expecting ISO 8601 format)
214
+ Date.parse data
215
+ when 'Object'
216
+ # generic object (usually a Hash), return directly
217
+ data
218
+ when /\AArray<(.+)>\z/
219
+ # e.g. Array<Pet>
220
+ sub_type = $1
221
+ data.map {|item| convert_to_type(item, sub_type) }
222
+ when /\AHash\<String, (.+)\>\z/
223
+ # e.g. Hash<String, Integer>
224
+ sub_type = $1
225
+ {}.tap do |hash|
226
+ data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
227
+ end
228
+ else
229
+ # models, e.g. Pet
230
+ GroupDocsAssemblyCloud.const_get(return_type).new.tap do |model|
231
+ model.build_from_hash data
232
+ end
233
+ end
234
+ end
235
+
236
+ # Save response body into a file in (the defined) temporary folder, using the filename
237
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
238
+ # The response body is written to the file in chunks in order to handle files which
239
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
240
+ # process can use.
241
+ #
242
+ # @see Configuration#temp_folder_path
243
+ def download_file(response)
244
+ tempfile = nil
245
+ encoding = nil
246
+ content_disposition = response.headers['Content-Disposition']
247
+ if content_disposition and content_disposition =~ /filename=/i
248
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
249
+ prefix = sanitize_filename(filename)
250
+ else
251
+ prefix = 'download-'
252
+ end
253
+ prefix += '-' unless prefix.end_with?('-')
254
+ encoding = response.body.encoding
255
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
256
+ @tempfile = tempfile
257
+ tempfile.write(response.body)
258
+ response.on_complete do |resp|
259
+ tempfile.close
260
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
261
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
262
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
263
+ "explicitly with `tempfile.delete`"
264
+ end
265
+ end
266
+
267
+ # Sanitize filename by removing path.
268
+ # e.g. ../../sun.gif becomes sun.gif
269
+ #
270
+ # @param [String] filename the filename to be sanitized
271
+ # @return [String] the sanitized filename
272
+ def sanitize_filename(filename)
273
+ filename.gsub(/.*[\/\\]/, '')
274
+ end
275
+
276
+ def build_request_url(path)
277
+ # Add leading and trailing slashes to path
278
+ path = "/#{path}".gsub(/\/+/, '/')
279
+ url = URI.encode(@config.base_url + path)
280
+ url = url.gsub(/v[0-9.]+\//, '') if url.include? 'connect/token'
281
+ url
282
+ end
283
+
284
+ # Builds the HTTP request body
285
+ #
286
+ # @param [Hash] header_params Header parameters
287
+ # @param [Hash] form_params Query parameters
288
+ # @param [Object] body HTTP body (JSON/XML)
289
+ # @return [String] HTTP body data in the form of string
290
+ def build_request_body(header_params, form_params, body)
291
+ # http form
292
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
293
+ header_params['Content-Type'] == 'multipart/form-data'
294
+ data = {}
295
+ form_params.each do |key, value|
296
+ case value
297
+ when ::File
298
+ data[key] = Faraday::UploadIO.new(value.path, MimeMagic.by_magic(value).to_s, key)
299
+ when ::Array, nil
300
+ data[key] = value
301
+ else
302
+ data[key] = value.to_s
303
+ end
304
+ end
305
+ elsif body
306
+ data = body.is_a?(String) ? body : body.to_json
307
+ else
308
+ data = nil
309
+ end
310
+ data
311
+ end
312
+
313
+ # Update hearder and query params based on authentication settings.
314
+ #
315
+ # @param [Hash] header_params Header parameters
316
+ # @param [Hash] query_params Query parameters
317
+ # @param [String] auth_names Authentication scheme name
318
+ def update_params_for_auth!(header_params, query_params, auth_names)
319
+ Array(auth_names).each do |auth_name|
320
+ auth_setting = @config.auth_settings[auth_name]
321
+ next unless auth_setting
322
+ case auth_setting[:in]
323
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
324
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
325
+ else raise ArgumentError, 'Authentication token must be in `query` of `header`'
326
+ end
327
+ end
328
+ end
329
+
330
+ # Sets user agent in HTTP header
331
+ #
332
+ # @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
333
+ def user_agent=(user_agent)
334
+ @user_agent = user_agent
335
+ @default_headers['User-Agent'] = @user_agent
336
+ end
337
+
338
+ # Return Accept header based on an array of accepts provided.
339
+ # @param [Array] accepts array for Accept
340
+ # @return [String] the Accept header (e.g. application/json)
341
+ def select_header_accept(accepts)
342
+ return nil if accepts.nil? || accepts.empty?
343
+ # use JSON when present, otherwise use all of the provided
344
+ json_accept = accepts.find { |s| json_mime?(s) }
345
+ return json_accept || accepts.join(',')
346
+ end
347
+
348
+ # Return Content-Type header based on an array of content types provided.
349
+ # @param [Array] content_types array for Content-Type
350
+ # @return [String] the Content-Type header (e.g. application/json)
351
+ def select_header_content_type(content_types)
352
+ # use application/json by default
353
+ return 'application/json' if content_types.nil? || content_types.empty?
354
+ # use JSON when present, otherwise use the first one
355
+ json_content_type = content_types.find { |s| json_mime?(s) }
356
+ return json_content_type || content_types.first
357
+ end
358
+
359
+ # Convert object (array, hash, object, etc) to JSON string.
360
+ # @param [Object] model object to be converted into JSON string
361
+ # @return [String] JSON string representation of the object
362
+ def object_to_http_body(model)
363
+ return '"' + model + '"' if model.is_a?(String)
364
+ return model if model.nil?
365
+ local_body = nil
366
+ if model.is_a?(Array)
367
+ local_body = model.map { |m| object_to_hash(m) }
368
+ else
369
+ local_body = object_to_hash(model)
370
+ end
371
+ local_body.to_json
372
+ end
373
+
374
+ # Convert object(non-array) to hash.
375
+ # @param [Object] obj object to be converted into JSON string
376
+ # @return [String] JSON string representation of the object
377
+ def object_to_hash(obj)
378
+ if obj.respond_to?(:to_hash)
379
+ obj.to_hash
380
+ else
381
+ obj
382
+ end
383
+ end
384
+
385
+ # Build parameter value according to the given collection format.
386
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
387
+ def build_collection_param(param, collection_format)
388
+ case collection_format
389
+ when :csv
390
+ param.join(',')
391
+ when :ssv
392
+ param.join(' ')
393
+ when :tsv
394
+ param.join("\t")
395
+ when :pipes
396
+ param.join('|')
397
+ when :multi
398
+ # return the array directly as faraday will handle it as expected
399
+ param
400
+ else
401
+ raise "unknown collection format: #{collection_format.inspect}"
402
+ end
403
+ end
404
+ end
405
+ end
@@ -0,0 +1,54 @@
1
+ # -----------------------------------------------------------------------------------
2
+ # <copyright company="Aspose" file="api_error.rb">
3
+ # Copyright (c) 2020 GroupDocs.Assembly for Cloud
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
+ module GroupDocsAssemblyCloud
27
+ #
28
+ # ApiError class for error handling
29
+ #
30
+ class ApiError < StandardError
31
+ attr_reader :code, :response_headers, :response_body
32
+
33
+ # Usage examples:
34
+ # ApiError.new
35
+ # ApiError.new("message")
36
+ # ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
37
+ # ApiError.new(:code => 404, :message => "Not Found")
38
+ def initialize(arg = nil)
39
+ if arg.is_a? Hash
40
+ if arg.key?(:message) || arg.key?('message')
41
+ super(arg[:message] || arg['message'])
42
+ else
43
+ super arg
44
+ end
45
+
46
+ arg.each do |k, v|
47
+ instance_variable_set "@#{k}", v
48
+ end
49
+ else
50
+ super arg
51
+ end
52
+ end
53
+ end
54
+ end