Module: RestfulClient
- Defined in:
- lib/restful_client.rb,
lib/restful_client/version.rb
Defined Under Namespace
Classes: RestError
Constant Summary collapse
- SERVER_SIDE_ERRORS_RANGE =
500
- VERSION =
"0.1.0"
- @@configuration =
nil
- @@logger =
nil
- @@timeout_occured_count =
0
Class Method Summary collapse
- .callerr_config(caller) ⇒ Object
- .configuration ⇒ Object
- .configure {|configuration| ... } ⇒ Object
- .delete(caller, path, payload = {}, extra_options = {}, &on_error_block) ⇒ Object
- .fake(caller, path, options = {}, &block) ⇒ Object
- .get(caller, path, params = {}, extra_options = {}, &on_error_block) ⇒ Object
- .legacy_postfix ⇒ Object
- .logger ⇒ Object
- .post(caller, path, payload, extra_options = {}, &on_error_block) ⇒ Object
- .post_raw(caller, path, payload, custom_timeout = timeout, &on_error_block) ⇒ Object
- .prepare_payload_with_headers(payload, custom_headers) ⇒ Object
- .prettify_logger(type, request, response) ⇒ Object
- .put(caller, path, payload, extra_options = {}, &on_error_block) ⇒ Object
- .report_method ⇒ Object
- .retries ⇒ Object
- .run_request(request, method, retry_if_needed) ⇒ Object
- .run_safe_request(caller, request, retry_if_needed, &on_error_block) ⇒ Object
- .srv_url(caller) ⇒ Object
- .timeout ⇒ Object
- .use_jynx? ⇒ Boolean
- .user_agent ⇒ Object
Class Method Details
.callerr_config(caller) ⇒ Object
74 75 76 77 78 |
# File 'lib/restful_client.rb', line 74 def callerr_config(caller) caller_setup = configuration.data["#{caller}#{legacy_postfix}"] raise "Couldn't find ['#{caller}#{legacy_postfix}'] in the configuration YAML !!" unless caller_setup caller_setup end |
.configuration ⇒ Object
27 28 29 |
# File 'lib/restful_client.rb', line 27 def configuration @@configuration end |
.configure {|configuration| ... } ⇒ Object
20 21 22 23 24 25 |
# File 'lib/restful_client.rb', line 20 def self.configure @@configuration ||= RestfulClientConfiguration.new yield(configuration) @@configuration.run! @@logger = RestfulClientLogger.logger end |
.delete(caller, path, payload = {}, extra_options = {}, &on_error_block) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/restful_client.rb', line 60 def delete(caller, path, payload = {}, = {}, &on_error_block) url = RestfulClientUri.uri_join(callerr_config(caller)["url"], path) headers, payload_as_str = prepare_payload_with_headers(payload, .fetch("headers", {})) request = Typhoeus::Request.new(url, headers: headers, method: 'DELETE', body: payload_as_str, timeout: timeout) run_safe_request(caller, request, true, &on_error_block) end |
.fake(caller, path, options = {}, &block) ⇒ Object
177 178 179 180 |
# File 'lib/restful_client.rb', line 177 def fake(caller, path, = {}, &block) url = RestfulClientUri.uri_join(callerr_config(caller)["url"], path) Typhoeus.stub(url, = {}, &block) end |
.get(caller, path, params = {}, extra_options = {}, &on_error_block) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/restful_client.rb', line 39 def get(caller, path, params = {}, = {}, &on_error_block) url = RestfulClientUri.uri_join(callerr_config(caller)["url"], path) headers = { "Accept" => "text/json" } headers.merge!(.fetch("headers", {})) request = Typhoeus::Request.new(url, headers: headers, method: 'GET', timeout: timeout, params: params) run_safe_request(caller, request, true, &on_error_block) end |
.legacy_postfix ⇒ Object
194 195 196 |
# File 'lib/restful_client.rb', line 194 def legacy_postfix configuration.legacy_postfix end |
.logger ⇒ Object
31 32 33 |
# File 'lib/restful_client.rb', line 31 def logger @@logger end |
.post(caller, path, payload, extra_options = {}, &on_error_block) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/restful_client.rb', line 47 def post(caller, path, payload, = {}, &on_error_block) url = RestfulClientUri.uri_join(callerr_config(caller)["url"], path) headers, payload_as_str = prepare_payload_with_headers(payload, .fetch("headers", {})) request = Typhoeus::Request.new(url, headers: headers, method: 'POST', body: payload_as_str, timeout: timeout) run_safe_request(caller, request, false, &on_error_block) end |
.post_raw(caller, path, payload, custom_timeout = timeout, &on_error_block) ⇒ Object
54 55 56 57 58 |
# File 'lib/restful_client.rb', line 54 def post_raw(caller, path, payload, custom_timeout = timeout, &on_error_block) url = RestfulClientUri.uri_join(callerr_config(caller)["url"], path) request = Typhoeus::Request.new(url, method: 'POST', body: payload, timeout: custom_timeout) run_safe_request(caller, request, false, &on_error_block) end |
.prepare_payload_with_headers(payload, custom_headers) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/restful_client.rb', line 163 def prepare_payload_with_headers(payload, custom_headers) headers = {} if payload.is_a?(Hash) payload_as_str = payload.to_json(root: false) headers.merge!({ "Content-Type" => "application/json" }) else payload_as_str = payload end headers.merge!(custom_headers) [headers, payload_as_str] end |
.prettify_logger(type, request, response) ⇒ Object
206 207 208 209 |
# File 'lib/restful_client.rb', line 206 def prettify_logger(type, request, response) return "#{type} with no request or response." unless request || response "#{type} #{response.code}/#{response.return_code} for: #{request..fetch(:method)} #{request.base_url}, Total time: #{response.total_time} seconds" end |
.put(caller, path, payload, extra_options = {}, &on_error_block) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/restful_client.rb', line 67 def put(caller, path, payload, = {}, &on_error_block) url = RestfulClientUri.uri_join(callerr_config(caller)["url"], path) headers, payload_as_str = prepare_payload_with_headers(payload, .fetch("headers", {})) request = Typhoeus::Request.new(url, headers: headers, method: 'PUT', body: payload_as_str, timeout: timeout) run_safe_request(caller, request, false, &on_error_block) end |
.report_method ⇒ Object
202 203 204 |
# File 'lib/restful_client.rb', line 202 def report_method configuration.report_method end |
.retries ⇒ Object
198 199 200 |
# File 'lib/restful_client.rb', line 198 def retries configuration.retries end |
.run_request(request, method, retry_if_needed) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 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 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/restful_client.rb', line 101 def run_request(request, method, retry_if_needed) logger.debug { "#{__method__} :: Request :: #{request.inspect}" } request.[:headers].merge!({'X-Forwarded-For' => $client_ip}) if $client_ip request.[:headers].merge!({'User-Agent' => user_agent}) request.on_complete do |response| #200, OK if response.success? logger.debug { "Success in #{method} :: Code: #{response.response_code}, #{response.body}" } return "" if response.body.empty? begin return JSON.parse(response.body) rescue => e logger.error { "Response from #{response.effective_url} is not a valid json - [#{response.body}]"} raise e end #Timeout occured elsif response.timed_out? @@timeout_occured_count = @@timeout_occured_count + 1 skip_raise = (retry_if_needed && @@timeout_occured_count <= retries) error_type = "TimeoutOccured" error_description = prettify_logger(error_type, request, response) logger.error { "Time out in #{method} for: #{error_description}" } exception = RuntimeError.new(error_description) exception.set_backtrace(caller) report_method.call("RestError", error_description, exception) raise RestError.new(response.return_code.to_sym) unless skip_raise # Could not get an http response, something's wrong. elsif response.code == 0 error_type = :HttpError error_description = prettify_logger(error_type, request, response) logger.error { "HttpError Error #{response.code}/#{response.return_code} for: #{error_description}" } exception = RuntimeError.new(error_description) exception.set_backtrace(caller) report_method.call("RestError", error_description, exception) raise RestError.new(error_type) # Received a non-successful http response. elsif response.code >= SERVER_SIDE_ERRORS_RANGE error_type = :BadReturnCode error_description = prettify_logger(error_type, request, response) logger.error { "#{error_type} #{response.code}/#{response.return_code} for: #{error_description}" } report_method.call("RestError", error_description, exception) exception = RuntimeError.new(error_description) exception.set_backtrace(caller) raise RestError.new(error_type) else raise RestError.new(:BadReturnCode) end end request.run end |
.run_safe_request(caller, request, retry_if_needed, &on_error_block) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/restful_client.rb', line 80 def run_safe_request(caller, request, retry_if_needed, &on_error_block) @@timeout_occured_count = 0 if !use_jynx? response = run_request(request.dup, __method__, false) elsif ServiceJynx.alive?(caller) begin response = run_request(request.dup, __method__, retry_if_needed) end while response.is_a?(Typhoeus::Response) response else response = on_error_block.call("#{caller} set as down.") if on_error_block end response rescue => e res = ServiceJynx.failure!(caller) if use_jynx? report_method.call("ServiceJynx", "Service #{caller} was taken down as a result of exception", e) if res == :WENT_DOWN on_error_block.call("Exception in #{caller} execution - #{e.}") if on_error_block end |
.srv_url(caller) ⇒ Object
35 36 37 |
# File 'lib/restful_client.rb', line 35 def srv_url(caller) callerr_config(caller)["url"] end |
.timeout ⇒ Object
182 183 184 |
# File 'lib/restful_client.rb', line 182 def timeout configuration.timeout end |
.use_jynx? ⇒ Boolean
190 191 192 |
# File 'lib/restful_client.rb', line 190 def use_jynx? configuration.use_jynx end |
.user_agent ⇒ Object
186 187 188 |
# File 'lib/restful_client.rb', line 186 def user_agent configuration.user_agent end |