Cased.Client (cased v1.0.0)
A client for the Cased API.
Link to this section Summary
Link to this section Types
create_opt()
Specs
create_opt() :: {:keys, keyword()} | {:key, String.t()} | {:environment_key, String.t()} | {:url, String.t()} | {:timeout, pos_integer() | :infinity}
create_opts()
Specs
create_opts() :: [create_opt()]
Specs
t() :: %Cased.Client{ environment_key: nil | String.t(), keys: %{required(atom()) => String.t()}, timeout: pos_integer() | :infinity, url: String.t() }
Link to this section Functions
create(opts \\ [])
Specs
create(opts :: create_opts()) :: {:ok, t()} | {:error, any()}
Create a Cased client.
Examples
Create a client with the policy key for your default
audit trail:
iex> {:ok, client} = Cased.Client.create(key: "policy_live_...")
Create a client key with policy keys for specific audit trails:
iex> {:ok, client} = Cased.Client.create(
...> keys: [
...> default: "policy_live_...",
...> users: "policy_live_users..."
...> ]
...> )
If you plan on using the API to interact with policies themselves, you need to provide an :environment_key
, for example:
iex> {:ok, client} = Cased.Client.create(
...> key: "policy_live_...",
...> environment_key: "environment_live_..."
...> )
)
Clients can be configured using runtime environment variables, your application
configuration, hardcoded values, or any combination you choose.
Just using runtime environment variable:
iex> {:ok, client} = Cased.Client.create( ...> key: System.fetch_env!("CASED_POLICY_KEY") ...> )
Just using application configuration:
iex> {:ok, client} = Cased.Client.create( ...> key: Application.fetch_env!(:your_app, :cased_policy_key) ...> )
Either/or:
iex> {:ok, client} = Cased.Client.create! ...> (key: System.get_env("CASED_POLICY_KEY") || Application.fetch_env!(:your_app, :cased_policy_key) ...> )
In the event your client is misconfigured, you'll get a `Cased.ConfigurationError` exception struct instead:
Not providing required options:
iex> {:error, %Cased.ConfigurationError{}} = Cased.Client.create()
You can also use `Cased.Client.create!/1` if you know you're passing the correct configuration options (otherwise it raises a `Cased.ConfigurationError` exception):
iex> client = Cased.Client.create!(key: "policylive...")
To simplify using clients across your application, consider writing a centralized function to handle constructing them:
defmodule YourApp do
# Rest of contents ...
def cased_client do
default_policy_key = System.get_env("CASED_POLICY_KEY")
|| Application.fetch_env!(:your_app, :cased_policy_key)
Cased.Client.create!(key: default_policy_key)
end end
For reuse, consider caching your client structs in `GenServer` state, ETS, or another Elixir caching mechanism.
create!(opts)
Specs
create!(opts :: create_opts()) :: t() | no_return()
Create a client or raise an exception.