Module: HttpHelper
- Included in:
- TmcHelpers
- Defined in:
- lib/helpers/tmc_helpers/http_helper/http_helper.rb
Overview
IMPORTANT: This helper is used by the test wrapper, so it doesn’t always have TestCase context. See logger statements for example.
Defined Under Namespace
Classes: WebHttpResponse
Instance Method Summary collapse
-
#web_request(http_method, uri, body: nil, json: nil, form_data: nil, basic_auth: nil, headers: {}, timeout: nil, verify_mode: nil, screen_image: nil, use_ssl: nil) ⇒ Object
Public: Performs a web request for the given URI.
Instance Method Details
#web_request(http_method, uri, body: nil, json: nil, form_data: nil, basic_auth: nil, headers: {}, timeout: nil, verify_mode: nil, screen_image: nil, use_ssl: nil) ⇒ Object
Public: Performs a web request for the given URI.
http_method - Symbol HTTP method to use from :get, :post, :put, :delete. uri - String full URI for the resource to request. body - String data to populate the request body (default: nil). json - Hash JSON object to send in the request body (default: nil). By default no JSON will be included. form_data - Hash form data to send in the request (default: nil). By default no form data will be included. basic_auth - Hash of basic authentication :username and :password to use (default: nil). headers - Hash of headers to add to the request (default: {}). timeout - Integer total milliseconds to wait for a response (default: 5.min). verify_mode - Integer desired HTTP verify mode (default: nil).
Returns the Hash JSON response, or nil if there was an error.
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 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 |
# File 'lib/helpers/tmc_helpers/http_helper/http_helper.rb', line 22 def web_request(http_method, uri, body: nil, json: nil, form_data: nil, basic_auth: nil, headers: {}, timeout: nil, verify_mode: nil, screen_image: nil, use_ssl: nil) http_method = http_method.to_s.upcase user_headers = headers timeout ||= 5.min logger.debug("Request: #{http_method} #{uri}#{body.nil? ? '': " #{body}"}", screen_image: screen_image, tag: 'REQUEST') if respond_to?(:logger) logger.warn("`use_ssl' parameter is deprecated.") if !use_ssl.nil? && respond_to?(:logger) uri = URI.parse(uri) req = case http_method when 'POST' Net::HTTP::Post.new(uri.request_uri, user_headers) when 'DELETE' Net::HTTP::Delete.new(uri.request_uri, user_headers) when 'PUT' Net::HTTP::Put.new(uri.request_uri, user_headers) when 'GET' Net::HTTP::Get.new(uri.request_uri, user_headers) else raise "Unsupported HTTP method: #{http_method}" end if !body.nil? req.body = body elsif !json.nil? req.body = json.to_json req.add_field('Content-Type', 'application/json') end unless form_data.nil? req.set_form_data(form_data) end unless basic_auth.nil? req.basic_auth(basic_auth[:username], basic_auth[:password]) end http = Net::HTTP.new(uri.host, uri.port) http.read_timeout = (timeout / 1000.0).round http.ssl_version = 'SSLv23' http.use_ssl = uri.scheme == 'https' if !verify_mode.nil? && http.respond_to?(:verify_mode) http.verify_mode = verify_mode end start_time = Time.now response = http.request(req) delta_sec = (Time.now - start_time) logger.debug("Result: #{response.code} #{response.msg} [#{'%.3f' % delta_sec} s]", tag: 'REQUEST') if respond_to?(:logger) if response.is_a?(Net::HTTPRedirection) case response.code.to_i when 304 # cached, do nothing when 302, 307 # keep verb, data, and headers response = web_request(http_method, response['location'], body: body, json: json, form_data: form_data, basic_auth: basic_auth, headers: headers, timeout: timeout, verify_mode: verify_mode, screen_image: screen_image) else # drop verb, data, and headers response = web_request('GET', response['location'], timeout: timeout, screen_image: screen_image) end end WebHttpResponse.new(response) end |