Class: Aliyun::Opensearch::ClientExt::Traffic::Header

Inherits:
Object
  • Object
show all
Includes:
Credentials
Defined in:
lib/aliyun/opensearch/client_ext/traffic/header.rb,
lib/aliyun/opensearch/client_ext/traffic/header/credentials.rb

Overview

流量API HTTP请求header

Author:

Defined Under Namespace

Modules: Credentials

Constant Summary collapse

DEFAULT_CONTENT_TYPE =

默认 conten-type

'application/json'
DEFAULT_UA =

默认 user-agent, 内容参考阿里云官方openapi sdk: github.com/aliyun/openapi-core-ruby-sdk/blob/master/lib/aliyunsdkcore.rb

"AlibabaCloud (#{Gem::Platform.local.os}; #{Gem::Platform.local.cpu}) Ruby/#{RUBY_VERSION} Core/ALiyunOpensearchClient-#{Aliyun::Opensearch::VERSION}"
NONCE_SUFFIX_MIN =

nonce后缀最小长度

100_000
NONCE_SUFFIX_MAX =

nonce后缀最大长度

999_999

Constants included from Credentials

Credentials::SIGN_PREFIX

Instance Method Summary collapse

Methods included from Credentials

#authorization, #canonicalized_open_search_headers, #canonicalized_resource, #signature

Constructor Details

#initialize(request_method, path, api_params: nil, access_key_id: nil, access_key_secret: nil) ⇒ Header

创建实例

Parameters:

  • request_method (String)

    接口请求方法(GET POST…)

  • path (String)

    请求资源路径

  • api_params (Hash) (defaults to: nil)

    请求接口参数

  • access_key_id (String) (defaults to: nil)

    阿里云 AccessKeyId

  • access_key_secret (String) (defaults to: nil)

    阿里云 AccessKeySecret

Author:



41
42
43
44
45
46
47
48
49
# File 'lib/aliyun/opensearch/client_ext/traffic/header.rb', line 41

def initialize(request_method, path, api_params: nil, access_key_id: nil, access_key_secret: nil)
  @request_method = request_method.to_s.upcase
  @path = path

  @api_params = api_params

  @access_key_id = access_key_id || Configuration.access_key_id
  @access_key_secret = access_key_secret || Configuration.access_key_secret
end

Instance Method Details

#content_md5String

生成 header中 Content-MD5的值

Returns:

  • (String)

    Content-MD5的值

Author:



124
125
126
# File 'lib/aliyun/opensearch/client_ext/traffic/header.rb', line 124

def content_md5
  Digest::MD5.hexdigest(@api_params.to_json) if @request_method == 'POST' && @api_params
end

#content_typeString

hearder中 Content-Type的值

Returns:

  • (String)

    Content-Type的值

Author:



89
90
91
# File 'lib/aliyun/opensearch/client_ext/traffic/header.rb', line 89

def content_type
  @content_type ||= DEFAULT_CONTENT_TYPE
end

#dateString

生成 header中 Date的值

Returns:

  • (String)

    Date的值

Author:



113
114
115
# File 'lib/aliyun/opensearch/client_ext/traffic/header.rb', line 113

def date
  @date ||= time_now.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
end

#generateHash

生成Header的Hash结构

Returns:

  • (Hash)

    请求用到的所有Header内容

Author:



58
59
60
61
62
63
64
65
66
67
# File 'lib/aliyun/opensearch/client_ext/traffic/header.rb', line 58

def generate
  result = {
    'User-Agent' => DEFAULT_UA,
    'Content-Type' => content_type,
    'Date' => date,
  }.merge(x_opensearch_headers)
  result.merge!('Content-Md5' => content_md5) if content_md5
  result.merge!('Authorization' => authorization)
  result
end

#x_opensearch_headersHash

以X-Opensearch开头的header

Returns:

  • (Hash)

    以X-Opensearch开头的header

Author:



76
77
78
79
80
# File 'lib/aliyun/opensearch/client_ext/traffic/header.rb', line 76

def x_opensearch_headers
  {
    'X-Opensearch-Nonce' => x_opensearch_nonce,
  }
end

#x_opensearch_nonceString

生成 header中 X-Opensearch-Nonce的值

Returns:

  • (String)

    X-Opensearch-Nonce的值

See Also:

Author:



102
103
104
# File 'lib/aliyun/opensearch/client_ext/traffic/header.rb', line 102

def x_opensearch_nonce
  @x_opensearch_nonce ||= "#{time_now.to_i}#{rand(NONCE_SUFFIX_MIN..NONCE_SUFFIX_MAX)}"
end