Class: Authkeeper::HttpService::Client

Inherits:
Object
  • Object
show all
Defined in:
app/lib/authkeeper/http_service/client.rb

Instance Method Summary collapse

Instance Method Details

#form_post(path:, body: {}, params: {}, headers: {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/lib/authkeeper/http_service/client.rb', line 35

def form_post(path:, body: {}, params: {}, headers: {})
  if Rails.env.test? && connection.adapter != 'Faraday::Adapter::Test'
    raise StandardError, 'please stub request in test env'
  end

  response = connection.post(path) do |request|
    params.each do |param, value|
      request.params[param] = value
    end
    headers.each do |header, value|
      request.headers[header] = value
    end
    request.body = URI.encode_www_form(body)
  end
  response.body if response.success?
end

#get(path:, params: nil, headers: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'app/lib/authkeeper/http_service/client.rb', line 13

def get(path:, params: nil, headers: nil)
  if Rails.env.test? && connection.adapter != 'Faraday::Adapter::Test'
    raise StandardError, 'please stub request in test env'
  end

  response = connection.get(path, params, headers)
  {
    success: response.success?,
    body: response.body
  }
end

#post(path:, body: nil, headers: nil) ⇒ Object

rubocop: disable Metrics/AbcSize



26
27
28
29
30
31
32
33
# File 'app/lib/authkeeper/http_service/client.rb', line 26

def post(path:, body: nil, headers: nil)
  if Rails.env.test? && connection.adapter != 'Faraday::Adapter::Test'
    raise StandardError, 'please stub request in test env'
  end

  response = connection.post(path, body, headers)
  response.body if response.success?
end