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.



11
12
13
14
15
16
17
18
19
20
21
22
# 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

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

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
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

Instance Method Details

#descriptionObject



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

#executeObject



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

        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_authorizationObject



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



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_headersObject



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_stringObject



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_listObject

NOTE: 暂不需要



50
51
52
# File 'lib/tencent_cos_sdk/utils.rb', line 50

def get_param_list
    ''
end

#get_paramsObject

NOTE: 暂不需要



45
46
47
# File 'lib/tencent_cos_sdk/utils.rb', line 45

def get_params
    ''
end

#response_descriptionObject



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