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.
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/pact_broker/client/base_client.rb', line 48
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'] != ''
@default_options = {}
@default_options[:verify] = false if (ENV['PACT_DISABLE_SSL_VERIFICATION'] == 'true' || ENV['PACT_BROKER_DISABLE_SSL_VERIFICATION'] == 'true')
end
|
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
46
47
48
|
# File 'lib/pact_broker/client/base_client.rb', line 46
def base_url
@base_url
end
|
#client_options ⇒ Object
Returns the value of attribute client_options.
46
47
48
|
# File 'lib/pact_broker/client/base_client.rb', line 46
def client_options
@client_options
end
|
Instance Method Details
66
67
68
|
# File 'lib/pact_broker/client/base_client.rb', line 66
def
end
|
70
71
72
|
# File 'lib/pact_broker/client/base_client.rb', line 70
def
.merge('Content-Type' => 'application/json')
end
|
74
75
76
|
# File 'lib/pact_broker/client/base_client.rb', line 74
def
.merge('Content-Type' => 'application/json')
end
|
62
63
64
|
# File 'lib/pact_broker/client/base_client.rb', line 62
def
{'Accept' => 'application/hal+json, application/json'}
end
|
#get(url, options = {}, &block) ⇒ Object
116
117
118
|
# File 'lib/pact_broker/client/base_client.rb', line 116
def get url, options = {}, &block
self.class.get(url, @default_options.merge(enrich_options(options)), &block)
end
|
#handle_response(response) ⇒ Object
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
103
104
105
|
# File 'lib/pact_broker/client/base_client.rb', line 78
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
107
108
109
110
|
# File 'lib/pact_broker/client/base_client.rb', line 107
def patch url, options
enriched = enrich_options(options)
self.class.patch(url, @default_options.merge(enriched.merge(body: options[:body].to_json)))
end
|
#put(url, options = {}, &block) ⇒ Object
112
113
114
|
# File 'lib/pact_broker/client/base_client.rb', line 112
def put url, options = {}, &block
self.class.put(url, @default_options.merge(enrich_options(options)), &block)
end
|
#url_for_relation(relation_name, params) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/pact_broker/client/base_client.rb', line 120
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. If you are using PactFlow, you may not have the permissions required for this action.")
end
end
end
|
#verbose? ⇒ Boolean
135
136
137
|
# File 'lib/pact_broker/client/base_client.rb', line 135
def verbose?
@verbose
end
|