Optional managed-auth layer: a GenServer that holds the client and re-authenticates for you.
The functional core (Picnic + %Picnic.Client{}) is fully usable without
this module; reach for it only when you want a long-lived, self-healing
handle:
{:ok, session} =
Picnic.Session.start_link(country: :nl, email: "user@example.com", password: "...")
{:ok, cart} = Picnic.Session.get_cart(session)On any result with error category :auth, the session re-logs-in once
and retries the call once; if that still fails, the error is returned as-is.
Login is lazy — the first authenticated call triggers it — so starting the
session never blocks on the network.
A login that requires two-factor authentication cannot be completed
transparently; the :two_factor_required error is returned to the caller,
who should complete the flow with Picnic.Auth and pass the resulting
token via the token store or :client.
Tokens can be persisted across restarts through a Picnic.Session.TokenStore.
Besides the named wrappers below, run/2 executes any function of the
current client under the same re-auth policy:
Picnic.Session.run(session, &Picnic.search(&1, "melk"))
Summary
Functions
Returns a specification to start this module under a supervisor.
Returns the session's current client, e.g. to persist its token.
Generic passthrough under the session's re-auth policy. See Picnic.request/4.
Runs fun with the session's current client, re-authenticating once on an
:auth error.
Starts a session.
Functions
@spec add_to_cart(GenServer.server(), String.t(), keyword()) :: Picnic.result()
See Picnic.add_to_cart/3.
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec clear_cart( GenServer.server(), keyword() ) :: Picnic.result()
See Picnic.clear_cart/2.
@spec client(GenServer.server()) :: Picnic.Client.t()
Returns the session's current client, e.g. to persist its token.
@spec deliveries( GenServer.server(), keyword() ) :: Picnic.result()
See Picnic.deliveries/2.
@spec get_cart( GenServer.server(), keyword() ) :: Picnic.result()
See Picnic.get_cart/2.
@spec get_user( GenServer.server(), keyword() ) :: Picnic.result()
See Picnic.get_user/2.
@spec remove_from_cart(GenServer.server(), String.t(), keyword()) :: Picnic.result()
@spec request(GenServer.server(), Picnic.HTTP.method(), String.t(), keyword()) :: Picnic.result()
Generic passthrough under the session's re-auth policy. See Picnic.request/4.
@spec run(GenServer.server(), (Picnic.Client.t() -> result)) :: result when result: var
Runs fun with the session's current client, re-authenticating once on an
:auth error.
@spec search(GenServer.server(), String.t(), keyword()) :: Picnic.result()
See Picnic.search/3.
@spec start_link(keyword()) :: GenServer.on_start()
Starts a session.
Options: :email and :password (required), :client (a prebuilt
%Picnic.Client{}; otherwise one is built from the remaining options,
which are passed to Picnic.Client.new/1), :token_store
({module, opts}, default the in-memory store), and :name for GenServer
registration.