Class: Lanmao::Http::Request
- Inherits:
-
Object
- Object
- Lanmao::Http::Request
- Defined in:
- lib/lanmao/http/request.rb
Instance Method Summary collapse
- #flow_id ⇒ Object
- #get_body ⇒ Object
- #identifier ⇒ Object
-
#initialize(params, config, service, type) ⇒ Request
constructor
A new instance of Request.
- #post ⇒ Object
Constructor Details
#initialize(params, config, service, type) ⇒ Request
Returns a new instance of Request.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/lanmao/http/request.rb', line 8 def initialize(params, config, service, type) @params = params @params[:timestamp] = Time.now.in_time_zone("Beijing").strftime('%Y%m%d%H%M%S') # 时间戳 @config = config @service = service @type = type @url = if :gateway == type # 网关模式 @config[:path] + "/gateway" elsif :service == type # 直连模式 @config[:path] + "/service" elsif :download == type # 对账文件下载模式 @config[:path] + "/download" else @config[:path] end @response = nil end |
Instance Method Details
#flow_id ⇒ Object
83 84 85 |
# File 'lib/lanmao/http/request.rb', line 83 def flow_id @params[:requestNo] || @params[:batchNo] end |
#get_body ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/lanmao/http/request.rb', line 28 def get_body Lanmao.logger.info "#{identifier} 请求内容为:\n#{@params.to_json}\n" sign_str = Sign::RSA.sign(@params.to_json, @config[:private_key]) body = { serviceName: @service, platformNo: @config[:platform_no], reqData: @params.to_json, keySerial: @config[:key_serial], sign: sign_str, } url = @url return body, url end |
#identifier ⇒ Object
87 88 89 |
# File 'lib/lanmao/http/request.rb', line 87 def identifier "[#{@service} - #{flow_id}] " end |
#post ⇒ Object
46 47 48 49 50 51 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 78 79 80 81 |
# File 'lib/lanmao/http/request.rb', line 46 def post # 1. api params Lanmao.logger.info "#{identifier} 请求内容为:\n#{@params.to_json}\n" # 2. sign sign_str = Sign::RSA.sign(@params.to_json, @config[:private_key]) # 3. merge sign post_body = { serviceName: @service, platformNo: @config[:platform_no], reqData: @params.to_json, keySerial: @config[:key_serial], sign: sign_str, } # 5. send http request Lanmao.logger.info "#{identifier} 发送的报文为:\n#{post_body}\n" http_response = RestClient.post(@url, post_body) Lanmao.logger.info "#{identifier} 返回的报文为:\n#{http_response.body.force_encoding('utf-8')}\n" if @type != :download # 直接返回,数据以字节流形式在 response body 中输出,无签名 return http_response.body if @type == :download # 6. create response @response = Lanmao::Http::Response.new(service: @service, flow_id: flow_id, http_response: http_response, raw_body: http_response.body, data: Utils.symbolize_keys(JSON.parse(http_response.body)), # data_valid: true data_valid: Sign::RSA.verify(http_response.body, http_response.headers[:sign], @config) ) end |