11# frozen_string_literal: true
22
3- require ' faraday'
4- require ' faraday/multipart'
5- require ' json'
3+ require " faraday"
4+ require " faraday/multipart"
5+ require " json"
66
77class JsonRequester
88 attr_reader :host , :conn
99
10- def initialize ( host , multipart : false , ssl_verify : true , timeout : 60 , user_agent : '' )
10+ def initialize ( host , multipart : false , ssl_verify : true , timeout : 60 , user_agent : "" )
1111 @host = host
1212 @multipart = multipart
1313 @ssl_verify = ssl_verify
1414 @timeout = timeout
1515 @user_agent = user_agent . strip . to_s
1616 end
1717
18- def http_send ( http_method , path , params = { } , headers = { } , sort_params : true , need_response_header : false , content_type_charset : ' utf-8' )
18+ def http_send ( http_method , path , params = { } , headers = { } , sort_params : true , need_response_header : false , content_type_charset : " utf-8" )
1919 puts "send #{ http_method } request to #{ @host } with\n path: #{ path } \n params: #{ params } \n headers: #{ headers } "
2020 if http_method == :get
2121 normal_send ( http_method , path , params , headers , sort_params : sort_params , need_response_header : need_response_header )
@@ -36,12 +36,12 @@ def normal_send(http_method, path, params = {}, headers = {}, sort_params: true,
3636 error_response ( e )
3737 end
3838
39- def json_send ( http_method , path , params = { } , headers = { } , sort_params : true , need_response_header : false , content_type_charset : ' utf-8' )
39+ def json_send ( http_method , path , params = { } , headers = { } , sort_params : true , need_response_header : false , content_type_charset : " utf-8" )
4040 conn = init_conn ( sort_params : sort_params )
4141 res = conn . send ( http_method ) do |req |
4242 req . url path
4343 req . headers = headers if object_present? ( headers )
44- req . headers [ ' Content-Type' ] = object_present? ( content_type_charset ) ? "application/json;charset=#{ content_type_charset } " : ' application/json'
44+ req . headers [ " Content-Type" ] = object_present? ( content_type_charset ) ? "application/json;charset=#{ content_type_charset } " : " application/json"
4545 req . body = params . to_json if object_present? ( params )
4646 end
4747 process_response ( res , need_response_header : need_response_header )
@@ -54,7 +54,7 @@ def form_send(http_method, path, params = {}, headers = {}, sort_params: true, n
5454 res = conn . send ( http_method ) do |req |
5555 req . url path
5656 req . headers = headers if object_present? ( headers )
57- req . headers [ ' Content-Type' ] = ' application/x-www-form-urlencoded;charset=utf-8'
57+ req . headers [ " Content-Type" ] = " application/x-www-form-urlencoded;charset=utf-8"
5858 req . body = URI . encode_www_form ( params ) if object_present? ( params )
5959 end
6060 process_response ( res , need_response_header : need_response_header )
@@ -93,21 +93,21 @@ def init_conn(sort_params: true)
9393 end
9494
9595 def process_response ( response , need_response_header : false )
96- result = { ' status' => response . status }
96+ result = { " status" => response . status }
9797 begin
9898 body = JSON . parse ( response . body )
99- body = { ' body' => body } unless body . is_a? ( Hash )
100- body [ ' body_status' ] = body . delete ( ' status' ) unless body [ ' status' ] . nil?
99+ body = { " body" => body } unless body . is_a? ( Hash )
100+ body [ " body_status" ] = body . delete ( " status" ) unless body [ " status" ] . nil?
101101 rescue StandardError
102- body = { ' body' => response . body }
102+ body = { " body" => response . body }
103103 end
104104 result . merge! ( body )
105- result [ ' headers' ] = response . headers . to_h if need_response_header
105+ result [ " headers" ] = response . headers . to_h if need_response_header
106106 result
107107 end
108108
109109 def error_response ( err )
110- { ' status' => 500 , ' message' => "#{ err . class . name } : #{ err . message } " }
110+ { " status" => 500 , " message" => "#{ err . class . name } : #{ err . message } " }
111111 end
112112
113113 def object_present? ( object )
0 commit comments