Class: TencentCosSdk::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/tencent_cos_sdk/utils.rb,
lib/tencent_cos_sdk/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 options
    self.http_method    = options[:http_method]
    self.uri            = options[:uri]
    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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



7
8
9
# File 'lib/tencent_cos_sdk/request.rb', line 7

def body
  @body
end

#fileObject

Returns the value of attribute file.



7
8
9
# File 'lib/tencent_cos_sdk/request.rb', line 7

def file
  @file
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/tencent_cos_sdk/request.rb', line 7

def headers
  @headers
end

#http_methodObject

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

#responseObject

Returns the value of attribute response.



8
9
10
# File 'lib/tencent_cos_sdk/request.rb', line 8

def response
  @response
end

#time_usedObject

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

#uriObject

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

#executeObject



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

        options = {
            method:     http_method,
            url:        "https://#{TencentCosSdk.conf.host}#{uri}",
            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

    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_authorizationObject



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 get_authorization
    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_listObject



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_headersObject



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_stringObject



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_listObject

NOTE: 暂不需要



40
41
42
# File 'lib/tencent_cos_sdk/utils.rb', line 40

def get_param_list
    ''
end

#get_paramsObject

NOTE: 暂不需要



35
36
37
# File 'lib/tencent_cos_sdk/utils.rb', line 35

def get_params
    ''
end