groupdocs_storage_cloud 18.6.0

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