Class: Resteze::Client
- Inherits:
-
Object
- Object
- Resteze::Client
- Includes:
- ApiModule
- 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.
56 57 58 |
# File 'lib/resteze/client.rb', line 56 def initialize(connection = self.class.default_connection) self.connection = connection end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
49 50 51 |
# File 'lib/resteze/client.rb', line 49 def connection @connection end |
Class Method Details
.active_client ⇒ Object
24 25 26 |
# File 'lib/resteze/client.rb', line 24 def self.active_client Thread.current[client_name] || default_client end |
.api_url(path = "") ⇒ Object
This can be overriden to customize
45 46 47 |
# File 'lib/resteze/client.rb', line 45 def self.api_url(path = "") [api_module.api_base.chomp("/".freeze), path].join end |
.client_name ⇒ Object
20 21 22 |
# File 'lib/resteze/client.rb', line 20 def self.client_name @client_name ||= to_s.underscore end |
.default_client ⇒ Object
28 29 30 |
# File 'lib/resteze/client.rb', line 28 def self.default_client Thread.current["#{client_name}_default_client"] ||= new(default_connection) end |
.default_connection ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/resteze/client.rb', line 32 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
40 41 42 |
# File 'lib/resteze/client.rb', line 40 def self.faraday_adapter @faraday_adapter ||= default_connection.builder.adapter.name.demodulize.underscore end |
.proxy_options ⇒ Object
14 15 16 17 18 |
# File 'lib/resteze/client.rb', line 14 def self. return if api_module.try(:proxy).blank? Faraday::ProxyOptions.from(api_module.proxy) end |
.user_agent ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/resteze/client.rb', line 5 def self.user_agent [ "Ruby/#{RUBY_VERSION}", "Faraday/#{Faraday::VERSION} (#{faraday_adapter})", "Resteze/#{Resteze::VERSION}", "#{api_module}/#{api_module::VERSION}" ].join(" ") 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/resteze/client.rb', line 74 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
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/resteze/client.rb', line 60 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 |