Class: TencentCloud::TpnsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/tencent_cloud/tpns/v20210422/tpns_client.rb

Constant Summary collapse

API_VERSION =
'2021-04-22'
ENDPOINTS =
{
  'gz' => 'https://api.tpns.tencent.com',
  'sh' => 'https://api.tpns.sh.tencent.com',
  'hk' => 'https://api.tpns.hk.tencent.com',
  'sg' => 'https://api.tpns.sgp.tencent.com'
}

Instance Method Summary collapse

Constructor Details

#initialize(access_id, secret_key, region) ⇒ TpnsClient

Returns a new instance of TpnsClient.



18
19
20
21
22
# File 'lib/tencent_cloud/tpns/v20210422/tpns_client.rb', line 18

def initialize(access_id, secret_key, region)
  @access_id = access_id
  @secret_key = secret_key
  @end_point = ENDPOINTS[region.to_s]
end

Instance Method Details

#http_request(api_path, data) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tencent_cloud/tpns/v20210422/tpns_client.rb', line 24

def http_request(api_path, data)
  current_timestamp = Time.current.to_i.to_s
  uri = URI.parse("#{@end_point}#{api_path}")

  request = Net::HTTP::Post.new(uri.to_s)
  request.body = data.to_json
  request.initialize_http_header({AccessId: @access_id, TimeStamp: current_timestamp, Sign: sign(current_timestamp, request.body)})
  request.set_content_type 'application/json'

  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request request
  end
end

#sign(current_timestamp, body) ⇒ Object



38
39
40
# File 'lib/tencent_cloud/tpns/v20210422/tpns_client.rb', line 38

def sign(current_timestamp, body)
  Base64.strict_encode64(OpenSSL::HMAC.hexdigest('sha256', @secret_key, "#{current_timestamp}#{@access_id}#{body}"))
end