Class: Tencent::Cloud::Client

Inherits:
Object
  • Object
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

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



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tencent/cloud/client.rb', line 50

def method_missing(symbol, *args)
  key = camel_case(symbol.to_s)
  # pp key, ::Tencent::Cloud.constants, ::Tencent::Cloud.const_defined?(key)
  super(symbol, *args) unless ::Tencent::Cloud.const_defined?(key)

  var_name = "@#{symbol}"
  instance_variable_get(var_name) || begin
    c = Tencent::Cloud.const_get(key).new region, secret_id, secret_key, options
    instance_variable_set(var_name, c)
    c
  end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



15
16
17
# File 'lib/tencent/cloud/client.rb', line 15

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/tencent/cloud/client.rb', line 15

def options
  @options
end

#regionObject (readonly)

Returns the value of attribute region.



15
16
17
# File 'lib/tencent/cloud/client.rb', line 15

def region
  @region
end

#schemaObject (readonly)

Returns the value of attribute schema.



15
16
17
# File 'lib/tencent/cloud/client.rb', line 15

def schema
  @schema
end

#secret_idObject (readonly)

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_keyObject (readonly)

Returns the value of attribute secret_key.



15
16
17
# File 'lib/tencent/cloud/client.rb', line 15

def secret_key
  @secret_key
end

#serviceObject (readonly)

Returns the value of attribute service.



15
16
17
# File 'lib/tencent/cloud/client.rb', line 15

def service
  @service
end

Instance Method Details

#algorithmObject



97
98
99
# File 'lib/tencent/cloud/client.rb', line 97

def algorithm
  'TC3-HMAC-SHA256'
end

#authorization(signature) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/tencent/cloud/client.rb', line 88

def authorization(signature)
  [
    algorithm, ' ',
    'Credential=', secret_id, '/', credential_scope, ', ',
    'SignedHeaders=', signed_headers, ', ',
    'Signature=', signature
  ].join
end

#camel_case(key) ⇒ Object



67
68
69
# File 'lib/tencent/cloud/client.rb', line 67

def camel_case(key)
  key.split('_').map(&:capitalize).join
end

#canonical_headersObject



109
110
111
112
113
114
# File 'lib/tencent/cloud/client.rb', line 109

def canonical_headers
  {
    'content-type' => 'application/json; charset=utf-8',
    'host' => host
  }.map { |p| p.join(':') }.join("\n") + "\n"
end

#connectionObject



32
33
34
35
36
37
38
39
40
# File 'lib/tencent/cloud/client.rb', line 32

def connection
  Faraday.new(url: "#{schema}://#{host}") do |conn|
    conn.request :retry
    # conn.request :url_encoded
    conn.response :logger
    conn.response :raise_error
    conn.adapter :net_http
  end
end

#credential_scopeObject



105
106
107
# File 'lib/tencent/cloud/client.rb', line 105

def credential_scope
  [sign_date, service, sign_version].join('/')
end

#hash_sha256(data) ⇒ Object



172
173
174
# File 'lib/tencent/cloud/client.rb', line 172

def hash_sha256(data)
  Digest::SHA256.hexdigest(data).downcase
end

#hashed_canonical_request(request_method, hashed_request_payload) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/tencent/cloud/client.rb', line 125

def hashed_canonical_request(request_method, hashed_request_payload)
  res = [
    request_method, '/', '',
    canonical_headers, signed_headers,
    hashed_request_payload
  ].join("\n")
  hash_sha256(res)
end

#hashed_request_payload(request_payload) ⇒ Object



120
121
122
123
# File 'lib/tencent/cloud/client.rb', line 120

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



168
169
170
# File 'lib/tencent/cloud/client.rb', line 168

def hex_encode(content)
  Digest.hexencode content
end

#hmac_sha256(key, data) ⇒ Object



176
177
178
# File 'lib/tencent/cloud/client.rb', line 176

def hmac_sha256(key, data)
  OpenSSL::HMAC.digest('SHA256', key, data)
end

#json(data) ⇒ Object



71
72
73
74
75
# File 'lib/tencent/cloud/client.rb', line 71

def json(data)
  return {} unless data && !data.empty?

  MultiJson.load data, symbolize_keys: true
end

#post(body = nil, headers = nil) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



42
43
44
45
46
47
48
# File 'lib/tencent/cloud/client.rb', line 42

def post(body = nil, headers = nil)
  yield(self) if block_given?
  params = body.is_a?(Hash) ? MultiJson.dump(body) : body
  Cloud.logger.debug { { body: body, headers: headers } }
  res = connection.post('/', params, public_headers(body, headers))
  json(res.body)
end

#public_headers(body, headers) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/tencent/cloud/client.rb', line 77

def public_headers(body, headers)
  datus = (headers || {}).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!(headers || {})
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/tencent/cloud/client.rb', line 63

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.end_with?('_api') || super
end

#secret_signingObject



157
158
159
160
161
# File 'lib/tencent/cloud/client.rb', line 157

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_dateObject



145
146
147
# File 'lib/tencent/cloud/client.rb', line 145

def sign_date
  sign_time.strftime '%F'
end

#sign_timeObject



149
150
151
# File 'lib/tencent/cloud/client.rb', line 149

def sign_time
  Time.now.getutc
end

#sign_timestampObject



153
154
155
# File 'lib/tencent/cloud/client.rb', line 153

def sign_timestamp
  sign_time.to_i.to_s
end

#sign_versionObject



101
102
103
# File 'lib/tencent/cloud/client.rb', line 101

def sign_version
  'tc3_request'
end

#signature(request_method, request_payload) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/tencent/cloud/client.rb', line 134

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

#signed_headersObject



116
117
118
# File 'lib/tencent/cloud/client.rb', line 116

def signed_headers
  %w[content-type host].join(';')
end

#string_to_sign(hashed_canonical_request) ⇒ Object



163
164
165
166
# File 'lib/tencent/cloud/client.rb', line 163

def string_to_sign(hashed_canonical_request)
  timestamp = sign_timestamp
  [algorithm, timestamp, credential_scope, hashed_canonical_request].join("\n")
end

#switch_to(service, host, schema: 'https') ⇒ Object



24
25
26
27
28
29
30
# File 'lib/tencent/cloud/client.rb', line 24

def switch_to(service, host, schema: 'https')
  Cloud.logger.debug { { schema: schema, service: service, host: host } }
  @service = service
  @host = host
  @schema = schema
  self
end