Class: TencentCosSdk::Request
- Inherits:
-
Object
- Object
- TencentCosSdk::Request
- Defined in:
- lib/tencent_cos_sdk/utils.rb,
lib/tencent_cos_sdk/request.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#file ⇒ Object
Returns the value of attribute file.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#http_method ⇒ Object
Returns the value of attribute http_method.
-
#response ⇒ Object
Returns the value of attribute response.
-
#time_used ⇒ Object
Returns the value of attribute time_used.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
- #execute ⇒ Object
- #get_authorization ⇒ Object
- #get_header_list ⇒ Object
- #get_headers ⇒ Object
- #get_http_string ⇒ Object
-
#get_param_list ⇒ Object
NOTE: 暂不需要.
-
#get_params ⇒ Object
NOTE: 暂不需要.
-
#initialize(options) ⇒ Request
constructor
A new instance of Request.
Constructor Details
#initialize(options) ⇒ Request
Returns a new instance of Request.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/tencent_cos_sdk/request.rb', line 10 def initialize self.http_method = [:http_method] self.uri = [:uri] self.headers = [:headers] || {} self.body = [:body] self.file = [:file] self.headers['Content-Type'] = 'application/octet-stream' if http_method == 'put' self.headers['Authorization'] = if [:sign] end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
7 8 9 |
# File 'lib/tencent_cos_sdk/request.rb', line 7 def body @body end |
#file ⇒ Object
Returns the value of attribute file.
7 8 9 |
# File 'lib/tencent_cos_sdk/request.rb', line 7 def file @file end |
#headers ⇒ Object
Returns the value of attribute headers.
7 8 9 |
# File 'lib/tencent_cos_sdk/request.rb', line 7 def headers @headers end |
#http_method ⇒ Object
Returns the value of attribute http_method.
7 8 9 |
# File 'lib/tencent_cos_sdk/request.rb', line 7 def http_method @http_method end |
#response ⇒ Object
Returns the value of attribute response.
8 9 10 |
# File 'lib/tencent_cos_sdk/request.rb', line 8 def response @response end |
#time_used ⇒ Object
Returns the value of attribute time_used.
8 9 10 |
# File 'lib/tencent_cos_sdk/request.rb', line 8 def time_used @time_used end |
#uri ⇒ Object
Returns the value of attribute uri.
7 8 9 |
# File 'lib/tencent_cos_sdk/request.rb', line 7 def uri @uri end |
Instance Method Details
#execute ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tencent_cos_sdk/request.rb', line 21 def execute start_time = Time.now begin puts description = { method: http_method, url: "https://#{TencentCosSdk.conf.host}#{uri}", headers: headers, verify_ssl: false } [:payload] = body if body [:payload] = IO.binread file if file self.response = RestClient::Request.execute rescue => e puts e.backtrace.first + ": #{e.message} (#{e.class})" e.backtrace[1..-1].each { |m| puts "\tfrom #{m}" } raise e if !e.response self.response = e.response ensure end_time = Time.now self.time_used = (end_time - start_time) puts response_description end file_path = uri["#{TencentCosSdk.conf.parent_path}/".length..-1] m = /attachment; filename\*="UTF-8''(.+)"/.match response.headers[:content_disposition] FileUtils.mkdir_p File.dirname(file_path) IO.binwrite(file_path, response) if m response end |
#get_authorization ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tencent_cos_sdk/utils.rb', line 6 def sign_time = "#{Time.now.to_i - 3600};#{Time.now.to_i + 3600}" sign_key = OpenSSL::HMAC.hexdigest('sha1', TencentCosSdk.conf.secret_key, sign_time) http_string = get_http_string sha1ed_http_string = Digest::SHA1.hexdigest http_string string_to_sign = "sha1\n#{sign_time}\n#{sha1ed_http_string}\n" signature = OpenSSL::HMAC.hexdigest('sha1', sign_key, string_to_sign) { 'q-sign-algorithm' => 'sha1', 'q-ak' => TencentCosSdk.conf.secret_id, 'q-sign-time' => sign_time, 'q-key-time' => sign_time, 'q-header-list' => get_header_list, 'q-url-param-list' => get_param_list, 'q-signature' => signature }.map do |k, v| "#{k}=#{v}" end.join('&') end |
#get_header_list ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/tencent_cos_sdk/utils.rb', line 52 def get_header_list return '' if !headers headers.map do |k, v| k.downcase end.sort.join(';') end |
#get_headers ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/tencent_cos_sdk/utils.rb', line 44 def get_headers return '' if !headers headers.map do |k, v| "#{k.downcase}=#{CGI::escape(v)}" end.sort.join('&') end |
#get_http_string ⇒ Object
27 28 29 30 31 32 |
# File 'lib/tencent_cos_sdk/utils.rb', line 27 def get_http_string http_string = http_method + "\n" http_string += uri + "\n" http_string += get_params + "\n" http_string += get_headers + "\n" end |
#get_param_list ⇒ Object
NOTE: 暂不需要
40 41 42 |
# File 'lib/tencent_cos_sdk/utils.rb', line 40 def get_param_list '' end |
#get_params ⇒ Object
NOTE: 暂不需要
35 36 37 |
# File 'lib/tencent_cos_sdk/utils.rb', line 35 def get_params '' end |