class TencentCosSdk::Request

Attributes

body[RW]
file[RW]
headers[RW]
http_method[RW]
options[RW]
path[RW]
response[RW]
time_used[RW]

Public Class Methods

new(options) click to toggle source
# File lib/tencent_cos_sdk/request.rb, line 11
def initialize options
    self.options        = options

    self.http_method    = options[:http_method]
    self.path           = options[:path]
    self.headers        = options[:headers] || {}
    self.body           = options[:body]
    self.file           = options[:file]

    self.headers['Content-Type']  = 'application/octet-stream'  if http_method == 'put'
    self.headers['Authorization'] = get_authorization           if options[:sign]
end

Public Instance Methods

description() click to toggle source
# 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() click to toggle source
# File lib/tencent_cos_sdk/request.rb, line 24
def execute
    start_time = Time.now

    begin
        puts description

        options = {
            method:     http_method,
            url:        TencentCosSdk.url(path),
            headers:    headers,
            verify_ssl: false
        }

        options[:payload] = body            if body
        options[:payload] = IO.binread file if file

        self.response = RestClient::Request.execute options
    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

    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() click to toggle source
# File lib/tencent_cos_sdk/utils.rb, line 16
def get_authorization
    sign_time           = options[: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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source

NOTE: 暂不需要

# File lib/tencent_cos_sdk/utils.rb, line 50
def get_param_list
    ''
end
get_params() click to toggle source

NOTE: 暂不需要

# File lib/tencent_cos_sdk/utils.rb, line 45
def get_params
    ''
end
response_description() click to toggle source
# 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