Class: Tencent::Cloud::Client
- Inherits:
-
Object
- Object
- Tencent::Cloud::Client
show all
- Defined in:
- lib/tencent/cloud/client.rb
Overview
create tencent cloud api client. request need different instance
> client = Tencent::Cloud.client.new ‘region’, ‘secret_id’, ‘secret_key’ > client.swith_to ‘cvm’, ‘cvm.tencentcloudapi.com’ # optional. for quick api call will use defualt > client.post json_text # text or hash, hash will use MultiJson.dump to convert
Direct Known Subclasses
SmsApi
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#algorithm ⇒ Object
-
#authorization(signature) ⇒ Object
-
#camel_case(key) ⇒ Object
-
#canonical_headers ⇒ Object
-
#connection ⇒ Object
-
#credential_scope ⇒ Object
-
#hash_sha256(data) ⇒ Object
-
#hashed_canonical_request(request_method, hashed_request_payload) ⇒ Object
-
#hashed_request_payload(request_payload) ⇒ Object
-
#hex_encode(content) ⇒ Object
-
#hmac_sha256(key, data) ⇒ Object
-
#initialize(region, secret_id, secret_key, **options) ⇒ Client
constructor
A new instance of Client.
-
#json(data) ⇒ Object
-
#method_missing(symbol, *args) ⇒ Object
-
#post(body = nil, headers = nil) {|_self| ... } ⇒ Object
-
#public_headers(body, headers) ⇒ Object
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#secret_signing ⇒ Object
-
#sign_date ⇒ Object
-
#sign_time ⇒ Object
-
#sign_timestamp ⇒ Object
-
#sign_version ⇒ Object
-
#signature(request_method, request_payload) ⇒ Object
-
#signed_headers ⇒ Object
-
#string_to_sign(hashed_canonical_request) ⇒ Object
-
#switch_to(service, host) ⇒ Object
Constructor Details
#initialize(region, secret_id, secret_key, **options) ⇒ Client
Returns a new instance of Client.
17
18
19
20
21
22
|
# File 'lib/tencent/cloud/client.rb', line 17
def initialize(region, secret_id, secret_key, **options)
@secret_id = secret_id
@region = region
@secret_key = secret_key
@options = options
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/tencent/cloud/client.rb', line 48
def method_missing(symbol, *args)
key = camel_case(symbol.to_s)
super(symbol, *args) unless ::Tencent::Cloud.const_defined?(key)
Tencent::Cloud.const_get(key).new region, secret_id, secret_key, options
end
|
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
15
16
17
|
# File 'lib/tencent/cloud/client.rb', line 15
def host
@host
end
|
#options ⇒ Object
Returns the value of attribute options.
15
16
17
|
# File 'lib/tencent/cloud/client.rb', line 15
def options
@options
end
|
#region ⇒ Object
Returns the value of attribute region.
15
16
17
|
# File 'lib/tencent/cloud/client.rb', line 15
def region
@region
end
|
#secret_id ⇒ Object
Returns the value of attribute secret_id.
15
16
17
|
# File 'lib/tencent/cloud/client.rb', line 15
def secret_id
@secret_id
end
|
#secret_key ⇒ Object
Returns the value of attribute secret_key.
15
16
17
|
# File 'lib/tencent/cloud/client.rb', line 15
def secret_key
@secret_key
end
|
#service ⇒ Object
Returns the value of attribute service.
15
16
17
|
# File 'lib/tencent/cloud/client.rb', line 15
def service
@service
end
|
Instance Method Details
#algorithm ⇒ Object
90
91
92
|
# File 'lib/tencent/cloud/client.rb', line 90
def algorithm
'TC3-HMAC-SHA256'
end
|
#authorization(signature) ⇒ Object
81
82
83
84
85
86
87
88
|
# File 'lib/tencent/cloud/client.rb', line 81
def authorization(signature)
[
algorithm, ' ',
'Credential=', secret_id, '/', credential_scope, ', ',
'SignedHeaders=', , ', ',
'Signature=', signature
].join
end
|
#camel_case(key) ⇒ Object
60
61
62
|
# File 'lib/tencent/cloud/client.rb', line 60
def camel_case(key)
key.split('_').map(&:capitalize).join
end
|
102
103
104
105
106
107
|
# File 'lib/tencent/cloud/client.rb', line 102
def
{
'content-type' => 'application/json; charset=utf-8',
'host' => host
}.map { |p| p.join(':') }.join("\n") + "\n"
end
|
#connection ⇒ Object
30
31
32
33
34
35
36
37
38
|
# File 'lib/tencent/cloud/client.rb', line 30
def connection
Faraday.new(url: "https://#{host}") do |conn|
conn.request :retry
conn.response :logger
conn.response :raise_error
conn.adapter :net_http
end
end
|
#credential_scope ⇒ Object
98
99
100
|
# File 'lib/tencent/cloud/client.rb', line 98
def credential_scope
[sign_date, service, sign_version].join('/')
end
|
#hash_sha256(data) ⇒ Object
165
166
167
|
# File 'lib/tencent/cloud/client.rb', line 165
def hash_sha256(data)
Digest::SHA256.hexdigest(data).downcase
end
|
#hashed_canonical_request(request_method, hashed_request_payload) ⇒ Object
118
119
120
121
122
123
124
125
|
# File 'lib/tencent/cloud/client.rb', line 118
def hashed_canonical_request(request_method, hashed_request_payload)
res = [
request_method, '/', '',
, ,
hashed_request_payload
].join("\n")
hash_sha256(res)
end
|
#hashed_request_payload(request_payload) ⇒ Object
113
114
115
116
|
# File 'lib/tencent/cloud/client.rb', line 113
def hashed_request_payload(request_payload)
request_payload = MultiJson.dump(request_payload) if request_payload.is_a?(Hash)
hash_sha256(request_payload)
end
|
#hex_encode(content) ⇒ Object
161
162
163
|
# File 'lib/tencent/cloud/client.rb', line 161
def hex_encode(content)
Digest.hexencode content
end
|
#hmac_sha256(key, data) ⇒ Object
169
170
171
|
# File 'lib/tencent/cloud/client.rb', line 169
def hmac_sha256(key, data)
OpenSSL::HMAC.digest('SHA256', key, data)
end
|
#json(data) ⇒ Object
64
65
66
67
68
|
# File 'lib/tencent/cloud/client.rb', line 64
def json(data)
return {} unless data && !data.empty?
MultiJson.load data, symbolize_keys: true
end
|
#post(body = nil, headers = nil) {|_self| ... } ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/tencent/cloud/client.rb', line 40
def post(body = nil, = nil)
yield(self) if block_given?
params = body.is_a?(Hash) ? MultiJson.dump(body) : body
Cloud.logger.debug { { body: body, headers: } }
res = connection.post('/', params, (body, ))
json(res.body)
end
|
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/tencent/cloud/client.rb', line 70
def (body, )
datus = ( || {}).dup
datus.merge!(body) if body.is_a?(Hash)
{
'Authorization' => authorization(signature('POST', body)),
'Content-Type' => 'application/json; charset=utf-8', 'Host' => host,
'X-TC-Action' => datus[:Action], 'X-TC-Timestamp' => sign_timestamp,
'X-TC-Version' => datus[:Version], 'X-TC-Region' => region
}.merge!( || {})
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
56
57
58
|
# File 'lib/tencent/cloud/client.rb', line 56
def respond_to_missing?(method_name, include_private = false)
method_name.to_s.end_with?('_api') || super
end
|
#secret_signing ⇒ Object
150
151
152
153
154
|
# File 'lib/tencent/cloud/client.rb', line 150
def secret_signing
secret_date = hmac_sha256('TC3' + secret_key, sign_date)
secret_service = hmac_sha256(secret_date, service)
hmac_sha256(secret_service, sign_version)
end
|
#sign_date ⇒ Object
138
139
140
|
# File 'lib/tencent/cloud/client.rb', line 138
def sign_date
sign_time.strftime '%F'
end
|
#sign_time ⇒ Object
142
143
144
|
# File 'lib/tencent/cloud/client.rb', line 142
def sign_time
@sign_time ||= Time.now.getutc
end
|
#sign_timestamp ⇒ Object
146
147
148
|
# File 'lib/tencent/cloud/client.rb', line 146
def sign_timestamp
sign_time.to_i.to_s
end
|
#sign_version ⇒ Object
94
95
96
|
# File 'lib/tencent/cloud/client.rb', line 94
def sign_version
'tc3_request'
end
|
#signature(request_method, request_payload) ⇒ Object
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/tencent/cloud/client.rb', line 127
def signature(request_method, request_payload)
hashed = hashed_request_payload(request_payload)
data = hashed_canonical_request(request_method, hashed)
content =
hmac_sha256(
secret_signing,
string_to_sign(data)
)
hex_encode(content)
end
|
109
110
111
|
# File 'lib/tencent/cloud/client.rb', line 109
def
%w[content-type host].join(';')
end
|
#string_to_sign(hashed_canonical_request) ⇒ Object
156
157
158
159
|
# File 'lib/tencent/cloud/client.rb', line 156
def string_to_sign(hashed_canonical_request)
timestamp = sign_timestamp
[algorithm, timestamp, credential_scope, hashed_canonical_request].join("\n")
end
|
#switch_to(service, host) ⇒ Object
24
25
26
27
28
|
# File 'lib/tencent/cloud/client.rb', line 24
def switch_to(service, host)
Cloud.logger.debug { {service: service, host: host} }
@service = service
@host = host
end
|