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 endpoints are accessed directly as methods on the client. The client resolves the endpoint prefix (for example, core_ or admin_) to the corresponding auto-generated OpenAPI API class.
New API groups added to the underlying OpenAPI client are automatically discovered and available without changes to this class.
Defined Under Namespace
Classes: Configuration, NoEndpointError, Railtie
Constant Summary collapse
- RESOURCE_ACTIONS =
%w[list retrieve create update partial_update destroy].freeze
- VERSION =
"0.2.0"
Class Method Summary collapse
-
.configuration ⇒ Configuration
Returns the global Configuration instance, creating it on the first call.
-
.configure {|config| ... } ⇒ Object
Yields the global Configuration object so that settings can be applied at startup (e.g., in a Rails initializer).
-
.endpoint?(name) ⇒ Boolean
Returns true if ‘name` is a known endpoint.
-
.endpoints ⇒ Array<Symbol>
Returns all known API endpoints.
-
.fetch_api_class_by_group(name) ⇒ Class
Returns the API class for a given API group.
-
.group?(name) ⇒ Boolean
Returns true if ‘name` is an API group.
-
.group_by_endpoint(name) ⇒ Symbol?
Returns the API group for a given endpoint.
-
.groups ⇒ Array<Symbol>
Returns all known API groups.
-
.reset_configuration! ⇒ Configuration
Resets the global configuration to a fresh Configuration instance.
-
.resource?(name) ⇒ Boolean
Returns true if ‘name` is a known resource.
-
.resources ⇒ Object
Returns all known API resources.
Instance Method Summary collapse
-
#initialize(host: nil, token: nil, scheme: nil, **options) ⇒ Client
constructor
A new instance of Client.
-
#method_missing(name) ⇒ Object
Dispatches endpoint calls directly to the corresponding generated API group instance, resolved by the endpoint prefix.
- #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.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/authentik/client.rb', line 195 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) ⇒ Object
Dispatches endpoint calls directly to the corresponding generated API group instance, resolved by the endpoint prefix.
216 217 218 219 220 221 222 223 224 225 |
# File 'lib/authentik/client.rb', line 216 def method_missing(name, ...) if self.class.endpoint?(name) group = self.class.group_by_endpoint(name) api_group_instance(group).public_send(name, ...) elsif self.class.group?(name) api_group_instance(name) else super end end |
Class Method Details
.configuration ⇒ Configuration
Returns the global Configuration instance, creating it on the first call.
68 69 70 |
# File 'lib/authentik/client.rb', line 68 def 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).
61 62 63 |
# File 'lib/authentik/client.rb', line 61 def configure yield(configuration) end |
.endpoint?(name) ⇒ Boolean
Returns true if ‘name` is a known endpoint.
120 121 122 |
# File 'lib/authentik/client.rb', line 120 def endpoint?(name) endpoints.include?(name.to_sym) end |
.endpoints ⇒ Array<Symbol>
Returns all known API endpoints.
127 128 129 |
# File 'lib/authentik/client.rb', line 127 def endpoints endpoint_group_map.keys end |
.fetch_api_class_by_group(name) ⇒ Class
Returns the API class for a given API group.
144 145 146 |
# File 'lib/authentik/client.rb', line 144 def fetch_api_class_by_group(name) group_api_class_map.fetch(name.to_sym) end |
.group?(name) ⇒ Boolean
Returns true if ‘name` is an API group.
83 84 85 |
# File 'lib/authentik/client.rb', line 83 def group?(name) groups.include?(name.downcase.to_sym) end |
.group_by_endpoint(name) ⇒ Symbol?
Returns the API group for a given endpoint.
135 136 137 |
# File 'lib/authentik/client.rb', line 135 def group_by_endpoint(name) endpoint_group_map[name.to_sym] end |
.groups ⇒ Array<Symbol>
Returns all known API groups.
90 91 92 |
# File 'lib/authentik/client.rb', line 90 def groups group_api_class_map.keys end |
.reset_configuration! ⇒ Configuration
Resets the global configuration to a fresh Configuration instance.
75 76 77 |
# File 'lib/authentik/client.rb', line 75 def reset_configuration! @configuration = Configuration.new end |
.resource?(name) ⇒ Boolean
Returns true if ‘name` is a known resource.
A resource is a collection of API endpoints that share a common prefix. For example, ‘:core_users` is the resource for `:core_users_list`, `:core_users_retrieve`, etc.
102 103 104 |
# File 'lib/authentik/client.rb', line 102 def resource?(name) resources.include?(name.to_sym) end |
.resources ⇒ Object
Returns all known API resources.
107 108 109 110 111 112 113 114 |
# File 'lib/authentik/client.rb', line 107 def resources @resources ||= begin remove_action_regexp = /(.*?)(_(#{RESOURCE_ACTIONS.join("|")}))?(_with_http_info)?$/ endpoints .map { |endpoint| endpoint.to_s.gsub(remove_action_regexp, '\1').to_sym } .uniq end end |
Instance Method Details
#respond_to_missing?(name, include_private = false) ⇒ Boolean
227 228 229 |
# File 'lib/authentik/client.rb', line 227 def respond_to_missing?(name, include_private = false) self.class.group?(name) || self.class.endpoint?(name) || super end |