Module: KuaiJieQian::Http

Defined in:
lib/kuai_jie_qian/http.rb

Class Method Summary collapse

Class Method Details

.post(host, project_id, path, params, content_type = 'application/json') ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kuai_jie_qian/http.rb', line 6

def self.post(host, project_id, path, params, content_type='application/json')
  final_url = "#{host}/#{path}"
  header = {
    "X-timevale-mode" => "package",
    "X-timevale-project-id" => project_id,
    "X-timevale-signature-algorithm" => "hmac-sha256",
  }.merge({'Content-Type' => content_type})

  if "application/json" == content_type
    request_body = params.to_json
  else
    request_body = params
  end

  # KuaiJieQian.logger.info "post [#{final_url}] with header: [#{header}], params: [#{params}]"

  response = RestClient.post(final_url, request_body, header)

  # KuaiJieQian.logger.info "post [#{final_url}] got result: [#{response}]\n"

  begin
    res = Utils.symbolize_keys(JSON.parse(response))
  rescue
    raise "post [#{final_url}] 解析结果出错"
  end

  # KuaiJieQian.logger.debug "res: [#{res}]\n"
  res
end