Class: Resteze::Client

Inherits:
Object
  • Object
show all
Includes:
ApiModule, Instrumentation
Defined in:
lib/resteze/client.rb

Defined Under Namespace

Classes: RequestLogContext

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#connectionObject

Returns the value of attribute connection.



50
51
52
# File 'lib/resteze/client.rb', line 50

def connection
  @connection
end

Class Method Details

.active_clientObject



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_nameObject



21
22
23
# File 'lib/resteze/client.rb', line 21

def self.client_name
  @client_name ||= to_s.underscore
end

.default_clientObject



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_connectionObject



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_adapterObject



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_optionsObject



15
16
17
18
19
# File 'lib/resteze/client.rb', line 15

def self.proxy_options
  return if api_module.try(:proxy).blank?

  Faraday::ProxyOptions.from(api_module.proxy)
end

.user_agentObject



6
7
8
9
10
11
12
13
# File 'lib/resteze/client.rb', line 6

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



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.options.open_timeout = api_module.open_timeout
      req.options.timeout      = api_module.read_timeout
      req.options.proxy        = self.class.proxy_options
      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

#requestObject



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