Class: Authentik::Client
- Inherits:
-
Object
- Object
- Authentik::Client
- Defined in:
- lib/authentik/client.rb,
lib/authentik/client/railtie.rb,
lib/authentik/client/version.rb,
lib/authentik/client/configuration.rb
Overview
Client provides a Ruby interface to the authentik API. It wraps the auto-generated OpenAPI client and organizes API calls into groups that correspond to the underlying API classes.
API groups are accessed as methods on the client. Each group returns an ApiProxy that forwards method calls to the corresponding OpenAPI API class, stripping the redundant API group prefix for brevity.
New API groups added to the underlying OpenAPI client are automatically discovered and available without changes to this class.
Defined Under Namespace
Classes: Configuration, Railtie
Constant Summary collapse
- VERSION =
"0.1.1"
Class Method Summary collapse
-
.api_map ⇒ Hash{Symbol => Hash}
Returns a hash mapping API group name symbols to their API class and prefix.
-
.configuration ⇒ Configuration
Returns the global Configuration instance, creating it on first call.
-
.configure {|config| ... } ⇒ Object
Yields the global Configuration object so that settings can be applied at startup (e.g., in a Rails initializer).
-
.reset_configuration! ⇒ Configuration
Resets the global configuration to a fresh Configuration instance.
Instance Method Summary collapse
-
#initialize(host: nil, token: nil, scheme: nil, **options) ⇒ Client
constructor
A new instance of Client.
-
#method_missing(name) ⇒ ApiProxy
Provides access to an API group by name.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
Constructor Details
#initialize(host: nil, token: nil, scheme: nil, **options) ⇒ Client
Returns a new instance of Client.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/authentik/client.rb', line 89 def initialize(host: nil, token: nil, scheme: nil, **) cfg = self.class.configuration resolved_host = host || cfg.host resolved_token = token || cfg.access_token resolved_scheme = scheme || cfg.scheme raise ArgumentError, "host is required" if resolved_host.nil? raise ArgumentError, "token is required" if resolved_token.nil? @api_client = build_api_client( cfg, host: resolved_host, token: resolved_token, scheme: resolved_scheme, ** ) @api_instances = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ ApiProxy
Provides access to an API group by name. Returns an ApiProxy that wraps the corresponding OpenAPI API class instance.
The method name is the API class name (without the “Api” suffix) in lowercase. For example, :core maps to CoreApi, :admin to AdminApi, and :oauth2 to OAuth2Api.
117 118 119 120 121 122 |
# File 'lib/authentik/client.rb', line 117 def method_missing(name, ...) api_info = self.class.api_map[name] return super unless api_info @api_instances[name] ||= ApiProxy.new(api_info[:klass].new(@api_client), api_info[:prefix]) end |
Class Method Details
.api_map ⇒ Hash{Symbol => Hash}
Returns a hash mapping API group name symbols to their API class and prefix. Automatically discovers all classes in Authentik::Api that end with “Api”.
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/authentik/client.rb', line 133 def self.api_map @api_map ||= Authentik::Api.constants .select { |c| c.to_s.end_with?("Api") } .filter_map do |c| klass = Authentik::Api.const_get(c) next unless klass.is_a?(Class) base = c.to_s.delete_suffix("Api").downcase [base.to_sym, {klass: klass, prefix: "#{base}_"}] end .to_h end |
.configuration ⇒ Configuration
Returns the global Configuration instance, creating it on first call.
63 64 65 |
# File 'lib/authentik/client.rb', line 63 def self.configuration @configuration ||= Configuration.new end |
.configure {|config| ... } ⇒ Object
Yields the global Configuration object so that settings can be applied at startup (e.g., in a Rails initializer).
56 57 58 |
# File 'lib/authentik/client.rb', line 56 def self.configure yield(configuration) end |
.reset_configuration! ⇒ Configuration
Resets the global configuration to a fresh Configuration instance.
70 71 72 |
# File 'lib/authentik/client.rb', line 70 def self.reset_configuration! @configuration = Configuration.new end |
Instance Method Details
#respond_to_missing?(name, include_private = false) ⇒ Boolean
124 125 126 |
# File 'lib/authentik/client.rb', line 124 def respond_to_missing?(name, include_private = false) self.class.api_map.key?(name) || super end |