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

Constant Summary collapse

RETRYABLE_ERRORS =
[Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EHOSTUNREACH, Net::ReadTimeout]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ HttpClient

Returns a new instance of HttpClient.



15
16
17
18
19
20
# File 'lib/pact_broker/client/hal/http_client.rb', line 15

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.



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

def password
  @password
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

#verboseObject

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

#build_nested_query(value, prefix = nil) ⇒ Object

From Rack lib/rack/utils.rb



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/pact_broker/client/hal/http_client.rb', line 158

def build_nested_query(value, prefix = nil)
  case value
  when Array
    value.map { |v|
      build_nested_query(v, "#{prefix}[]")
    }.join("&")
  when Hash
    value.map { |k, v|
      build_nested_query(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k))
    }.delete_if(&:empty?).join('&')
  when nil
    prefix
  else
    raise ArgumentError, "value must be a Hash" if prefix.nil?
    "#{prefix}=#{escape(value)}"
  end
end

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pact_broker/client/hal/http_client.rb', line 49

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'].include?(http_method)
  request['Content-Type'] ||= "application/merge-patch+json" if ['Patch'].include?(http_method)
  request['Accept'] = "application/hal+json"
  request['Accept-Encoding'] = nil if verbose?
  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
  unless headers.any? { |k, _| k.downcase == 'user-agent' }
    request['User-Agent'] = PactBroker::Client.user_agent_string('net-http', Net::HTTP::VERSION)
  end
  request
end

#default_max_triesObject



124
125
126
# File 'lib/pact_broker/client/hal/http_client.rb', line 124

def default_max_tries
  5
end

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



44
45
46
47
# File 'lib/pact_broker/client/hal/http_client.rb', line 44

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

#disable_ssl_verification?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/pact_broker/client/hal/http_client.rb', line 153

def disable_ssl_verification?
  ENV['PACT_DISABLE_SSL_VERIFICATION'] == 'true' || ENV['PACT_BROKER_DISABLE_SSL_VERIFICATION'] == 'true'
end

#escape(s) ⇒ Object



176
177
178
# File 'lib/pact_broker/client/hal/http_client.rb', line 176

def escape(s)
  URI.encode_www_form_component(s)
end

#get(href, params = {}, headers = {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/pact_broker/client/hal/http_client.rb', line 22

def get href, params = {}, headers = {}
  query = build_nested_query(params)
  uri = URI(href)
  uri.query = query
  perform_request(create_request(uri, 'Get', nil, headers), uri)
end

#output_streamObject



132
133
134
# File 'lib/pact_broker/client/hal/http_client.rb', line 132

def output_stream
  AuthorizationHeaderRedactor.new($stdout)
end

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



39
40
41
42
# File 'lib/pact_broker/client/hal/http_client.rb', line 39

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

#perform_request(request, uri) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pact_broker/client/hal/http_client.rb', line 68

def perform_request request, uri
  response = until_truthy_or_max_times(condition: ->(resp) { resp.code.to_i < 500 }) do
    http = Net::HTTP.new(uri.host, uri.port, :ENV)
    http.set_debug_output(output_stream) if verbose?
    http.use_ssl = (uri.scheme == 'https')
    # Need to manually set the ca_file and ca_path for the pact-ruby-standalone.
    # The env vars seem to be picked up automatically in later Ruby versions.
    # See https://github.com/pact-foundation/pact-ruby-standalone/issues/57
    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?
        $stdout.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



34
35
36
37
# File 'lib/pact_broker/client/hal/http_client.rb', line 34

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



29
30
31
32
# File 'lib/pact_broker/client/hal/http_client.rb', line 29

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

#sleep(seconds) ⇒ Object



128
129
130
# File 'lib/pact_broker/client/hal/http_client.rb', line 128

def sleep seconds
  Kernel.sleep seconds
end

#until_truthy_or_max_times(options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pact_broker/client/hal/http_client.rb', line 97

def until_truthy_or_max_times options = {}
  max_tries = options.fetch(:times, default_max_tries)
  tries = 0
  sleep_interval = options.fetch(:sleep, 5)
  sleep(sleep_interval) if options[:sleep_first]
  while true
    begin
      result = yield
      return result if max_tries < 2
      if options[:condition]
        condition_result = options[:condition].call(result)
        return result if condition_result
      else
        return result if result
      end
      tries += 1
      return result if max_tries == tries
      sleep sleep_interval
    rescue *RETRYABLE_ERRORS => e
      tries += 1
      $stderr.puts "ERROR: Error making request - #{e.class} #{e.message} #{e.backtrace.find{|l| l.include?('pact_broker-client')}}, attempt #{tries} of #{max_tries}"
      raise e if max_tries == tries
      sleep sleep_interval
    end
  end
end

#verbose?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/pact_broker/client/hal/http_client.rb', line 136

def verbose?
  verbose || ENV["VERBOSE"] == "true"
end

#x509_certificate?Boolean

Returns:

  • (Boolean)


140
141
142
143
# File 'lib/pact_broker/client/hal/http_client.rb', line 140

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_fileObject



145
146
147
# File 'lib/pact_broker/client/hal/http_client.rb', line 145

def x509_client_cert_file
  File.read(ENV['X509_CLIENT_CERT_FILE'])
end

#x509_client_key_fileObject



149
150
151
# File 'lib/pact_broker/client/hal/http_client.rb', line 149

def x509_client_key_file
  File.read(ENV['X509_CLIENT_KEY_FILE'])
end