Class: Auth::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/http_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id = nil, client_secret = nil, auth_server = nil, base_uri = nil, auth_client = OAuth2::Client, faraday_connection_opts: {}) ⇒ HttpClient

Returns a new instance of HttpClient.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/http_client.rb', line 9

def initialize(
  client_id = nil,
  client_secret = nil,
  auth_server = nil,
  base_uri = nil,
  auth_client = OAuth2::Client,
  faraday_connection_opts: {}
)
  raise Auth::Errors::ClientHasNoClientId if client_id.nil?
  raise Auth::Errors::ClientHasNoClientSecret if client_secret.nil?
  raise Auth::Errors::ClientHasNoAuthServer if auth_server.nil?
  raise Auth::Errors::ClientHasNoBaseUri if base_uri.nil?

  @authenticated_client = nil

  site_url = URI.parse(auth_server)
  token_url = "#{site_url.path}/oauth/token"
  authorisation_url = "#{site_url.path}/oauth/token"
  site_url = "#{site_url.scheme}://#{site_url.host}:#{site_url.port}"

  @base_uri = base_uri
  @client =
    auth_client.new client_id,
                    client_secret,
                    auth_scheme: :request_body,
                    site: site_url,
                    token_url:,
                    authorisation_url:,
                    raise_errors: false,
                    connection_opts: faraday_connection_opts
end

Instance Attribute Details

#authenticated_clientObject (readonly)

Returns the value of attribute authenticated_client.



7
8
9
# File 'lib/http_client.rb', line 7

def authenticated_client
  @authenticated_client
end

Class Method Details

.delegate(*methods) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/http_client.rb', line 49

def self.delegate(*methods)
  methods.each do |method_name|
    define_method(method_name) do |*args, &block|
      request method_name, *args, &block
    end
  end
end

Instance Method Details

#refreshObject



41
42
43
# File 'lib/http_client.rb', line 41

def refresh
  @authenticated_client = @client.client_credentials.get_token
end

#refresh?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/http_client.rb', line 45

def refresh?
  @authenticated_client.nil? || @authenticated_client.expired?
end

#request(method_name, *args, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/http_client.rb', line 59

def request(method_name, *args, &block)
  refresh? && refresh

  args[0] = @base_uri + args[0]

  if @authenticated_client.respond_to? method_name
    response = @authenticated_client.send method_name, *args, &block
    if response.status == 401
      # a 401 here is assumed to be due to an expired token
      # otherwise, refreshing the token and calling again should make no difference to the ultimate response
      refresh
      response = @authenticated_client.send method_name, *args, &block
    end

    response
  end
rescue Faraday::ConnectionFailed
  raise Auth::Errors::NetworkConnectionFailed
end