Class: Resteze::Client
- Inherits:
-
Object
- Object
- Resteze::Client
- Includes:
- ApiModule, Instrumentation
- Defined in:
- lib/resteze/client.rb
Defined Under Namespace
Classes: RequestLogContext
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
Class Method Summary collapse
- .active_client ⇒ Object
-
.api_url(path = "") ⇒ Object
This can be overriden to customize.
- .client_name ⇒ Object
- .default_client ⇒ Object
- .default_connection ⇒ Object
- .faraday_adapter ⇒ Object
- .proxy_options ⇒ Object
- .user_agent ⇒ Object
Instance Method Summary collapse
-
#execute_request(method, path, headers: {}, params: {}) ⇒ Object
TODO: Look at refactoring this if possible to improve the Abc size rubocop:disable Metrics/AbcSize.
-
#initialize(connection = self.class.default_connection) ⇒ Client
constructor
A new instance of Client.
- #request ⇒ Object
Constructor Details
#initialize(connection = self.class.default_connection) ⇒ Client
Returns a new instance of Client.
57 58 59 |
# File 'lib/resteze/client.rb', line 57 def initialize(connection = self.class.default_connection) self.connection = connection end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
50 51 52 |
# File 'lib/resteze/client.rb', line 50 def connection @connection end |
Class Method Details
.active_client ⇒ Object
25 26 27 |
# File 'lib/resteze/client.rb', line 25 def self.active_client Thread.current[client_name] || default_client end |
.api_url(path = "") ⇒ Object
This can be overriden to customize
46 47 48 |
# File 'lib/resteze/client.rb', line 46 def self.api_url(path = "") [api_module.api_base.chomp("/".freeze), path].join end |
.client_name ⇒ Object
21 22 23 |
# File 'lib/resteze/client.rb', line 21 def self.client_name @client_name ||= to_s.underscore end |
.default_client ⇒ Object
29 30 31 |
# File 'lib/resteze/client.rb', line 29 def self.default_client Thread.current["#{client_name}_default_client"] ||= new(default_connection) end |
.default_connection ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/resteze/client.rb', line 33 def self.default_connection Thread.current["#{client_name}_default_connection"] ||= Faraday.new do |conn| conn.use Faraday::Request::UrlEncoded conn.use api_module::Middleware::RaiseError conn.adapter Faraday.default_adapter end end |
.faraday_adapter ⇒ Object
41 42 43 |
# File 'lib/resteze/client.rb', line 41 def self.faraday_adapter @faraday_adapter ||= default_connection.builder.adapter.name.demodulize.underscore end |
.proxy_options ⇒ Object
15 16 17 18 19 |
# File 'lib/resteze/client.rb', line 15 def self. return if api_module.try(:proxy).blank? Faraday::ProxyOptions.from(api_module.proxy) end |
Instance Method Details
#execute_request(method, path, headers: {}, params: {}) ⇒ Object
TODO: Look at refactoring this if possible to improve the Abc size rubocop:disable Metrics/AbcSize
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/resteze/client.rb', line 75 def execute_request(method, path, headers: {}, params: {}) params = util.objects_to_ids(params) body, query_params = process_params(method, params) headers = request_headers.merge(util.normalize_headers(headers)) context = request_log_context(body:, method:, path:, query_params:, headers:) http_resp = execute_request_with_rescues(context) do connection.run_request(method, api_url(path), body, headers) do |req| req..open_timeout = api_module.open_timeout req..timeout = api_module.read_timeout req..proxy = self.class. req.params = query_params unless query_params.nil? end end api_module::Response.from_faraday_response(http_resp).tap do |response| @last_response = response end end |
#request ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/resteze/client.rb', line 61 def request @last_response = nil old_client = Thread.current[client_name] Thread.current[client_name] = self begin res = yield [res, @last_response] ensure Thread.current[client_name] = old_client end end |