aspose_imaging_cloud 1.0.0 → 19.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +224 -61
  3. data/lib/aspose-imaging-cloud.rb +161 -0
  4. data/lib/aspose-imaging-cloud/api/imaging_api.rb +684 -0
  5. data/lib/aspose-imaging-cloud/api_client.rb +357 -0
  6. data/lib/aspose-imaging-cloud/api_error.rb +42 -0
  7. data/lib/{aspose_imaging_cloud → aspose-imaging-cloud}/configuration.rb +236 -166
  8. data/lib/aspose-imaging-cloud/models/big_rectangular_filter_properties.rb +206 -0
  9. data/lib/aspose-imaging-cloud/models/bilateral_smoothing_filter_properties.rb +281 -0
  10. data/lib/aspose-imaging-cloud/models/bmp_properties.rb +202 -0
  11. data/lib/aspose-imaging-cloud/models/convolution_filter_properties.rb +236 -0
  12. data/lib/aspose-imaging-cloud/models/deconvolution_filter_properties.rb +266 -0
  13. data/lib/aspose-imaging-cloud/models/dicom_properties.rb +584 -0
  14. data/lib/aspose-imaging-cloud/models/disc_usage.rb +222 -0
  15. data/lib/aspose-imaging-cloud/models/djvu_properties.rb +232 -0
  16. data/lib/aspose-imaging-cloud/models/dng_properties.rb +329 -0
  17. data/lib/aspose-imaging-cloud/models/error.rb +232 -0
  18. data/lib/aspose-imaging-cloud/models/error_details.rb +217 -0
  19. data/lib/aspose-imaging-cloud/models/exif_data.rb +1560 -0
  20. data/lib/aspose-imaging-cloud/models/file_version.rb +277 -0
  21. data/lib/aspose-imaging-cloud/models/file_versions.rb +204 -0
  22. data/lib/aspose-imaging-cloud/models/files_list.rb +204 -0
  23. data/lib/aspose-imaging-cloud/models/files_upload_result.rb +216 -0
  24. data/lib/aspose-imaging-cloud/models/filter_properties_base.rb +206 -0
  25. data/lib/aspose-imaging-cloud/models/gauss_wiener_filter_properties.rb +296 -0
  26. data/lib/aspose-imaging-cloud/models/gaussian_blur_filter_properties.rb +266 -0
  27. data/lib/aspose-imaging-cloud/models/gif_properties.rb +247 -0
  28. data/lib/aspose-imaging-cloud/models/image_duplicates.rb +204 -0
  29. data/lib/aspose-imaging-cloud/models/image_duplicates_set.rb +204 -0
  30. data/lib/aspose-imaging-cloud/models/image_features.rb +257 -0
  31. data/lib/aspose-imaging-cloud/models/imaging_response.rb +412 -0
  32. data/lib/aspose-imaging-cloud/models/jfif_data.rb +247 -0
  33. data/lib/aspose-imaging-cloud/models/jpeg2000_properties.rb +214 -0
  34. data/lib/aspose-imaging-cloud/models/jpeg_exif_data.rb +1738 -0
  35. data/lib/aspose-imaging-cloud/models/jpeg_properties.rb +222 -0
  36. data/lib/aspose-imaging-cloud/models/median_filter_properties.rb +221 -0
  37. data/lib/aspose-imaging-cloud/models/motion_wiener_filter_properties.rb +311 -0
  38. data/lib/aspose-imaging-cloud/models/object_exist.rb +222 -0
  39. data/lib/aspose-imaging-cloud/models/odg_metadata.rb +372 -0
  40. data/lib/aspose-imaging-cloud/models/odg_page.rb +222 -0
  41. data/lib/aspose-imaging-cloud/models/odg_properties.rb +229 -0
  42. data/lib/aspose-imaging-cloud/models/png_properties.rb +192 -0
  43. data/lib/aspose-imaging-cloud/models/psd_properties.rb +242 -0
  44. data/lib/aspose-imaging-cloud/models/requests/add_search_image_request.rb +91 -0
  45. data/lib/aspose-imaging-cloud/models/requests/append_tiff_request.rb +88 -0
  46. data/lib/aspose-imaging-cloud/models/requests/compare_images_request.rb +94 -0
  47. data/lib/aspose-imaging-cloud/models/requests/convert_tiff_to_fax_request.rb +80 -0
  48. data/lib/aspose-imaging-cloud/models/requests/copy_file_request.rb +91 -0
  49. data/lib/aspose-imaging-cloud/models/requests/copy_folder_request.rb +88 -0
  50. data/lib/aspose-imaging-cloud/models/requests/create_cropped_image_request.rb +121 -0
  51. data/lib/aspose-imaging-cloud/models/requests/create_folder_request.rb +77 -0
  52. data/lib/aspose-imaging-cloud/models/requests/create_image_features_request.rb +89 -0
  53. data/lib/aspose-imaging-cloud/models/requests/create_image_frame_request.rb +112 -0
  54. data/lib/aspose-imaging-cloud/models/requests/create_image_search_request.rb +79 -0
  55. data/lib/aspose-imaging-cloud/models/requests/create_image_tag_request.rb +96 -0
  56. data/lib/aspose-imaging-cloud/models/requests/create_modified_bmp_request.rb +108 -0
  57. data/lib/aspose-imaging-cloud/models/requests/create_modified_emf_request.rb +127 -0
  58. data/lib/aspose-imaging-cloud/models/requests/create_modified_gif_request.rb +102 -0
  59. data/lib/aspose-imaging-cloud/models/requests/create_modified_jpeg2000_request.rb +95 -0
  60. data/lib/aspose-imaging-cloud/models/requests/create_modified_jpeg_request.rb +90 -0
  61. data/lib/aspose-imaging-cloud/models/requests/create_modified_psd_request.rb +90 -0
  62. data/lib/aspose-imaging-cloud/models/requests/create_modified_svg_request.rb +114 -0
  63. data/lib/aspose-imaging-cloud/models/requests/create_modified_tiff_request.rb +104 -0
  64. data/lib/aspose-imaging-cloud/models/requests/create_modified_web_p_request.rb +116 -0
  65. data/lib/aspose-imaging-cloud/models/requests/create_modified_wmf_request.rb +127 -0
  66. data/lib/aspose-imaging-cloud/models/requests/create_resized_image_request.rb +105 -0
  67. data/lib/aspose-imaging-cloud/models/requests/create_rotate_flipped_image_request.rb +97 -0
  68. data/lib/aspose-imaging-cloud/models/requests/create_saved_image_as_request.rb +89 -0
  69. data/lib/aspose-imaging-cloud/models/requests/create_updated_image_request.rb +145 -0
  70. data/lib/aspose-imaging-cloud/models/requests/create_web_site_image_features_request.rb +88 -0
  71. data/lib/aspose-imaging-cloud/models/requests/crop_image_request.rb +120 -0
  72. data/lib/aspose-imaging-cloud/models/requests/delete_file_request.rb +80 -0
  73. data/lib/aspose-imaging-cloud/models/requests/delete_folder_request.rb +80 -0
  74. data/lib/aspose-imaging-cloud/models/requests/delete_image_features_request.rb +88 -0
  75. data/lib/aspose-imaging-cloud/models/requests/delete_image_search_request.rb +80 -0
  76. data/lib/aspose-imaging-cloud/models/requests/delete_search_image_request.rb +88 -0
  77. data/lib/aspose-imaging-cloud/models/requests/download_file_request.rb +80 -0
  78. data/lib/aspose-imaging-cloud/models/requests/extract_image_features_request.rb +91 -0
  79. data/lib/aspose-imaging-cloud/models/requests/extract_image_frame_properties_request.rb +82 -0
  80. data/lib/aspose-imaging-cloud/models/requests/extract_image_properties_request.rb +75 -0
  81. data/lib/aspose-imaging-cloud/models/requests/filter_effect_image_request.rb +103 -0
  82. data/lib/aspose-imaging-cloud/models/requests/find_image_duplicates_request.rb +88 -0
  83. data/lib/aspose-imaging-cloud/models/requests/find_images_by_tags_request.rb +104 -0
  84. data/lib/aspose-imaging-cloud/models/requests/find_similar_images_request.rb +102 -0
  85. data/lib/aspose-imaging-cloud/models/requests/get_disc_usage_request.rb +70 -0
  86. data/lib/aspose-imaging-cloud/models/requests/get_file_versions_request.rb +77 -0
  87. data/lib/aspose-imaging-cloud/models/requests/get_files_list_request.rb +77 -0
  88. data/lib/aspose-imaging-cloud/models/requests/get_image_features_request.rb +88 -0
  89. data/lib/aspose-imaging-cloud/models/requests/get_image_frame_properties_request.rb +87 -0
  90. data/lib/aspose-imaging-cloud/models/requests/get_image_frame_request.rb +111 -0
  91. data/lib/aspose-imaging-cloud/models/requests/get_image_properties_request.rb +80 -0
  92. data/lib/aspose-imaging-cloud/models/requests/get_image_search_status_request.rb +80 -0
  93. data/lib/aspose-imaging-cloud/models/requests/get_search_image_request.rb +88 -0
  94. data/lib/aspose-imaging-cloud/models/requests/http_request.rb +50 -0
  95. data/lib/aspose-imaging-cloud/models/requests/imaging_request.rb +101 -0
  96. data/lib/aspose-imaging-cloud/models/requests/modify_bmp_request.rb +107 -0
  97. data/lib/aspose-imaging-cloud/models/requests/modify_emf_request.rb +126 -0
  98. data/lib/aspose-imaging-cloud/models/requests/modify_gif_request.rb +101 -0
  99. data/lib/aspose-imaging-cloud/models/requests/modify_jpeg2000_request.rb +94 -0
  100. data/lib/aspose-imaging-cloud/models/requests/modify_jpeg_request.rb +89 -0
  101. data/lib/aspose-imaging-cloud/models/requests/modify_psd_request.rb +89 -0
  102. data/lib/aspose-imaging-cloud/models/requests/modify_svg_request.rb +113 -0
  103. data/lib/aspose-imaging-cloud/models/requests/modify_tiff_request.rb +103 -0
  104. data/lib/aspose-imaging-cloud/models/requests/modify_web_p_request.rb +115 -0
  105. data/lib/aspose-imaging-cloud/models/requests/modify_wmf_request.rb +126 -0
  106. data/lib/aspose-imaging-cloud/models/requests/move_file_request.rb +91 -0
  107. data/lib/aspose-imaging-cloud/models/requests/move_folder_request.rb +88 -0
  108. data/lib/aspose-imaging-cloud/models/requests/object_exists_request.rb +80 -0
  109. data/lib/aspose-imaging-cloud/models/requests/resize_image_request.rb +104 -0
  110. data/lib/aspose-imaging-cloud/models/requests/rotate_flip_image_request.rb +96 -0
  111. data/lib/aspose-imaging-cloud/models/requests/save_image_as_request.rb +88 -0
  112. data/lib/aspose-imaging-cloud/models/requests/storage_exists_request.rb +74 -0
  113. data/lib/aspose-imaging-cloud/models/requests/update_image_features_request.rb +91 -0
  114. data/lib/aspose-imaging-cloud/models/requests/update_image_request.rb +144 -0
  115. data/lib/aspose-imaging-cloud/models/requests/update_search_image_request.rb +91 -0
  116. data/lib/aspose-imaging-cloud/models/requests/upload_file_request.rb +86 -0
  117. data/lib/aspose-imaging-cloud/models/search_context_status.rb +212 -0
  118. data/lib/aspose-imaging-cloud/models/search_result.rb +217 -0
  119. data/lib/aspose-imaging-cloud/models/search_results_set.rb +204 -0
  120. data/lib/aspose-imaging-cloud/models/sharpen_filter_properties.rb +266 -0
  121. data/lib/aspose-imaging-cloud/models/small_rectangular_filter_properties.rb +206 -0
  122. data/lib/aspose-imaging-cloud/models/storage_exist.rb +207 -0
  123. data/lib/aspose-imaging-cloud/models/storage_file.rb +252 -0
  124. data/lib/aspose-imaging-cloud/models/svg_properties.rb +202 -0
  125. data/lib/aspose-imaging-cloud/models/tiff_frame.rb +242 -0
  126. data/lib/aspose-imaging-cloud/models/tiff_options.rb +734 -0
  127. data/lib/aspose-imaging-cloud/models/tiff_properties.rb +224 -0
  128. data/lib/aspose-imaging-cloud/models/web_p_properties.rb +252 -0
  129. data/lib/aspose-imaging-cloud/version.rb +15 -0
  130. metadata +149 -129
  131. data/LICENSE +0 -22
  132. data/aspose_imaging_cloud.gemspec +0 -26
  133. data/lib/aspose_imaging_cloud.rb +0 -43
  134. data/lib/aspose_imaging_cloud/api/imaging_api.rb +0 -1776
  135. data/lib/aspose_imaging_cloud/api_client.rb +0 -335
  136. data/lib/aspose_imaging_cloud/api_error.rb +0 -24
  137. data/lib/aspose_imaging_cloud/models/base_object.rb +0 -86
  138. data/lib/aspose_imaging_cloud/models/base_response.rb +0 -53
  139. data/lib/aspose_imaging_cloud/models/bmp_properties.rb +0 -37
  140. data/lib/aspose_imaging_cloud/models/exif_data.rb +0 -855
  141. data/lib/aspose_imaging_cloud/models/gif_properties.rb +0 -53
  142. data/lib/aspose_imaging_cloud/models/imaging_response.rb +0 -149
  143. data/lib/aspose_imaging_cloud/models/jfif_data.rb +0 -61
  144. data/lib/aspose_imaging_cloud/models/jpeg_exif_data.rb +0 -991
  145. data/lib/aspose_imaging_cloud/models/jpeg_properties.rb +0 -53
  146. data/lib/aspose_imaging_cloud/models/psd_properties.rb +0 -61
  147. data/lib/aspose_imaging_cloud/models/tiff_frame.rb +0 -61
  148. data/lib/aspose_imaging_cloud/models/tiff_options.rb +0 -419
  149. data/lib/aspose_imaging_cloud/models/tiff_properties.rb +0 -55
  150. data/lib/aspose_imaging_cloud/version.rb +0 -3
  151. data/test/data/TestDemo.tif +0 -0
  152. data/test/data/aspose.jpg +0 -0
  153. data/test/data/aspose_imaging_for_cloud.png +0 -0
  154. data/test/data/demo.tif +0 -0
  155. data/test/data/sample-multi.tif +0 -0
  156. data/test/data/sample.bmp +0 -0
  157. data/test/data/sample.gif +0 -0
  158. data/test/data/sample.psd +0 -0
  159. data/test/data/sample.tif +0 -0
  160. data/test/data/test.bmp +0 -0
  161. data/test/imaging_tests.rb +0 -291
@@ -0,0 +1,357 @@
1
+ # ----------------------------------------------------------------------------
2
+ # <copyright company="Aspose" file="api_client.rb">
3
+ # Copyright (c) 2018-2019 Aspose Pty Ltd. All rights reserved.
4
+ # </copyright>
5
+ # <summary>
6
+ # Permission is hereby granted, free of charge, to any person obtaining a
7
+ # copy of this software and associated documentation files (the "Software"),
8
+ # to deal in the Software without restriction, including without limitation
9
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
+ # and/or sell copies of the Software, and to permit persons to whom the
11
+ # Software is furnished to do so, subject to the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all 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
21
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
+ # DEALINGS IN THE 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 'uri'
33
+
34
+ module AsposeImagingCloud
35
+ # Generic API client
36
+ # Swagger generic API client. This client handles the client-
37
+ # server communication, and is invariant across implementations.
38
+ class ApiClient
39
+ # The Configuration object holding settings to be used in the API client.
40
+ attr_accessor :config
41
+
42
+ # Defines the headers to be used in HTTP requests of all API calls by default.
43
+ #
44
+ # @return [Hash]
45
+ attr_accessor :default_headers
46
+
47
+ # Initializes the ApiClient
48
+ # @param [String] app_key Key to access the server.
49
+ # @param [String] app_sid ID to access the server.
50
+ # @param [String] base_url Server URL.
51
+ # @param [String] api_version Api version.
52
+ # @param [Object] debug Debug switch [true, false].
53
+ def initialize(app_key = nil, app_sid = nil, base_url = 'api-qa.aspose.cloud', api_version = 'v3.0', debug = false)
54
+ @config = Configuration.new(app_key, app_sid, base_url, api_version, debug)
55
+ @default_headers = {
56
+ 'x-aspose-client' => 'ruby sdk',
57
+ 'x-aspose-version' => '19.12'
58
+ }
59
+ end
60
+
61
+ # Call an API with given options.
62
+ #
63
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
64
+ # the data deserialized from response body (could be nil), response status code and response headers.
65
+ def call_api(http_method, path, opts = {})
66
+ response = build_request(http_method, path, opts)
67
+
68
+ if @config.debugging
69
+ @config.logger.debug "'HTTP' response body '~BEGIN~'\n #{response.body}\n'~END~'\n"
70
+ end
71
+
72
+ unless response.success?
73
+ if response.status.zero?
74
+ # Errors from libcurl will be made visible here
75
+ raise ApiError.new(response.return_message, 0)
76
+ else
77
+ begin
78
+ error = convert_to_type(JSON.parse(response.body, symbolize_names: true)[:error], "Error")
79
+ ensure
80
+ raise ApiError.new("#{response.status} Error connecting to the API #{path}; Message: #{error}",
81
+ response.status, error)
82
+ end
83
+ end
84
+ end
85
+
86
+ data = ''
87
+ if opts[:return_type]
88
+ data = opts[:return_type] == 'File' ? response.body : deserialize(response, opts[:return_type])
89
+ end
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
+ elsif http_method == :get && body.nil? && !form_params.any? && header_params['Content-Type'] == 'multipart/form-data'
127
+ req_opts[:headers].delete('Content-Type')
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 :net_http_persistent, pool_size: 5
134
+ end
135
+
136
+ case http_method
137
+ when :post
138
+ return conn.post url, req_opts[:body]
139
+ when :put
140
+ return conn.put url, req_opts[:body]
141
+ when :get
142
+ return conn.get url, req_opts[:body]
143
+ else
144
+ return conn.delete url do |c|
145
+ c.body = req_opts[:body]
146
+ end
147
+ end
148
+ end
149
+
150
+ # Check if the given MIME is a JSON MIME.
151
+ # JSON MIME examples:
152
+ # application/json
153
+ # application/json; charset=UTF8
154
+ # APPLICATION/JSON
155
+ # */*
156
+ # @param [String] mime MIME
157
+ # @return [Boolean] True if the MIME is application/json
158
+ def json_mime?(mime)
159
+ (mime == '*/*') || !(mime =~ %r{Application/.*json(?!p)(;.*)?}i).nil?
160
+ end
161
+
162
+ # Deserialize the response to the given return type.
163
+ #
164
+ # @param [Response] response HTTP response
165
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
166
+ def deserialize(response, return_type)
167
+ body = response.body
168
+
169
+ # handle file downloading - return the File instance processed in request callbacks
170
+ # note that response body is empty when the file is written in chunks in request on_body callback
171
+ return @tempfile if return_type == 'File'
172
+
173
+ return nil if body.nil? || body.empty?
174
+
175
+ # return response body directly for String return type
176
+ return body if return_type == 'String'
177
+
178
+ # ensuring a default content type
179
+ content_type = response.headers['Content-Type'] || 'application/json'
180
+
181
+ raise "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
182
+
183
+ begin
184
+ data = JSON.parse("[#{body}]", symbolize_names: true)[0]
185
+ rescue JSON::ParserError => e
186
+ if %w[String Date DateTime].include?(return_type)
187
+ data = body
188
+ else
189
+ raise e
190
+ end
191
+ end
192
+
193
+ convert_to_type data, return_type
194
+ end
195
+
196
+ # Convert data to the given return type.
197
+ # @param [Object] data Data to be converted
198
+ # @param [String] return_type Return type
199
+ # @return [Mixed] Data in a particular type
200
+ def convert_to_type(data, return_type)
201
+ return nil if data.nil?
202
+
203
+ case return_type
204
+ when 'String'
205
+ data.to_s
206
+ when 'Integer'
207
+ data.to_i
208
+ when 'Float'
209
+ data.to_f
210
+ when 'BOOLEAN'
211
+ data == true
212
+ when 'DateTime'
213
+ # parse date time (expecting ISO 8601 format)
214
+ DateTime.parse data
215
+ when 'Date'
216
+ # parse date time (expecting ISO 8601 format)
217
+ Date.parse data
218
+ when 'Object'
219
+ # generic object (usually a Hash), return directly
220
+ data
221
+ when /\AArray<(.+)>\z/
222
+ # e.g. Array<Pet>
223
+ sub_type = Regexp.last_match(1)
224
+ data.map { |item| convert_to_type(item, sub_type) }
225
+ when /\AHash\<String, (.+)\>\z/
226
+ # e.g. Hash<String, Integer>
227
+ sub_type = Regexp.last_match(1)
228
+ {}.tap do |hash|
229
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
230
+ end
231
+ else
232
+ # models, e.g. Pet
233
+ AsposeImagingCloud.const_get(return_type).new.tap do |model|
234
+ model.build_from_hash data
235
+ end
236
+ end
237
+ end
238
+
239
+ # Save response body into a file in (the defined) temporary folder, using the filename
240
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
241
+ # The response body is written to the file in chunks in order to handle files which
242
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
243
+ # process can use.
244
+ #
245
+ # @see Configuration#temp_folder_path
246
+ def download_file(response)
247
+ tempfile = nil
248
+ encoding = nil
249
+ content_disposition = response.headers['Content-Disposition']
250
+ if content_disposition and content_disposition =~ /filename=/i
251
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
252
+ prefix = sanitize_filename(filename)
253
+ else
254
+ prefix = 'download-'
255
+ end
256
+ prefix += '-' unless prefix.end_with?('-')
257
+ encoding = response.body.encoding
258
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path).binmode
259
+ @tempfile = tempfile
260
+ tempfile.write(response.body)
261
+ response.on_complete do |resp|
262
+ tempfile.close
263
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
264
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
265
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
266
+ "explicitly with `tempfile.delete`"
267
+ end
268
+ end
269
+
270
+ # Sanitize filename by removing path.
271
+ # e.g. ../../sun.gif becomes sun.gif
272
+ #
273
+ # @param [String] filename the filename to be sanitized
274
+ # @return [String] the sanitized filename
275
+ def sanitize_filename(filename)
276
+ filename.gsub(%r{.*[/\\]}, '')
277
+ end
278
+
279
+ def build_request_url(path)
280
+ # Add leading and trailing slashes to path
281
+ path = "/#{path}".gsub(%r{/+}, '/')
282
+ URI.encode(@config.base_url + path)
283
+ end
284
+
285
+ # Builds the HTTP request body
286
+ #
287
+ # @param [Hash] header_params Header parameters
288
+ # @param [Hash] form_params Query parameters
289
+ # @param [Object] body HTTP body (JSON/XML)
290
+ # @return [String] HTTP body data in the form of string
291
+ def build_request_body(header_params, form_params, body)
292
+ # http form
293
+ if (header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
294
+ header_params['Content-Type'] == 'multipart/form-data') && form_params.any?
295
+ data = {}
296
+ form_params.each do |key, value|
297
+ case value
298
+ when ::File, ::Tempfile
299
+ data[key] = Faraday::UploadIO.new(value.path, MimeMagic.by_magic(value).to_s, key)
300
+ when ::Array, nil
301
+ data[key] = value
302
+ when ::StringIO
303
+ data[key] = Faraday::UploadIO.new(value, MimeMagic.by_magic(value).to_s, key)
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.to_sym]
324
+ next unless auth_setting
325
+
326
+ case auth_setting[:in]
327
+ when 'header'
328
+ header_params[auth_setting[:key]] = auth_setting[:value]
329
+ when 'query'
330
+ query_params[auth_setting[:key]] = auth_setting[:value]
331
+ else
332
+ raise ArgumentError, 'Authentication token must be in `query` of `header`'
333
+ end
334
+ end
335
+ end
336
+
337
+ # Build parameter value according to the given collection format.
338
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
339
+ def build_collection_param(param, collection_format)
340
+ case collection_format
341
+ when :csv
342
+ param.join(',')
343
+ when :ssv
344
+ param.join(' ')
345
+ when :tsv
346
+ param.join("\t")
347
+ when :pipes
348
+ param.join('|')
349
+ when :multi
350
+ # return the array directly as typhoeus will handle it as expected
351
+ param
352
+ else
353
+ raise "unknown collection format: #{collection_format.inspect}"
354
+ end
355
+ end
356
+ end
357
+ end
@@ -0,0 +1,42 @@
1
+ # ----------------------------------------------------------------------------
2
+ # <copyright company="Aspose" file="api_error.rb">
3
+ # Copyright (c) 2018-2019 Aspose Pty Ltd. All rights reserved.
4
+ # </copyright>
5
+ # <summary>
6
+ # Permission is hereby granted, free of charge, to any person obtaining a
7
+ # copy of this software and associated documentation files (the "Software"),
8
+ # to deal in the Software without restriction, including without limitation
9
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
+ # and/or sell copies of the Software, and to permit persons to whom the
11
+ # Software is furnished to do so, subject to the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all 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
21
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
+ # DEALINGS IN THE SOFTWARE.
23
+ # </summary>
24
+ # ----------------------------------------------------------------------------
25
+
26
+
27
+ module AsposeImagingCloud
28
+ # Imaging API Exception
29
+ class ApiError < StandardError
30
+ attr_reader :message, :code, :error
31
+
32
+ # Initializes a new instance of the ApiError class
33
+ # @param [String] message
34
+ # @param [String] code
35
+ # @param [String] error
36
+ def initialize(message, code, error = nil)
37
+ @message = message
38
+ @code = code
39
+ @error = error
40
+ end
41
+ end
42
+ end
@@ -1,166 +1,236 @@
1
- require 'uri'
2
- require 'singleton'
3
-
4
- module AsposeImagingCloud
5
- class Configuration
6
-
7
- include Singleton
8
-
9
- # Default api client
10
- attr_accessor :api_client
11
-
12
- # Defines url scheme
13
- attr_accessor :scheme
14
-
15
- # Defines url host
16
- attr_accessor :host
17
-
18
- # Defines url base path
19
- attr_accessor :base_path
20
-
21
- # Defines API keys used with API Key authentications.
22
- #
23
- # @return [Hash] key: parameter name, value: parameter value (API key)
24
- #
25
- # @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
26
- # config.api_key['api_key'] = 'xxx'
27
- attr_accessor :api_key
28
-
29
- # Defines API key prefixes used with API Key authentications.
30
- #
31
- # @return [Hash] key: parameter name, value: API key prefix
32
- #
33
- # @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
34
- # config.api_key_prefix['api_key'] = 'Token'
35
- attr_accessor :api_key_prefix
36
-
37
- # Defines the username used with HTTP basic authentication.
38
- #
39
- # @return [String]
40
- attr_accessor :username
41
-
42
- # Defines the password used with HTTP basic authentication.
43
- #
44
- # @return [String]
45
- attr_accessor :password
46
-
47
- # Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
48
- # details will be logged with `logger.debug` (see the `logger` attribute).
49
- # Default to false.
50
- #
51
- # @return [true, false]
52
- attr_accessor :debugging
53
-
54
- # Defines the logger used for debugging.
55
- # Default to `Rails.logger` (when in Rails) or logging to STDOUT.
56
- #
57
- # @return [#debug]
58
- attr_accessor :logger
59
-
60
- # Defines the temporary folder to store downloaded files
61
- # (for API endpoints that have file response).
62
- # Default to use `Tempfile`.
63
- #
64
- # @return [String]
65
- attr_accessor :temp_folder_path
66
-
67
- ### TLS/SSL
68
- # Set this to false to skip verifying SSL certificate when calling API from https server.
69
- # Default to true.
70
- #
71
- # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
72
- #
73
- # @return [true, false]
74
- attr_accessor :verify_ssl
75
-
76
- # Set this to customize the certificate file to verify the peer.
77
- #
78
- # @return [String] the path to the certificate file
79
- #
80
- # @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
81
- # https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
82
- attr_accessor :ssl_ca_cert
83
-
84
- # Client certificate file (for client certificate)
85
- attr_accessor :cert_file
86
-
87
- # Client private key file (for client certificate)
88
- attr_accessor :key_file
89
-
90
- attr_accessor :inject_format
91
-
92
- attr_accessor :force_ending_format
93
-
94
- class << self
95
- def method_missing(method_name, *args, &block)
96
- config = Configuration.instance
97
- if config.respond_to?(method_name)
98
- config.send(method_name, *args, &block)
99
- else
100
- super
101
- end
102
- end
103
- end
104
-
105
- def initialize
106
- @scheme = 'http'
107
- @host = 'api.aspose.com'
108
- @base_path = '/v1.1'
109
- @api_key = {}
110
- @api_key_prefix = {}
111
- @verify_ssl = true
112
- @cert_file = nil
113
- @key_file = nil
114
- @debugging = false
115
- @inject_format = false
116
- @force_ending_format = false
117
- @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
118
- end
119
-
120
- def api_client
121
- @api_client ||= ApiClient.new
122
- end
123
-
124
- def scheme=(scheme)
125
- # remove :// from scheme
126
- @scheme = scheme.sub(/:\/\//, '')
127
- end
128
-
129
- def host=(host)
130
- # remove http(s):// and anything after a slash
131
- @host = host.sub(/https?:\/\//, '').split('/').first
132
- end
133
-
134
- def base_path=(base_path)
135
- # Add leading and trailing slashes to base_path
136
- @base_path = "/#{base_path}".gsub(/\/+/, '/')
137
- @base_path = "" if @base_path == "/"
138
- end
139
-
140
- def base_url
141
- url = "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
142
- URI.encode(url)
143
- end
144
-
145
- # Gets API key (with prefix if set).
146
- # @param [String] param_name the parameter name of API key auth
147
- def api_key_with_prefix(param_name)
148
- if @api_key_prefix[param_name]
149
- "#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
150
- else
151
- @api_key[param_name]
152
- end
153
- end
154
-
155
- # Gets Basic Auth token string
156
- def basic_auth_token
157
- 'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
158
- end
159
-
160
- # Returns Auth Settings hash for api client.
161
- def auth_settings
162
- {
163
- }
164
- end
165
- end
166
- end
1
+ # ----------------------------------------------------------------------------
2
+ # <copyright company="Aspose" file="configuration.rb">
3
+ # Copyright (c) 2018-2019 Aspose Pty Ltd. All rights reserved.
4
+ # </copyright>
5
+ # <summary>
6
+ # Permission is hereby granted, free of charge, to any person obtaining a
7
+ # copy of this software and associated documentation files (the "Software"),
8
+ # to deal in the Software without restriction, including without limitation
9
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
+ # and/or sell copies of the Software, and to permit persons to whom the
11
+ # Software is furnished to do so, subject to the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all 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
21
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
+ # DEALINGS IN THE SOFTWARE.
23
+ # </summary>
24
+ # ----------------------------------------------------------------------------
25
+
26
+
27
+ require 'uri'
28
+
29
+ module AsposeImagingCloud
30
+
31
+ # Class which contains configuration parameters
32
+ class Configuration
33
+ attr_accessor :on_premise
34
+
35
+ # Defines url scheme
36
+ attr_accessor :scheme
37
+
38
+ # Defines url host
39
+ attr_accessor :host
40
+
41
+ # Defines url base path
42
+ attr_accessor :base_path
43
+
44
+ # Defines api version
45
+ attr_accessor :api_version
46
+
47
+ # Defines API keys used with API Key authentications.
48
+ #
49
+ # @return [Hash] key: parameter name, value: parameter value (API key)
50
+ #
51
+ # @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
52
+ # config.api_key['api_key'] = 'xxx'
53
+ attr_accessor :api_key
54
+
55
+ # Defines API key prefixes used with API Key authentications.
56
+ #
57
+ # @return [Hash] key: parameter name, value: API key prefix
58
+ #
59
+ # @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
60
+ # config.api_key_prefix['api_key'] = 'Token'
61
+ attr_accessor :api_key_prefix
62
+
63
+ # Defines the username used with HTTP basic authentication.
64
+ #
65
+ # @return [String]
66
+ attr_accessor :username
67
+
68
+ # Defines the password used with HTTP basic authentication.
69
+ #
70
+ # @return [String]
71
+ attr_accessor :password
72
+
73
+ # Defines the access token (Bearer) used with OAuth2.
74
+ attr_accessor :access_token
75
+
76
+ # Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
77
+ # details will be logged with `logger.debug` (see the `logger` attribute).
78
+ # Default to false.
79
+ #
80
+ # @return [true, false]
81
+ attr_accessor :debugging
82
+
83
+ # Defines the logger used for debugging.
84
+ # Default to `Rails.logger` (when in Rails) or logging to STDOUT.
85
+ #
86
+ # @return [#debug]
87
+ attr_accessor :logger
88
+
89
+ # Defines the temporary folder to store downloaded files
90
+ # (for API endpoints that have file response).
91
+ # Default to use `Tempfile`.
92
+ #
93
+ # @return [String]
94
+ attr_accessor :temp_folder_path
95
+
96
+ # The time limit for HTTP request in seconds.
97
+ # Default to 0 (never times out).
98
+ attr_accessor :timeout
99
+
100
+ # Set this to false to skip client side validation in the operation.
101
+ # Default to true.
102
+ # @return [true, false]
103
+ attr_accessor :client_side_validation
104
+
105
+ ### TLS/SSL setting
106
+ # Set this to false to skip verifying SSL certificate when calling API from https server.
107
+ # Default to true.
108
+ #
109
+ # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
110
+ #
111
+ # @return [true, false]
112
+ attr_accessor :verify_ssl
113
+
114
+ ### TLS/SSL setting
115
+ # Set this to false to skip verifying SSL host name
116
+ # Default to true.
117
+ #
118
+ # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
119
+ #
120
+ # @return [true, false]
121
+ attr_accessor :verify_ssl_host
122
+
123
+ ### TLS/SSL setting
124
+ # Set this to customize the certificate file to verify the peer.
125
+ #
126
+ # @return [String] the path to the certificate file
127
+ #
128
+ # @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
129
+ # https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
130
+ attr_accessor :ssl_ca_cert
131
+
132
+ ### TLS/SSL setting
133
+ # Client certificate file (for client certificate)
134
+ attr_accessor :cert_file
135
+
136
+ ### TLS/SSL setting
137
+ # Client private key file (for client certificate)
138
+ attr_accessor :key_file
139
+
140
+ # Set this to customize parameters encoding of array parameter with multi collectionFormat.
141
+ # Default to nil.
142
+ #
143
+ # @see The params_encoding option of Ethon. Related source code:
144
+ # https://github.com/typhoeus/ethon/blob/master/lib/ethon/easy/queryable.rb#L96
145
+ attr_accessor :params_encoding
146
+
147
+ attr_accessor :inject_format
148
+
149
+ attr_accessor :force_ending_format
150
+
151
+ # Initializes a new instance.
152
+ # @param [String] app_key Key to access the server.
153
+ # @param [String] app_sid ID to access the server.
154
+ # @param [String] base_url Server URL.
155
+ # @param [String] api_version Api version.
156
+ # @param [Object] debug Debug switch [true, false].
157
+ def initialize(app_key = nil, app_sid = nil, base_url = 'api-qa.aspose.cloud', api_version = 'v3.0', debug = false)
158
+ @scheme = 'https'
159
+ self.host = base_url
160
+ @api_version = api_version
161
+ self.base_path = @api_version
162
+ @api_key = { 'api_key': app_key || '',
163
+ 'app_sid': app_sid || '' }
164
+ @api_key_prefix = {}
165
+ @timeout = 0
166
+ @client_side_validation = true
167
+ @verify_ssl = true
168
+ @verify_ssl_host = true
169
+ @params_encoding = nil
170
+ @cert_file = nil
171
+ @key_file = nil
172
+ @debugging = debug
173
+ @inject_format = false
174
+ @force_ending_format = false
175
+ @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
176
+ @on_premise = (!(app_key || app_sid) && base_url)
177
+
178
+ yield(self) if block_given?
179
+ end
180
+
181
+ def configure
182
+ yield(self) if block_given?
183
+ end
184
+
185
+ def scheme=(scheme)
186
+ # remove :// from scheme
187
+ @scheme = scheme.sub(/:\/\//, '')
188
+ end
189
+
190
+ def host=(host)
191
+ # remove http(s):// and anything after a slash
192
+ @host = host.sub(/https?:\/\//, '').split('/').first
193
+ end
194
+
195
+ def base_path=(base_path)
196
+ return unless base_path
197
+
198
+ # Add leading and trailing slashes to base_path
199
+ @base_path = "/#{base_path}".gsub(/\/+/, '/')
200
+ @base_path = '' if @base_path == '/'
201
+ end
202
+
203
+ def base_url
204
+ url = "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
205
+ URI.encode(url)
206
+ end
207
+
208
+ # Gets API key (with prefix if set).
209
+ # @param [String] param_name the parameter name of API key auth
210
+ def api_key_with_prefix(param_name)
211
+ if @api_key_prefix[param_name]
212
+ "#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
213
+ else
214
+ @api_key[param_name]
215
+ end
216
+ end
217
+
218
+ # Gets Basic Auth token string
219
+ def basic_auth_token
220
+ 'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
221
+ end
222
+
223
+ # Returns Auth Settings hash for api client.
224
+ def auth_settings
225
+ {
226
+ JWT:
227
+ {
228
+ type: 'oauth2',
229
+ in: 'header',
230
+ key: 'Authorization',
231
+ value: "Bearer #{access_token}"
232
+ }
233
+ }
234
+ end
235
+ end
236
+ end