Class: PactBroker::Client::Hal::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/client/hal/http_client.rb

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ HttpClient

Returns a new instance of HttpClient.



12
13
14
15
16
17
# File 'lib/pact_broker/client/hal/http_client.rb', line 12

def initialize options
  @username = options[:username]
  @password = options[:password]
  @verbose = options[:verbose]
  @token = options[:token]
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/pact_broker/client/hal/http_client.rb', line 10

def password
  @password
end

#tokenObject

Returns the value of attribute token.



10
11
12
# File 'lib/pact_broker/client/hal/http_client.rb', line 10

def token
  @token
end

#usernameObject

Returns the value of attribute username.



10
11
12
# File 'lib/pact_broker/client/hal/http_client.rb', line 10

def username
  @username
end

#verboseObject

Returns the value of attribute verbose.



10
11
12
# File 'lib/pact_broker/client/hal/http_client.rb', line 10

def verbose
  @verbose
end

Instance Method Details

#create_request(uri, http_method, body = nil, headers = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pact_broker/client/hal/http_client.rb', line 41

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



19
20
21
22
23
24
# File 'lib/pact_broker/client/hal/http_client.rb', line 19

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_streamObject



67
68
69
# File 'lib/pact_broker/client/hal/http_client.rb', line 67

def output_stream
  AuthorizationHeaderRedactor.new($stdout)
end

#patch(href, body = nil, headers = {}) ⇒ Object



36
37
38
39
# File 'lib/pact_broker/client/hal/http_client.rb', line 36

def patch href, body = nil, headers = {}
  uri = URI(href)
  perform_request(create_request(uri, 'Patch', body, headers), uri)
end

#perform_request(request, uri) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pact_broker/client/hal/http_client.rb', line 55

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



31
32
33
34
# File 'lib/pact_broker/client/hal/http_client.rb', line 31

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



26
27
28
29
# File 'lib/pact_broker/client/hal/http_client.rb', line 26

def put href, body = nil, headers = {}
  uri = URI(href)
  perform_request(create_request(uri, 'Put', body, headers), uri)
end