class COS::HTTP

Constants

DEFAULT_CONTENT_TYPE

默认content-type

OPEN_TIMEOUT

请求创建超时

READ_TIMEOUT

响应读取超时

Attributes

config[R]
signature[R]

Public Class Methods

new(config) click to toggle source
# File lib/cos/http.rb, line 23
def initialize(config)
  @config    = config
  @signature = Signature.new(config)
end

Public Instance Methods

get(path, headers, signature = nil) click to toggle source

GET请求

# File lib/cos/http.rb, line 29
def get(path, headers, signature = nil)
  do_request('GET', path, headers, signature)
end
post(path, headers, signature, payload) click to toggle source

POST请求

# File lib/cos/http.rb, line 34
def post(path, headers, signature, payload)
  do_request('POST', path, headers, signature, payload)
end

Private Instance Methods

do_request(method, path, headers, signature = nil, payload = nil) click to toggle source

发送请求

# File lib/cos/http.rb, line 41
def do_request(method, path, headers, signature = nil, payload = nil)

  # 整理头部信息
  headers['content-type']  ||= DEFAULT_CONTENT_TYPE
  headers['user-agent']    = get_user_agent
  headers['authorization'] = signature
  headers['accept']        = 'application/json'

  # 请求地址
  url = "#{config.api_base}#{path}"

  logger.debug("Send HTTP request, method: #{method}, url: " \
                  "#{url}, headers: #{headers}, payload: #{payload}")

  response = RestClient::Request.execute(
      :method       => method,
      :url          => URI.encode(url),
      :headers      => headers,
      :payload      => payload,
      :open_timeout => @config.open_timeout || OPEN_TIMEOUT,
      :timeout      => @config.read_timeout || READ_TIMEOUT
  ) do |resp, request, result, &blk|

    # 捕获异常
    if resp.code >= 300
      e = ServerError.new(resp)
      logger.warn(e.to_s)
      raise e
    else
      resp.return!(request, result, &blk)
    end

  end

  logger.debug("Received HTTP response, code: #{response.code}, headers: " \
                  "#{response.headers}, body: #{response.body}")

  # 解析JSON结果
  parse_data(response)
end
get_user_agent() click to toggle source

获取user agent

# File lib/cos/http.rb, line 89
def get_user_agent
  "cos-ruby-sdk/#{VERSION} ruby-#{RUBY_VERSION}/#{RUBY_PLATFORM}"
end
parse_data(response) click to toggle source

解析结果json 取出data部分

# File lib/cos/http.rb, line 83
def parse_data(response)
  j = JSON.parse(response.body, symbolize_names: true)
  j[:data]
end