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.
-
#options ⇒ Object
Returns the value of attribute options.
-
#path ⇒ Object
Returns the value of attribute path.
-
#response ⇒ Object
Returns the value of attribute response.
-
#time_used ⇒ Object
Returns the value of attribute time_used.
Instance Method Summary collapse
- #description ⇒ Object
- #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.
- #response_description ⇒ Object
Constructor Details
#initialize(options) ⇒ Request
Returns a new instance of Request.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/tencent_cos_sdk/request.rb', line 11 def initialize self. = self.http_method = [:http_method] self.path = [:path] 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 |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/tencent_cos_sdk/request.rb', line 9 def @options end |
#path ⇒ Object
Returns the value of attribute path.
7 8 9 |
# File 'lib/tencent_cos_sdk/request.rb', line 7 def path @path 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 |
Instance Method Details
#description ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/tencent_cos_sdk/request.rb', line 61 def description s = "\n\n" s << "#{http_method.upcase} #{TencentCosSdk.uri(@path)} HTTP/1.1\n".light_magenta headers.each do |k, v| s << "#{k}: #{v}\n".red end if headers s << "\n" s << body << "\n" if body return s end |
#execute ⇒ Object
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 58 59 |
# File 'lib/tencent_cos_sdk/request.rb', line 24 def execute start_time = Time.now begin puts description = { method: http_method, url: TencentCosSdk.url(path), 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.} (#{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 if /attachment; filename\*="UTF-8''(.+)"/.match response.headers[:content_disposition] FileUtils.mkdir_p File.dirname(path) IO.binwrite(path, response) end response end |
#get_authorization ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tencent_cos_sdk/utils.rb', line 16 def sign_time = [: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
62 63 64 65 66 67 68 |
# File 'lib/tencent_cos_sdk/utils.rb', line 62 def get_header_list return '' if !headers headers.map do |k, v| k.downcase end.sort.join(';') end |
#get_headers ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/tencent_cos_sdk/utils.rb', line 54 def get_headers return '' if !headers headers.map do |k, v| "#{k.downcase}=#{CGI::escape(v)}" end.sort.join('&') end |
#get_http_string ⇒ Object
37 38 39 40 41 42 |
# File 'lib/tencent_cos_sdk/utils.rb', line 37 def get_http_string http_string = http_method + "\n" http_string += TencentCosSdk.uri(@path) + "\n" http_string += get_params + "\n" http_string += get_headers + "\n" end |
#get_param_list ⇒ Object
NOTE: 暂不需要
50 51 52 |
# File 'lib/tencent_cos_sdk/utils.rb', line 50 def get_param_list '' end |
#get_params ⇒ Object
NOTE: 暂不需要
45 46 47 |
# File 'lib/tencent_cos_sdk/utils.rb', line 45 def get_params '' end |
#response_description ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/tencent_cos_sdk/request.rb', line 75 def response_description return nil if !response s = "%s | %.3f sec\n".light_magenta % [response.description.chomp, time_used] max_key_size = response.headers.max_by do |k, v| k.size end[0].size s << response.headers.map do |k, v| "%#{max_key_size}s".green % k + ': ' + v.green + "\n" end.join s << response.to_s if response.headers[:content_type] == 'application/xml' return s end |