Class: Katello::HttpResource
Defined Under Namespace
Classes: NetworkException, RestClientException
Constant Summary
collapse
- REQUEST_MAP =
{
get: Net::HTTP::Get,
post: Net::HTTP::Post,
put: Net::HTTP::Put,
delete: Net::HTTP::Delete
}.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#filter_sensitive_data
Constructor Details
#initialize(json = {}) ⇒ HttpResource
Returns a new instance of HttpResource.
25
26
27
|
# File 'app/lib/katello/http_resource.rb', line 25
def initialize(json = {})
@json = json
end
|
Instance Attribute Details
#json ⇒ Object
Returns the value of attribute json.
23
24
25
|
# File 'app/lib/katello/http_resource.rb', line 23
def json
@json
end
|
Class Method Details
.hash_to_query(query_parameters) ⇒ Object
151
152
153
|
# File 'app/lib/katello/http_resource.rb', line 151
def hash_to_query(query_parameters)
"?#{URI.encode_www_form(query_parameters)}"
end
|
.issue_request(method:, path:, headers: {}, payload: nil) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'app/lib/katello/http_resource.rb', line 84
def issue_request(method:, path:, headers: {}, payload: nil)
logger.debug("Resource #{method.upcase} request: #{path}")
logger.debug "Headers: #{.to_json}"
logger.debug "Body: #{filter_sensitive_data(payload.to_json)}"
client = rest_client(REQUEST_MAP[method], method, path)
args = [method, payload, ].compact
process_response(client.send(*args))
rescue RestClient::Exception => e
raise_rest_client_exception e, path, method.upcase
rescue Errno::ECONNREFUSED
service = path.split("/").second
raise Errors::ConnectionRefusedException, _("A backend service [ %s ] is unreachable") % service.capitalize
end
|
.join_path(*args) ⇒ Object
107
108
109
110
111
112
|
# File 'app/lib/katello/http_resource.rb', line 107
def join_path(*args)
args.inject("") do |so_far, current|
so_far << '/' if (!so_far.empty? && so_far[so_far.length - 1].chr != '/') || current[0].chr != '/'
so_far << current.strip
end
end
|
.logger ⇒ Object
56
57
58
|
# File 'app/lib/katello/http_resource.rb', line 56
def logger
fail NotImplementedError
end
|
.process_response(resp) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'app/lib/katello/http_resource.rb', line 60
def process_response(resp)
logger.debug "Processing response: #{resp.code}"
logger.debug filter_sensitive_data(resp.body)
return resp unless resp.code.to_i >= 400
parsed = {}
message = "Rest exception while processing the call"
service_code = ""
status_code = resp.code.to_s
begin
parsed = JSON.parse resp.body
message = parsed["displayMessage"] if parsed["displayMessage"]
service_code = parsed["code"] if parsed["code"]
rescue => error
logger.error "Error parsing the body: " << error.backtrace.join("\n")
if %w(404 500 502 503 504).member? resp.code.to_s
logger.error "Remote server status code " << resp.code.to_s
raise RestClientException, {:message => error.to_s, :service_code => service_code, :code => status_code}, caller
else
raise NetworkException, [resp.code.to_s, resp.body].reject { |s| s.blank? }.join(' ')
end
end
fail RestClientException, {:message => message, :service_code => service_code, :code => status_code}, caller
end
|
.raise_rest_client_exception(e, a_path, http_method) ⇒ Object
re-raise the same exception with nicer error message
101
102
103
104
105
|
# File 'app/lib/katello/http_resource.rb', line 101
def raise_rest_client_exception(e, a_path, http_method)
msg = "#{name}: #{e.message} #{e.http_body} (#{http_method} #{a_path})"
e.message = msg
fail e
end
|
.rest_client(http_type, method, path) ⇒ Object
Creates a RestClient::Resource class with a signed OAuth style Authentication header added to the request headers.
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'app/lib/katello/http_resource.rb', line 116
def rest_client(http_type, method, path)
url = self.site + path
params = { :site => self.site,
:http_method => method,
:request_token_path => "",
:authorize_path => "",
:access_token_path => ""}
params[:ca_file] = self.ca_cert_file unless self.ca_cert_file.nil?
consumer = OAuth::Consumer.new(self.consumer_key,
self.consumer_secret,
params)
request = http_type.new(url)
consumer.sign!(request)
= {'Authorization' => request['Authorization']}
options = {
:headers => ,
:open_timeout => SETTINGS[:katello][:rest_client_timeout],
:timeout => SETTINGS[:katello][:rest_client_timeout]
}
options[:ssl_ca_file] = self.ca_cert_file unless self.ca_cert_file.nil?
options[:ssl_client_cert] = self.ssl_client_cert unless self.ssl_client_cert.nil?
options[:ssl_client_key] = self.ssl_client_key unless self.ssl_client_key.nil?
RestClient::Resource.new(url, options)
end
|
Instance Method Details
#[](key) ⇒ Object
29
30
31
|
# File 'app/lib/katello/http_resource.rb', line 29
def [](key)
@json[key]
end
|
#[]=(key, value) ⇒ Object
33
34
35
|
# File 'app/lib/katello/http_resource.rb', line 33
def []=(key, value)
@json[key] = value
end
|