Module: Eezee::Client::Requester

Defined in:
lib/eezee/client/requester.rb

Constant Summary collapse

METHODS =
%i[get post patch put delete].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/eezee/client/requester.rb', line 11

def self.extended(base)
  METHODS.each do |method|
    base.send(
      :define_singleton_method,
      method,
      ->(options = {}) { eezee_client_request(options, method) }
    )
  end
end

Instance Method Details

#build_eezee_request_lazyObject



50
51
52
53
54
# File 'lib/eezee/client/requester.rb', line 50

def build_eezee_request_lazy
  return unless eezee_options.dig(:service_options, :lazy)

  build_eezee_request(true)
end

#build_faraday_client(request) ⇒ Object



73
74
75
76
77
# File 'lib/eezee/client/requester.rb', line 73

def build_faraday_client(request)
  Faraday.new(request.uri) do |config|
    faraday_client_options!(config, request)
  end
end

#build_faraday_request(req, client, method) ⇒ Object



60
61
62
63
64
# File 'lib/eezee/client/requester.rb', line 60

def build_faraday_request(req, client, method)
  client.send(method) do |faraday_req|
    build_faraday_request_body(faraday_req, req)
  end
end

#build_faraday_request_body(faraday_req, req) ⇒ Object



66
67
68
69
70
71
# File 'lib/eezee/client/requester.rb', line 66

def build_faraday_request_body(faraday_req, req)
  return unless req.payload

  faraday_req.body = req.payload
  faraday_req.body = faraday_req.body.to_json unless req.url_encoded
end

#build_final_request(options, method) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/eezee/client/requester.rb', line 38

def build_final_request(options, method)
  build_eezee_request_lazy

  Eezee.configuration
       .request_by(eezee_options[:request], options)
       .tap do |request|
    request.before!(request)
    request.method = method
    request.log if request.logger
  end
end

#eezee_client_request(options, method) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/eezee/client/requester.rb', line 21

def eezee_client_request(options, method)
  request = build_final_request(options, method)

  build_faraday_client(request)
    .then { |client| build_faraday_request(request, client, method) }
    .then { |response| Eezee::Response.new(response) }
    .tap  { |response| response.log if request.logger }
    .tap  { |response| request.after!(request, response, nil) }
rescue Faraday::Error => e
  response = Eezee::Response.new(e)
  error = Eezee::RequestErrorFactory.build(request, response)
  error.log if request.logger
  return response if rescue_faraday_error?(request, response, error)

  raise error
end

#faraday_client_options!(config, request) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/eezee/client/requester.rb', line 79

def faraday_client_options!(config, request)
  config.request :url_encoded if request.url_encoded
  config.use(Faraday::Response::RaiseError) if request.raise_error
  config.headers = request.headers if request.headers
  config.options[:open_timeout] = request.open_timeout if request.open_timeout
  config.options[:timeout] = request.timeout if request.timeout
  config.adapter(Faraday.default_adapter)
  config.use(:ddtrace, request.ddtrace) if request.ddtrace.any?
end

#rescue_faraday_error?(req, res, err) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/eezee/client/requester.rb', line 56

def rescue_faraday_error?(req, res, err)
  req.after!(req, res, err) || (err.is_a?(Eezee::TimeoutError) && !req.raise_error)
end