Module: Lanmao::Api::CommunicateResultHelper

Defined in:
lib/lanmao/api/api_helper.rb

Instance Method Summary collapse

Instance Method Details

#download_post(service, params, type) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lanmao/api/api_helper.rb', line 52

def download_post(service, params, type)
  request = Http::Request.new(params, @config, service, type)
  response = request.post
  res = {
    result: 'P', # 默认
    request_params: params,
    response: response,
    flow_id: nil,
    code: nil,
    error_code: nil,
    error_msg: nil,
    data: nil,
  }
  result = JSON.parse(response).with_indifferent_access rescue {}
  if "INIT" == result[:status]
    res[:result] = "F"
    res[:code] = result[:code]
    res[:error_code] = result[:errorCode]
    res[:error_msg] = result[:errorMessage]
  else
    res[:result] = "S"
    res[:data] = response
  end

  res
end

#operate_post(request_type, service, params, type) ⇒ Object

操作类的 api,统一返回处理

Parameters:

  • request_type (Symbole)

    操作类(:operate)/查询类(:query)

  • service (String)

    上饶的服务

  • params (Hash)

    参数

  • type (Symbol)

    gateway => 网关模式, service => 直连模式



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lanmao/api/api_helper.rb', line 14

def operate_post(request_type, service, params, type)
  request = Http::Request.new(params, @config, service, type)
  response = request.post

  res = Lanmao::Utils.api_result(params, response)

  # 向服务器发送操作,超时类的都应该当 pending 处理
  if :operate == request_type
    if response.http_pending?
      Lanmao.logger.info "#{request.identifier} 最终返回的数据为:\n#{res}\n"
      return res
    end

    # 确定的错误
    # 调用状态(0为调用成功、1为失败,返回1时请看【error_code】及【error_msg】描述)
    if '1' == response.data[:code].to_s
      res[:result] = 'F'
      Lanmao.logger.info "#{request.identifier} 最终返回的数据为:\n#{res}\n"
      return res
    end

  # 查询类 api,http 没成功都返回 pending
  elsif :query == request_type
    unless response.http_success?
      Lanmao.logger.info "#{request.identifier} 最终返回的数据为:\n#{res}\n"
      return res
    end

  else
    raise '未知的请求类型,请选择设置:操作类(:operate)/查询类(:query)'
  end

  res[:result] = 'S' if 'SUCCESS' == response.data[:status] && '0' == response.data[:code].to_s # 业务是否成功标记位

  Lanmao.logger.info "#{request.identifier} 最终返回的数据为:\n#{res}\n"
  res
end