class Pact::Hal::HttpClient

Attributes

password[RW]
token[RW]
username[RW]
verbose[RW]

Public Class Methods

new(options) click to toggle source
# File lib/pact/hal/http_client.rb, line 12
def initialize options
  @username = options[:username]
  @password = options[:password]
  @verbose = options[:verbose]
  @token = options[:token]
end

Public Instance Methods

create_request(uri, http_method, body = nil, headers = {}) click to toggle source
# File lib/pact/hal/http_client.rb, line 38
def create_request uri, http_method, body = nil, headers = {}
  request = Net::HTTP.const_get(http_method).new(uri.request_uri)
  headers.each do | key, value |
    request[key] = value
  end
  request.body = body if body
  request.basic_auth username, password if username
  request['Authorization'] = "Bearer #{token}" if token
  request
end
disable_ssl_verification?() click to toggle source
# File lib/pact/hal/http_client.rb, line 77
def disable_ssl_verification?
  ENV['PACT_DISABLE_SSL_VERIFICATION'] == 'true' || ENV['PACT_BROKER_DISABLE_SSL_VERIFICATION'] == 'true'
end
get(href, params = {}) click to toggle source
# File lib/pact/hal/http_client.rb, line 19
def get href, params = {}, headers = {}
  uri = URI(href)
  if params && params.any?
    existing_params = Rack::Utils.parse_nested_query(uri.query)
    uri.query = Rack::Utils.build_nested_query(existing_params.merge(params))
  end
  perform_request(create_request(uri, 'Get', nil, headers), uri)
end
output_stream() click to toggle source
# File lib/pact/hal/http_client.rb, line 69
def output_stream
  AuthorizationHeaderRedactor.new(Pact.configuration.output_stream)
end
perform_request(request, uri) click to toggle source
# File lib/pact/hal/http_client.rb, line 49
def perform_request request, uri
  response = Retry.until_true do
    http = Net::HTTP.new(uri.host, uri.port, :ENV)
    http.set_debug_output(output_stream) if verbose?
    http.use_ssl = (uri.scheme == 'https')
    http.ca_file = ENV['SSL_CERT_FILE'] if ENV['SSL_CERT_FILE'] && ENV['SSL_CERT_FILE'] != ''
    http.ca_path = ENV['SSL_CERT_DIR'] if ENV['SSL_CERT_DIR'] && ENV['SSL_CERT_DIR'] != ''
    if disable_ssl_verification?
      if verbose?
        Pact.configuration.output_stream.puts("SSL verification is disabled")
      end
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
    http.start do |http|
      http.request request
    end
  end
  Response.new(response)
end
post(href, body = nil, headers = {}) click to toggle source
# File lib/pact/hal/http_client.rb, line 33
def post href, body = nil, headers = {}
  uri = URI(href)
  perform_request(create_request(uri, 'Post', body, headers), uri)
end
put(href, body = nil, headers = {}) click to toggle source
# File lib/pact/hal/http_client.rb, line 28
def put href, body = nil, headers = {}
  uri = URI(href)
  perform_request(create_request(uri, 'Put', body, headers), uri)
end
verbose?() click to toggle source
# File lib/pact/hal/http_client.rb, line 73
def verbose?
  verbose || ENV['VERBOSE'] == 'true'
end