aspose_storage_cloud 1.0.1 → 18.6

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