Skip to content

Commit

Permalink
chore: update hal client code
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 24, 2018
1 parent 8c61b11 commit 7e82fd0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
8 changes: 8 additions & 0 deletions lib/pact/hal/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ def respond_to_missing?(method_name, include_private = false)
end

class ErrorEntity < Entity

def initialize(data, http_client, response = nil)
@data = data
@links = {}
@client = http_client
@response = response
end

def success?
false
end
Expand Down
15 changes: 11 additions & 4 deletions lib/pact/hal/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
module Pact
module Hal
class HttpClient
attr_accessor :username, :password
attr_accessor :username, :password, :verbose

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

def get href, params = {}, headers = {}
Expand Down Expand Up @@ -41,9 +42,11 @@ def create_request uri, http_method, body = nil, headers = {}
end

def perform_request request, uri
options = {:use_ssl => uri.scheme == 'https'}
response = Retry.until_true do
Net::HTTP.start(uri.host, uri.port, :ENV, options) do |http|
http = Net::HTTP.new(uri.host, uri.port, :ENV)
http.set_debug_output(Pact.configuration.output_stream) if verbose
http.use_ssl = (uri.scheme == 'https')
http.start do |http|
http.request request
end
end
Expand All @@ -52,14 +55,18 @@ def perform_request request, uri

class Response < SimpleDelegator
def body
bod = __getobj__().body
bod = raw_body
if bod && bod != ''
JSON.parse(bod)
else
nil
end
end

def raw_body
__getobj__().body
end

def success?
__getobj__().code.start_with?("2")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact/hal/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def wrap_response(http_response)
if http_response.success?
Entity.new(http_response.body, @http_client, http_response)
else
ErrorEntity.new(http_response.body, @http_client, http_response)
ErrorEntity.new(http_response.raw_body, @http_client, http_response)
end
end

Expand Down

0 comments on commit 7e82fd0

Please sign in to comment.