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



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)
  # pp key, ::Tencent::Cloud.constants, ::Tencent::Cloud.const_defined?(key)
  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

#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

#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



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=', signed_headers, ', ',
    '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

#canonical_headersObject



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

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

#connectionObject



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.request :url_encoded
    conn.response :logger
    conn.response :raise_error
    conn.adapter :net_http
  end
end

#credential_scopeObject



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, '/', '',
    canonical_headers, signed_headers,
    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

Yields:

  • (_self)

Yield Parameters:



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

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



70
71
72
73
74
75
76
77
78
79
# File 'lib/tencent/cloud/client.rb', line 70

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)


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_signingObject



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_dateObject



138
139
140
# File 'lib/tencent/cloud/client.rb', line 138

def sign_date
  sign_time.strftime '%F'
end

#sign_timeObject



142
143
144
# File 'lib/tencent/cloud/client.rb', line 142

def sign_time
  @sign_time ||= Time.now.getutc
end

#sign_timestampObject



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

def sign_timestamp
  sign_time.to_i.to_s
end

#sign_versionObject



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

#signed_headersObject



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

def signed_headers
  %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