Class: GarnetClient::Utils::HttpRequest
- Inherits:
-
Object
- Object
- GarnetClient::Utils::HttpRequest
- Defined in:
- lib/garnet_client/utils/http_request.rb
Constant Summary collapse
- DEFAULT_ERR_MSG =
'{ "status": {"code": 1, "message": "其他错误"} }'
Class Method Summary collapse
Class Method Details
.send_get(service_path) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/garnet_client/utils/http_request.rb', line 36 def self.send_get(service_path) api_url = "#{GarnetClient.api_base_url}/#{service_path}" headers = GarnetClient.response_headers response = HTTParty.get(api_url, :headers => headers) html_result = response.body html_content = '' if GarnetClient.debug_mode log_file = File.join(Rails.root, "log", "garnet_client.log") logger = Logger.new(log_file) logger.info('--------------GarnetClient DEBUG--------------') logger.info("URL:#{api_url.to_s}") logger.info("RESPONSE:#{html_result.force_encoding('UTF-8')}") end begin msg = JSON.parse(html_result) rescue JSON::ParserError => e html_content = html_result msg = JSON.parse(DEFAULT_ERR_MSG) end return msg, html_content end |
.send_post(service_path, query_params) ⇒ Object
发送请求
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/garnet_client/utils/http_request.rb', line 10 def self.send_post(service_path, query_params) api_url = "#{GarnetClient.api_base_url}/#{service_path}" headers = GarnetClient.response_headers response = HTTParty.post(api_url, :body =>JSON.dump(query_params), :headers => headers) html_result = response.body html_content = '' if GarnetClient.debug_mode log_file = File.join(Rails.root, "log", "garnet_client.log") logger = Logger.new(log_file) logger.info('--------------GarnetClient DEBUG--------------') logger.info("URL:#{api_url.to_s}") logger.info("PARAMS:#{query_params.to_s}") logger.info("RESPONSE:#{html_result.force_encoding('UTF-8')}") end begin msg = JSON.parse(html_result) rescue JSON::ParserError => e html_content = html_result msg = JSON.parse(DEFAULT_ERR_MSG) end return msg, html_content end |