Class: Katello::HttpResource
- Inherits:
-
Object
- Object
- Katello::HttpResource
- Includes:
- Concerns::FilterSensitiveData
- Defined in:
- app/lib/katello/http_resource.rb
Direct Known Subclasses
Resources::Candlepin::CandlepinResource, Resources::Registry::RegistryResource
Defined Under Namespace
Classes: NetworkException, RestClientException
Constant Summary collapse
- REQUEST_MAP =
{ get: Net::HTTP::Get, post: Net::HTTP::Post, put: Net::HTTP::Put, patch: Net::HTTP::Patch, delete: Net::HTTP::Delete, }.freeze
Instance Attribute Summary collapse
-
#json ⇒ Object
readonly
Returns the value of attribute json.
Class Method Summary collapse
- .hash_to_query(query_parameters) ⇒ Object
- .issue_request(method:, path:, headers: {}, payload: nil) ⇒ Object
- .join_path(*args) ⇒ Object
- .logger ⇒ Object
- .process_response(resp) ⇒ Object
-
.raise_rest_client_exception(e, a_path, http_method) ⇒ Object
re-raise the same exception with nicer error message.
-
.rest_client(http_type, method, path) ⇒ Object
Creates a RestClient::Resource class with a signed OAuth style Authentication header added to the request headers.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(json = {}) ⇒ HttpResource
constructor
A new instance of HttpResource.
Methods included from Concerns::FilterSensitiveData
Constructor Details
#initialize(json = {}) ⇒ HttpResource
Returns a new instance of HttpResource.
26 27 28 |
# File 'app/lib/katello/http_resource.rb', line 26 def initialize(json = {}) @json = json end |
Instance Attribute Details
#json ⇒ Object (readonly)
Returns the value of attribute json.
24 25 26 |
# File 'app/lib/katello/http_resource.rb', line 24 def json @json end |
Class Method Details
.hash_to_query(query_parameters) ⇒ Object
157 158 159 |
# File 'app/lib/katello/http_resource.rb', line 157 def hash_to_query(query_parameters) "?#{URI.encode_www_form(query_parameters)}" end |
.issue_request(method:, path:, headers: {}, payload: nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/lib/katello/http_resource.rb', line 86 def issue_request(method:, path:, headers: {}, payload: nil) logger.debug("Resource #{method.upcase} request: #{path}") logger.debug "Headers: #{headers.to_json}" begin logger.debug "Body: #{filter_sensitive_data(payload.to_json)}" rescue JSON::GeneratorError, Encoding::UndefinedConversionError logger.debug "Body: Error: could not render payload as json" end client = rest_client(REQUEST_MAP[method], method, path) args = [method, payload, headers].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
113 114 115 116 117 118 |
# File 'app/lib/katello/http_resource.rb', line 113 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
58 59 60 |
# File 'app/lib/katello/http_resource.rb', line 58 def logger fail NotImplementedError end |
.process_response(resp) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/lib/katello/http_resource.rb', line 62 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 = {} = "Rest exception while processing the call" service_code = "" status_code = resp.code.to_s begin parsed = JSON.parse resp.body = 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 => , :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
107 108 109 110 111 |
# File 'app/lib/katello/http_resource.rb', line 107 def raise_rest_client_exception(e, a_path, http_method) msg = "#{name}: #{e.} #{e.http_body} (#{http_method} #{a_path})" e. = 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.
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 150 151 152 153 154 155 |
# File 'app/lib/katello/http_resource.rb', line 122 def rest_client(http_type, method, path) # Need full path to properly generate the signature url = self.site + path params = { :site => self.site, :http_method => method, :request_token_path => "", :authorize_path => "", :access_token_path => ""} params[:ca_file] = self.ssl_ca_file unless self.ssl_ca_file.nil? # New OAuth consumer to setup signing the request consumer = OAuth::Consumer.new(self.consumer_key, self.consumer_secret, params) # The type is passed in, GET/POST/PUT/DELETE request = http_type.new(url) # Sign the request with OAuth consumer.sign!(request) # Extract the header and add it to the RestClient added_header = {'Authorization' => request['Authorization']} = { :headers => added_header, :open_timeout => SETTINGS[:katello][:rest_client_timeout], :timeout => SETTINGS[:katello][:rest_client_timeout], } [:ssl_ca_file] = self.ssl_ca_file unless self.ssl_ca_file.nil? [:ssl_client_cert] = self.ssl_client_cert unless self.ssl_client_cert.nil? [:ssl_client_key] = self.ssl_client_key unless self.ssl_client_key.nil? RestClient::Resource.new(url, ) end |
Instance Method Details
#[](key) ⇒ Object
30 31 32 |
# File 'app/lib/katello/http_resource.rb', line 30 def [](key) @json[key] end |
#[]=(key, value) ⇒ Object
34 35 36 |
# File 'app/lib/katello/http_resource.rb', line 34 def []=(key, value) @json[key] = value end |