Class: TencentCloud::Common::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/tencent_cloud/common/base_client.rb

Direct Known Subclasses

NlpClient, OcrClient, TmsClient, TrtcClient

Instance Method Summary collapse

Constructor Details

#initialize(credential, region = nil) ⇒ BaseClient

Returns a new instance of BaseClient.



10
11
12
13
# File 'lib/tencent_cloud/common/base_client.rb', line 10

def initialize(credential, region = nil)
  @credential = credential
  @region = region
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, body) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tencent_cloud/common/base_client.rb', line 35

def method_missing(m, body)
  unless self.class::APIS.keys.include?(m)
    raise TencentCloud::Common::Exception::TencentCloudSDKException, 'InvalidMethod, method not found'
  end

  api = self.class::APIS[m]
  unless body.is_a?(Hash)
    raise TencentCloud::Common::Exception::TencentCloudSDKException, 'InvalidPayload, payload must be a hash'
  end

  get_response(api, body)
  # puts "There's no method called #{m} here -- please try again."
end

Instance Method Details

#camel_case(str) ⇒ Object



15
16
17
18
19
# File 'lib/tencent_cloud/common/base_client.rb', line 15

def camel_case(str)
  return str if str !~ /_/ && str =~ /[A-Z]+.*/

  str.split('_').map(&:capitalize).join
end

#get_response(action, body) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tencent_cloud/common/base_client.rb', line 21

def get_response(action, body)
  headers = {
    'X-TC-Action' => action,
    'X-TC-Version' => self.class::API_VERSION,
    'X-TC-Timestamp' => Time.now.to_i
  }
  headers['X-TC-Region'] = @region if @region
  request = TencentCloud::Common::Http::Request.new @credential,
                                                    self.class,
                                                    headers: headers,
                                                    body: JSON.generate(body, space: ' ')
  request.run
end