Picnic.Session (Picnic v0.1.0)

Copy Markdown View Source

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

add_to_cart(session, product_id, opts \\ [])

@spec add_to_cart(GenServer.server(), String.t(), keyword()) :: Picnic.result()

See Picnic.add_to_cart/3.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear_cart(session, opts \\ [])

@spec clear_cart(
  GenServer.server(),
  keyword()
) :: Picnic.result()

See Picnic.clear_cart/2.

client(session)

@spec client(GenServer.server()) :: Picnic.Client.t()

Returns the session's current client, e.g. to persist its token.

deliveries(session, opts \\ [])

@spec deliveries(
  GenServer.server(),
  keyword()
) :: Picnic.result()

See Picnic.deliveries/2.

get_cart(session, opts \\ [])

@spec get_cart(
  GenServer.server(),
  keyword()
) :: Picnic.result()

See Picnic.get_cart/2.

get_user(session, opts \\ [])

@spec get_user(
  GenServer.server(),
  keyword()
) :: Picnic.result()

See Picnic.get_user/2.

remove_from_cart(session, product_id, opts \\ [])

@spec remove_from_cart(GenServer.server(), String.t(), keyword()) :: Picnic.result()

See Picnic.remove_from_cart/3.

request(session, method, path, opts \\ [])

Generic passthrough under the session's re-auth policy. See Picnic.request/4.

run(session, fun)

@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.

search(session, term, opts \\ [])

@spec search(GenServer.server(), String.t(), keyword()) :: Picnic.result()

See Picnic.search/3.

start_link(opts)

@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.