Class: PactBroker::Client::BaseClient
- Inherits:
-
Object
- Object
- PactBroker::Client::BaseClient
show all
- Includes:
- HTTParty, StringToSymbol, UrlHelpers
- Defined in:
- lib/pact_broker/client/base_client.rb
Constant Summary
collapse
- ERROR_CODE_MAPPING =
{
401 => "Authentication failed",
403 => "Authorization failed (insufficient permissions)",
409 => "Potential duplicate pacticipants"
}.freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
#string_keys_to_symbols
Methods included from UrlHelpers
#encode_param, #encode_query_param
Constructor Details
#initialize(options) ⇒ BaseClient
Returns a new instance of BaseClient.
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/pact_broker/client/base_client.rb', line 47
def initialize options
@base_url = options[:base_url]
@client_options = options[:client_options] || {}
@verbose = @client_options[:verbose]
self.class.base_uri base_url
self.class.debug_output($stderr) if verbose?
self.class.basic_auth(client_options[:basic_auth][:username], client_options[:basic_auth][:password]) if client_options[:basic_auth]
self.class.('Authorization' => "Bearer #{client_options[:token]}") if client_options[:token]
self.class.ssl_ca_file(ENV['SSL_CERT_FILE']) if ENV['SSL_CERT_FILE'] && ENV['SSL_CERT_FILE'] != ''
self.class.ssl_ca_path(ENV['SSL_CERT_DIR']) if ENV['SSL_CERT_DIR'] && ENV['SSL_CERT_DIR'] != ''
end
|
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
45
46
47
|
# File 'lib/pact_broker/client/base_client.rb', line 45
def base_url
@base_url
end
|
#client_options ⇒ Object
Returns the value of attribute client_options.
45
46
47
|
# File 'lib/pact_broker/client/base_client.rb', line 45
def client_options
@client_options
end
|
Instance Method Details
63
64
65
|
# File 'lib/pact_broker/client/base_client.rb', line 63
def
end
|
67
68
69
|
# File 'lib/pact_broker/client/base_client.rb', line 67
def
.merge('Content-Type' => 'application/json')
end
|
71
72
73
|
# File 'lib/pact_broker/client/base_client.rb', line 71
def
.merge('Content-Type' => 'application/json')
end
|
59
60
61
|
# File 'lib/pact_broker/client/base_client.rb', line 59
def
{'Accept' => 'application/hal+json, application/json'}
end
|
#get(url, *args) ⇒ Object
112
113
114
|
# File 'lib/pact_broker/client/base_client.rb', line 112
def get url, *args
self.class.get(url, *args)
end
|
#handle_response(response) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/pact_broker/client/base_client.rb', line 75
def handle_response response
if response.success?
yield response
elsif response.code == 404
nil
elsif ERROR_CODE_MAPPING.key?(response.code)
message = ERROR_CODE_MAPPING.fetch(response.code)
if response.body && response.body.size > 0
message = message + ": #{response.body}"
end
raise Error.new(message)
else
error_message = nil
begin
errors = JSON.parse(response.body)['errors']
error_message = if errors.is_a?(Array)
errors.join("\n")
elsif errors.is_a?(Hash)
errors.collect{ |key, value| "#{key}: #{value}" }.join("\n")
else
response.body
end
rescue
raise Error.new("status=#{response.code} #{response.body}")
end
raise Error.new(error_message)
end
end
|
#patch(url, options) ⇒ Object
104
105
106
|
# File 'lib/pact_broker/client/base_client.rb', line 104
def patch url, options
self.class.patch(url, options.merge(body: options[:body].to_json))
end
|
#put(url, *args) ⇒ Object
108
109
110
|
# File 'lib/pact_broker/client/base_client.rb', line 108
def put url, *args
self.class.put(url, *args)
end
|
#url_for_relation(relation_name, params) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/pact_broker/client/base_client.rb', line 116
def url_for_relation relation_name, params
handle_response(get("/", headers: )) do | response |
relation = (JSON.parse(response.body)['_links'] || {})[relation_name]
if relation
url = relation['href']
params.each do | (key, value) |
url = url.gsub("{#{key}}", encode_param(value))
end
url
else
raise PactBroker::Client::RelationNotFound.new("Could not find relation #{relation_name} in index resource. Try upgrading your Pact Broker as the feature you require may not exist in your version.")
end
end
end
|
#verbose? ⇒ Boolean
131
132
133
|
# File 'lib/pact_broker/client/base_client.rb', line 131
def verbose?
@verbose
end
|