Class: Pact::Hal::HttpClient
- Inherits:
-
Object
- Object
- Pact::Hal::HttpClient
- Defined in:
- lib/pact/hal/http_client.rb
Defined Under Namespace
Classes: Response
Instance Attribute Summary collapse
-
#password ⇒ Object
Returns the value of attribute password.
-
#token ⇒ Object
Returns the value of attribute token.
-
#username ⇒ Object
Returns the value of attribute username.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #create_request(uri, http_method, body = nil, headers = {}) ⇒ Object
- #disable_ssl_verification? ⇒ Boolean
- #get(href, params = {}, headers = {}) ⇒ Object
-
#initialize(options) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #output_stream ⇒ Object
- #perform_request(request, uri) ⇒ Object
- #post(href, body = nil, headers = {}) ⇒ Object
- #put(href, body = nil, headers = {}) ⇒ Object
- #verbose? ⇒ Boolean
- #x509_certificate? ⇒ Boolean
- #x509_client_cert_file ⇒ Object
- #x509_client_key_file ⇒ Object
Constructor Details
#initialize(options) ⇒ HttpClient
Returns a new instance of HttpClient.
12 13 14 15 16 17 |
# File 'lib/pact/hal/http_client.rb', line 12 def initialize @username = [:username] @password = [:password] @verbose = [:verbose] @token = [:token] end |
Instance Attribute Details
#password ⇒ Object
Returns the value of attribute password.
10 11 12 |
# File 'lib/pact/hal/http_client.rb', line 10 def password @password end |
#token ⇒ Object
Returns the value of attribute token.
10 11 12 |
# File 'lib/pact/hal/http_client.rb', line 10 def token @token end |
#username ⇒ Object
Returns the value of attribute username.
10 11 12 |
# File 'lib/pact/hal/http_client.rb', line 10 def username @username end |
#verbose ⇒ Object
Returns the value of attribute verbose.
10 11 12 |
# File 'lib/pact/hal/http_client.rb', line 10 def verbose @verbose end |
Instance Method Details
#create_request(uri, http_method, body = nil, headers = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# 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? ⇒ Boolean
96 97 98 |
# File 'lib/pact/hal/http_client.rb', line 96 def disable_ssl_verification? ENV['PACT_DISABLE_SSL_VERIFICATION'] == 'true' || ENV['PACT_BROKER_DISABLE_SSL_VERIFICATION'] == 'true' end |
#get(href, params = {}, headers = {}) ⇒ Object
19 20 21 22 23 24 25 26 |
# 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 ⇒ Object
75 76 77 |
# File 'lib/pact/hal/http_client.rb', line 75 def output_stream AuthorizationHeaderRedactor.new(Pact.configuration.output_stream) end |
#perform_request(request, uri) ⇒ Object
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 |
# 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 x509_certificate? http.cert = OpenSSL::X509::Certificate.new(x509_client_cert_file) http.key = OpenSSL::PKey::RSA.new(x509_client_key_file) end 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 = {}) ⇒ Object
33 34 35 36 |
# 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 = {}) ⇒ Object
28 29 30 31 |
# 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? ⇒ Boolean
79 80 81 |
# File 'lib/pact/hal/http_client.rb', line 79 def verbose? verbose || ENV['VERBOSE'] == 'true' end |
#x509_certificate? ⇒ Boolean
83 84 85 86 |
# File 'lib/pact/hal/http_client.rb', line 83 def x509_certificate? ENV['X509_CLIENT_CERT_FILE'] && ENV['X509_CLIENT_CERT_FILE'] != '' && ENV['X509_CLIENT_KEY_FILE'] && ENV['X509_CLIENT_KEY_FILE'] != '' end |
#x509_client_cert_file ⇒ Object
88 89 90 |
# File 'lib/pact/hal/http_client.rb', line 88 def x509_client_cert_file File.read(ENV['X509_CLIENT_CERT_FILE']) end |
#x509_client_key_file ⇒ Object
92 93 94 |
# File 'lib/pact/hal/http_client.rb', line 92 def x509_client_key_file File.read(ENV['X509_CLIENT_KEY_FILE']) end |