Class: PactBroker::Client::Hal::HttpClient
- Inherits:
-
Object
- Object
- PactBroker::Client::Hal::HttpClient
- Defined in:
- lib/pact_broker/client/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
- #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
Constructor Details
#initialize(options) ⇒ HttpClient
Returns a new instance of HttpClient.
11 12 13 14 15 16 |
# File 'lib/pact_broker/client/hal/http_client.rb', line 11 def initialize @username = [:username] @password = [:password] @verbose = [:verbose] @token = [:token] end |
Instance Attribute Details
#password ⇒ Object
Returns the value of attribute password.
9 10 11 |
# File 'lib/pact_broker/client/hal/http_client.rb', line 9 def password @password end |
#token ⇒ Object
Returns the value of attribute token.
9 10 11 |
# File 'lib/pact_broker/client/hal/http_client.rb', line 9 def token @token end |
#username ⇒ Object
Returns the value of attribute username.
9 10 11 |
# File 'lib/pact_broker/client/hal/http_client.rb', line 9 def username @username end |
#verbose ⇒ Object
Returns the value of attribute verbose.
9 10 11 |
# File 'lib/pact_broker/client/hal/http_client.rb', line 9 def verbose @verbose end |
Instance Method Details
#create_request(uri, http_method, body = nil, headers = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pact_broker/client/hal/http_client.rb', line 35 def create_request uri, http_method, body = nil, headers = {} request = Net::HTTP.const_get(http_method).new(uri.request_uri) request['Content-Type'] = "application/json" if ['Post', 'Put', 'Patch'].include?(http_method) request['Accept'] = "application/hal+json" 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 |
#get(href, params = {}, headers = {}) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/pact_broker/client/hal/http_client.rb', line 18 def get href, params = {}, headers = {} query = params.collect{ |(key, value)| "#{CGI::escape(key)}=#{CGI::escape(value)}" }.join("&") uri = URI(href) uri.query = query perform_request(create_request(uri, 'Get', nil, headers), uri) end |
#output_stream ⇒ Object
61 62 63 |
# File 'lib/pact_broker/client/hal/http_client.rb', line 61 def output_stream AuthorizationHeaderRedactor.new($stdout) end |
#perform_request(request, uri) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/pact_broker/client/hal/http_client.rb', line 49 def perform_request request, uri response = Retry.until_truthy_or_max_times 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.start do |http| http.request request end end Response.new(response) end |
#post(href, body = nil, headers = {}) ⇒ Object
30 31 32 33 |
# File 'lib/pact_broker/client/hal/http_client.rb', line 30 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
25 26 27 28 |
# File 'lib/pact_broker/client/hal/http_client.rb', line 25 def put href, body = nil, headers = {} uri = URI(href) perform_request(create_request(uri, 'Put', body, headers), uri) end |