aspose_html_cloud 19.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/aspose_html_cloud.rb +63 -0
- data/lib/aspose_html_cloud/api/html_api.rb +2478 -0
- data/lib/aspose_html_cloud/api/storage_api.rb +859 -0
- data/lib/aspose_html_cloud/api_client.rb +393 -0
- data/lib/aspose_html_cloud/api_error.rb +54 -0
- data/lib/aspose_html_cloud/configuration.rb +177 -0
- data/lib/aspose_html_cloud/models/base_model.rb +145 -0
- data/lib/aspose_html_cloud/models/disc_usage.rb +113 -0
- data/lib/aspose_html_cloud/models/error.rb +122 -0
- data/lib/aspose_html_cloud/models/error_details.rb +107 -0
- data/lib/aspose_html_cloud/models/file_version.rb +167 -0
- data/lib/aspose_html_cloud/models/file_versions.rb +94 -0
- data/lib/aspose_html_cloud/models/files_list.rb +94 -0
- data/lib/aspose_html_cloud/models/files_upload_result.rb +106 -0
- data/lib/aspose_html_cloud/models/object_exist.rb +111 -0
- data/lib/aspose_html_cloud/models/storage_exist.rb +96 -0
- data/lib/aspose_html_cloud/models/storage_file.rb +135 -0
- data/lib/aspose_html_cloud/version.rb +31 -0
- data/spec/api/html_api_spec.rb +2612 -0
- data/spec/api/model_spec.rb +430 -0
- data/spec/api/storage_api_spec.rb +542 -0
- data/spec/spec_helper.rb +165 -0
- metadata +249 -0
@@ -0,0 +1,393 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
=begin
|
3
|
+
--------------------------------------------------------------------------------------------------------------------
|
4
|
+
<copyright company="Aspose" file="api_client.rb">
|
5
|
+
</copyright>
|
6
|
+
Copyright (c) 2019 Aspose.HTML for Cloud
|
7
|
+
<summary>
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
10
|
+
in the Software without restriction, including without limitation the rights
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
13
|
+
furnished to do so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
16
|
+
copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24
|
+
SOFTWARE.
|
25
|
+
</summary>
|
26
|
+
--------------------------------------------------------------------------------------------------------------------
|
27
|
+
=end
|
28
|
+
|
29
|
+
require 'date'
|
30
|
+
require 'json'
|
31
|
+
require 'logger'
|
32
|
+
require 'tempfile'
|
33
|
+
require 'typhoeus'
|
34
|
+
require 'uri'
|
35
|
+
require 'securerandom'
|
36
|
+
require 'net/http'
|
37
|
+
|
38
|
+
|
39
|
+
module AsposeHtml
|
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)
|
52
|
+
@config = config
|
53
|
+
@user_agent = "Aspose SDK"
|
54
|
+
@default_headers = {
|
55
|
+
'Content-Type' => "application/json",
|
56
|
+
'User-Agent' => @user_agent,
|
57
|
+
'Authorization' => "Bearer #{@config.access_token}"
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.default(args)
|
62
|
+
@@default ||= ApiClient.new(Configuration.new(args))
|
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
|
+
http_method = http_method.to_sym.downcase
|
71
|
+
request = build_request(http_method, path, opts)
|
72
|
+
response = request.run
|
73
|
+
|
74
|
+
if @config.debug
|
75
|
+
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
76
|
+
end
|
77
|
+
|
78
|
+
unless response.success?
|
79
|
+
if response.timed_out?
|
80
|
+
fail ApiError.new('Connection timed out')
|
81
|
+
elsif response.code == 0
|
82
|
+
# Errors from libcurl will be made visible here
|
83
|
+
fail ApiError.new(:code => 0,
|
84
|
+
:message => response.return_message)
|
85
|
+
else
|
86
|
+
fail ApiError.new(:code => response.code,
|
87
|
+
:response_headers => response.headers,
|
88
|
+
:response_body => response.body),
|
89
|
+
response.status_message
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
if opts[:return_type]
|
94
|
+
data = deserialize(response, opts[:return_type])
|
95
|
+
else
|
96
|
+
data = nil
|
97
|
+
end
|
98
|
+
return data, response.code, response.headers
|
99
|
+
end
|
100
|
+
|
101
|
+
# Builds the HTTP request
|
102
|
+
#
|
103
|
+
# @param [String] http_method HTTP method/verb (e.g. POST)
|
104
|
+
# @param [String] path URL path (e.g. /account/new)
|
105
|
+
# @option opts [Hash] :header_params Header parameters
|
106
|
+
# @option opts [Hash] :query_params Query parameters
|
107
|
+
# @option opts [Hash] :form_params Query parameters
|
108
|
+
# @option opts [Object] :body HTTP body (JSON/XML)
|
109
|
+
# @return [Typhoeus::Request] A Typhoeus Request
|
110
|
+
def build_request(http_method, path, opts = {})
|
111
|
+
url = build_request_url(path)
|
112
|
+
|
113
|
+
header_params = @default_headers.merge(opts[:header_params] || {})
|
114
|
+
query_params = opts[:query_params] || {}
|
115
|
+
form_params = opts[:form_params] || {}
|
116
|
+
|
117
|
+
# set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
|
118
|
+
_verify_ssl_host = @config.verify_ssl_host ? 2 : 0
|
119
|
+
|
120
|
+
req_opts = {
|
121
|
+
:method => http_method,
|
122
|
+
:headers => header_params,
|
123
|
+
:params => query_params,
|
124
|
+
:params_encoding => @config.params_encoding,
|
125
|
+
:timeout => @config.timeout,
|
126
|
+
:ssl_verifypeer => @config.verify_ssl,
|
127
|
+
:ssl_verifyhost => _verify_ssl_host,
|
128
|
+
:sslcert => @config.cert_file,
|
129
|
+
:sslkey => @config.key_file,
|
130
|
+
:verbose => @config.debug
|
131
|
+
}
|
132
|
+
|
133
|
+
# set custom cert, if provided
|
134
|
+
req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
|
135
|
+
|
136
|
+
if [:post, :patch, :put, :delete].include?(http_method)
|
137
|
+
req_body = build_request_body(header_params, form_params, opts[:body])
|
138
|
+
req_opts.update :body => req_body
|
139
|
+
|
140
|
+
if @config.debug
|
141
|
+
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
request = Typhoeus::Request.new(url, req_opts)
|
146
|
+
download_file(request) if opts[:return_type] == 'File'
|
147
|
+
request
|
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 =~ /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
|
+
fail "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
|
+
case return_type
|
203
|
+
when 'String'
|
204
|
+
data.to_s
|
205
|
+
when 'Integer'
|
206
|
+
data.to_i
|
207
|
+
when 'Float'
|
208
|
+
data.to_f
|
209
|
+
when 'BOOLEAN'
|
210
|
+
data == true
|
211
|
+
when 'DateTime'
|
212
|
+
# parse date time (expecting ISO 8601 format)
|
213
|
+
DateTime.parse data
|
214
|
+
when 'Date'
|
215
|
+
# parse date time (expecting ISO 8601 format)
|
216
|
+
Date.parse data
|
217
|
+
when 'Object'
|
218
|
+
# generic object (usually a Hash), return directly
|
219
|
+
data
|
220
|
+
when /\AArray<(.+)>\z/
|
221
|
+
# e.g. Array<Pet>
|
222
|
+
sub_type = $1
|
223
|
+
data.map { |item| convert_to_type(item, sub_type) }
|
224
|
+
when /\AHash\<String, (.+)\>\z/
|
225
|
+
# e.g. Hash<String, Integer>
|
226
|
+
sub_type = $1
|
227
|
+
{}.tap do |hash|
|
228
|
+
data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
|
229
|
+
end
|
230
|
+
else
|
231
|
+
# models, e.g. Pet
|
232
|
+
AsposeHtml.const_get(return_type).new.tap do |model|
|
233
|
+
model.build_from_hash data
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
# Save response body into a file in (the defined) temporary folder, using the filename
|
239
|
+
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
240
|
+
# The response body is written to the file in chunks in order to handle files which
|
241
|
+
# size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
|
242
|
+
# process can use.
|
243
|
+
#
|
244
|
+
# @see Configuration#temp_folder_path
|
245
|
+
def download_file(request)
|
246
|
+
tempfile = nil
|
247
|
+
encoding = nil
|
248
|
+
request.on_headers do |response|
|
249
|
+
content_disposition = response.headers['Content-Disposition']
|
250
|
+
if content_disposition && 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 = prefix + '-' unless prefix.end_with?('-')
|
257
|
+
encoding = response.body.encoding
|
258
|
+
tempfile = File.open(@config.temp_folder_path + "/" + prefix, "wb")
|
259
|
+
@tempfile = tempfile
|
260
|
+
end
|
261
|
+
request.on_body do |chunk|
|
262
|
+
# chunk.force_encoding(encoding)
|
263
|
+
tempfile.write(chunk)
|
264
|
+
end
|
265
|
+
request.on_complete do |response|
|
266
|
+
tempfile.close
|
267
|
+
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
|
268
|
+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
269
|
+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
270
|
+
"explicitly with `tempfile.delete`"
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
# Sanitize filename by removing path.
|
275
|
+
# e.g. ../../sun.gif becomes sun.gif
|
276
|
+
#
|
277
|
+
# @param [String] filename the filename to be sanitized
|
278
|
+
# @return [String] the sanitized filename
|
279
|
+
def sanitize_filename(filename)
|
280
|
+
filename.gsub(/.*[\/\\]/, '')
|
281
|
+
end
|
282
|
+
|
283
|
+
def build_request_url(path)
|
284
|
+
# Add leading and trailing slashes to path
|
285
|
+
path = "/#{path}".gsub(/\/+/, '/')
|
286
|
+
URI.encode(@config.base_url + path)
|
287
|
+
end
|
288
|
+
|
289
|
+
# Builds the HTTP request body
|
290
|
+
#
|
291
|
+
# @param [Hash] header_params Header parameters
|
292
|
+
# @param [Hash] form_params Query parameters
|
293
|
+
# @param [Object] body HTTP body (JSON/XML)
|
294
|
+
# @return [String] HTTP body data in the form of string
|
295
|
+
def build_request_body(header_params, form_params, body)
|
296
|
+
# http form
|
297
|
+
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
|
298
|
+
header_params['Content-Type'] == 'multipart/form-data'
|
299
|
+
data = {}
|
300
|
+
form_params.each do |key, value|
|
301
|
+
case value
|
302
|
+
when ::File, ::Array, nil
|
303
|
+
# let typhoeus handle File, Array and nil parameters
|
304
|
+
# but Put don't processing
|
305
|
+
data[key] = value
|
306
|
+
else
|
307
|
+
data[key] = value.to_s
|
308
|
+
end
|
309
|
+
end
|
310
|
+
elsif body
|
311
|
+
data = body.is_a?(String) ? body : body.to_json
|
312
|
+
else
|
313
|
+
data = nil
|
314
|
+
end
|
315
|
+
data
|
316
|
+
end
|
317
|
+
|
318
|
+
# Sets user agent in HTTP header
|
319
|
+
#
|
320
|
+
# @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
|
321
|
+
def user_agent=(user_agent)
|
322
|
+
@user_agent = user_agent
|
323
|
+
@default_headers['User-Agent'] = @user_agent
|
324
|
+
end
|
325
|
+
|
326
|
+
# Return Accept header based on an array of accepts provided.
|
327
|
+
# @param [Array] accepts array for Accept
|
328
|
+
# @return [String] the Accept header (e.g. application/json)
|
329
|
+
def select_header_accept(accepts)
|
330
|
+
return nil if accepts.nil? || accepts.empty?
|
331
|
+
# use JSON when present, otherwise use all of the provided
|
332
|
+
json_accept = accepts.find { |s| json_mime?(s) }
|
333
|
+
json_accept || accepts.join(',')
|
334
|
+
end
|
335
|
+
|
336
|
+
# Return Content-Type header based on an array of content types provided.
|
337
|
+
# @param [Array] content_types array for Content-Type
|
338
|
+
# @return [String] the Content-Type header (e.g. application/json)
|
339
|
+
def select_header_content_type(content_types)
|
340
|
+
# use application/json by default
|
341
|
+
return 'application/json' if content_types.nil? || content_types.empty?
|
342
|
+
# use JSON when present, otherwise use the first one
|
343
|
+
json_content_type = content_types.find { |s| json_mime?(s) }
|
344
|
+
json_content_type || content_types.first
|
345
|
+
end
|
346
|
+
|
347
|
+
# Convert object (array, hash, object, etc) to JSON string.
|
348
|
+
# @param [Object] model object to be converted into JSON string
|
349
|
+
# @return [String] JSON string representation of the object
|
350
|
+
def object_to_http_body(model)
|
351
|
+
return model if model.nil? || model.is_a?(String)
|
352
|
+
local_body = nil
|
353
|
+
if model.is_a?(Array)
|
354
|
+
local_body = model.map { |m| object_to_hash(m) }
|
355
|
+
else
|
356
|
+
local_body = object_to_hash(model)
|
357
|
+
end
|
358
|
+
local_body.to_json
|
359
|
+
end
|
360
|
+
|
361
|
+
# Convert object(non-array) to hash.
|
362
|
+
# @param [Object] obj object to be converted into JSON string
|
363
|
+
# @return [String] JSON string representation of the object
|
364
|
+
def object_to_hash(obj)
|
365
|
+
if obj.respond_to?(:to_hash)
|
366
|
+
obj.to_hash
|
367
|
+
else
|
368
|
+
obj
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
372
|
+
# Build parameter value according to the given collection format.
|
373
|
+
# @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
|
374
|
+
def build_collection_param(param, collection_format)
|
375
|
+
case collection_format
|
376
|
+
when :csv
|
377
|
+
param.join(',')
|
378
|
+
when :ssv
|
379
|
+
param.join(' ')
|
380
|
+
when :tsv
|
381
|
+
param.join("\t")
|
382
|
+
when :pipes
|
383
|
+
param.join('|')
|
384
|
+
when :multi
|
385
|
+
# return the array directly as typhoeus will handle it as expected
|
386
|
+
param
|
387
|
+
else
|
388
|
+
fail "unknown collection format: #{collection_format.inspect}"
|
389
|
+
end
|
390
|
+
end
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
=begin
|
3
|
+
--------------------------------------------------------------------------------------------------------------------
|
4
|
+
<copyright company="Aspose" file="api_error.rb">
|
5
|
+
</copyright>
|
6
|
+
Copyright (c) 2019 Aspose.HTML for Cloud
|
7
|
+
<summary>
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
10
|
+
in the Software without restriction, including without limitation the rights
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
13
|
+
furnished to do so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
16
|
+
copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24
|
+
SOFTWARE.
|
25
|
+
</summary>
|
26
|
+
--------------------------------------------------------------------------------------------------------------------
|
27
|
+
=end
|
28
|
+
|
29
|
+
module AsposeHtml
|
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
|
@@ -0,0 +1,177 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
=begin
|
3
|
+
--------------------------------------------------------------------------------------------------------------------
|
4
|
+
<copyright company="Aspose" file="configuration.rb">
|
5
|
+
</copyright>
|
6
|
+
Copyright (c) 2019 Aspose.HTML for Cloud
|
7
|
+
<summary>
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
10
|
+
in the Software without restriction, including without limitation the rights
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
13
|
+
furnished to do so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
16
|
+
copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24
|
+
SOFTWARE.
|
25
|
+
</summary>
|
26
|
+
--------------------------------------------------------------------------------------------------------------------
|
27
|
+
=end
|
28
|
+
|
29
|
+
require 'uri'
|
30
|
+
require 'tmpdir'
|
31
|
+
require 'json'
|
32
|
+
require 'typhoeus'
|
33
|
+
|
34
|
+
|
35
|
+
module AsposeHtml
|
36
|
+
class Configuration
|
37
|
+
|
38
|
+
|
39
|
+
# Defines the access token (Bearer) used with OAuth2.
|
40
|
+
attr_accessor :access_token
|
41
|
+
|
42
|
+
# Defines the logger used for debugging.
|
43
|
+
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
|
44
|
+
#
|
45
|
+
# @return [#debug]
|
46
|
+
attr_accessor :logger
|
47
|
+
|
48
|
+
# Defines the temporary folder to store downloaded files
|
49
|
+
# (for API endpoints that have file response).
|
50
|
+
# Default to use `Tempfile`.
|
51
|
+
#
|
52
|
+
# @return [String]
|
53
|
+
attr_accessor :temp_folder_path
|
54
|
+
|
55
|
+
# The time limit for HTTP request in seconds.
|
56
|
+
# Default to 0 (never times out).
|
57
|
+
attr_accessor :timeout
|
58
|
+
|
59
|
+
# Set this to false to skip client side validation in the operation.
|
60
|
+
# Default to true.
|
61
|
+
# @return [true, false]
|
62
|
+
attr_accessor :client_side_validation
|
63
|
+
|
64
|
+
### TLS/SSL setting
|
65
|
+
# Set this to false to skip verifying SSL certificate when calling API from https server.
|
66
|
+
# Default to true.
|
67
|
+
#
|
68
|
+
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
|
69
|
+
#
|
70
|
+
# @return [true, false]
|
71
|
+
attr_accessor :verify_ssl
|
72
|
+
|
73
|
+
### TLS/SSL setting
|
74
|
+
# Set this to false to skip verifying SSL host name
|
75
|
+
# Default to true.
|
76
|
+
#
|
77
|
+
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
|
78
|
+
#
|
79
|
+
# @return [true, false]
|
80
|
+
attr_accessor :verify_ssl_host
|
81
|
+
|
82
|
+
### TLS/SSL setting
|
83
|
+
# Set this to customize the certificate file to verify the peer.
|
84
|
+
#
|
85
|
+
# @return [String] the path to the certificate file
|
86
|
+
#
|
87
|
+
# @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
|
88
|
+
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
|
89
|
+
attr_accessor :ssl_ca_cert
|
90
|
+
|
91
|
+
### TLS/SSL setting
|
92
|
+
# Client certificate file (for client certificate)
|
93
|
+
attr_accessor :cert_file
|
94
|
+
|
95
|
+
### TLS/SSL setting
|
96
|
+
# Client private key file (for client certificate)
|
97
|
+
attr_accessor :key_file
|
98
|
+
|
99
|
+
# Set this to customize parameters encoding of array parameter with multi collectionFormat.
|
100
|
+
# Default to nil.
|
101
|
+
#
|
102
|
+
# @see The params_encoding option of Ethon. Related source code:
|
103
|
+
# https://github.com/typhoeus/ethon/blob/master/lib/ethon/easy/queryable.rb#L96
|
104
|
+
attr_accessor :params_encoding
|
105
|
+
|
106
|
+
attr_accessor :inject_format
|
107
|
+
|
108
|
+
attr_accessor :force_ending_format
|
109
|
+
|
110
|
+
def initialize(params)
|
111
|
+
self.class.class_eval {attr_accessor *params.keys}
|
112
|
+
params.each {|key,value| send("#{key}=",value)}
|
113
|
+
|
114
|
+
@timeout = 0
|
115
|
+
@temp_folder_path = Dir.tmpdir()
|
116
|
+
@access_token = req_token
|
117
|
+
@client_side_validation = true
|
118
|
+
@verify_ssl = false
|
119
|
+
@verify_ssl_host = false
|
120
|
+
@params_encoding = nil
|
121
|
+
@cert_file = nil
|
122
|
+
@key_file = nil
|
123
|
+
@inject_format = false
|
124
|
+
@force_ending_format = false
|
125
|
+
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
126
|
+
|
127
|
+
yield(self) if block_given?
|
128
|
+
end
|
129
|
+
|
130
|
+
# The default Configuration object.
|
131
|
+
def self.default(args)
|
132
|
+
@@default ||= Configuration.new(args)
|
133
|
+
end
|
134
|
+
|
135
|
+
def configure
|
136
|
+
yield(self) if block_given?
|
137
|
+
end
|
138
|
+
|
139
|
+
def base_url
|
140
|
+
url = @basePath
|
141
|
+
URI.encode(url)
|
142
|
+
end
|
143
|
+
|
144
|
+
# Request token
|
145
|
+
def req_token
|
146
|
+
tries = 0
|
147
|
+
begin
|
148
|
+
Typhoeus::Config.user_agent = "Aspose SDK"
|
149
|
+
tries += 1
|
150
|
+
response = Typhoeus::Request.new(@authPath,
|
151
|
+
method: :post,
|
152
|
+
body: {grant_type: "client_credentials",
|
153
|
+
client_id: @appSID,
|
154
|
+
client_secret: @apiKey
|
155
|
+
},
|
156
|
+
headers: {Accept: "application/json",
|
157
|
+
ContentType: "application/x-www-form-urlencoded"
|
158
|
+
},
|
159
|
+
ssl_verifyhost: 0,
|
160
|
+
ssl_verifypeer: false,
|
161
|
+
verbose: @debug
|
162
|
+
).run
|
163
|
+
hash = JSON.parse(response.body, symbolize_names: true)
|
164
|
+
hash[:access_token]
|
165
|
+
rescue => ex
|
166
|
+
puts "#{ex.class}: #{ex.message}"
|
167
|
+
puts "Try connect - #{tries}"
|
168
|
+
if (tries<5)
|
169
|
+
sleep(2**tries)
|
170
|
+
retry
|
171
|
+
end
|
172
|
+
raise "Can not connect to server #{@authPath}"
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|