agala v2.5.1 Agala.Conn

The Agala connection.

This module defines a Agala.Conn struct. This struct contains both request and response data.

Request fields

These fields contain request information:

  • request - request data structure. It’s internal structure depends on provider type.

Link to this section Summary

Functions

Assigns a value to a key in the connection

Examples

iex> conn.assigns[:hello]
nil
iex> conn = assign(conn, :hello, :world)
iex> conn.assigns[:hello]
:world

Starts a task to assign a value to a key in the connection. await_assign/2 can be used to wait for the async task to complete and retrieve the resulting value. Behind the scenes, it uses Task.async/1.

Examples

iex> conn.assigns[:hello]
nil
iex> conn = async_assign(conn, :hello, fn -> :world end)
iex> conn.assigns[:hello]
%Task{...}

Awaits the completion of an async assign. Returns a connection with the value resulting from the async assignment placed under key in the :assigns field. Behind the scenes, it uses Task.await/2.

Examples

iex> conn.assigns[:hello]
nil
iex> conn = async_assign(conn, :hello, fn -> :world end)
iex> conn = await_assign(conn, :hello) # blocks until `conn.assigns[:hello]` is available
iex> conn.assigns[:hello]
:world

Halts the Agala.Chain pipeline by preventing further chains downstream from being invoked. See the docs for Agala.Chain.Builder for more information on halting a Chain pipeline

Assigns a new private key and value in the connection. This storage is meant to be used by libraries and frameworks to avoid writing to the user storage (the :assigns field). It is recommended for libraries/frameworks to prefix the keys with the library name. For example, if some plug needs to store a :hello key, it should do so as :plug_hello:

iex> conn.private[:plug_hello]
nil
iex> conn = put_private(conn, :plug_hello, :world)
iex> conn.private[:plug_hello]
:world

Specifies the name for the bot, which will send the response back to side APIs

Specifies the lambda function that will be called after the result of provider’s respponse to the bot’s response will appear. The lambda shuld have only one parameter - Agala.Conn.t for current connection. It’ll have request with request to bot, response with response from bot, and fallback with response sending results

Link to this section Types

Link to this type t()
t() :: %Agala.Conn{
  assigns: Map.t(),
  fallback: function() | Map.t(),
  halted: boolean(),
  multi: Agala.Conn.Multi.t(),
  private: Map.t(),
  request: Map.t(),
  request_bot_params: Agala.BotParams.t(),
  response: Map.t(),
  responser: String.t() | Atom,
  responser_name: String.t() | Atom
}

Link to this section Functions

Link to this function assign(conn, key, value)
assign(t(), atom(), term()) :: t()

Assigns a value to a key in the connection

Examples

iex> conn.assigns[:hello]
nil
iex> conn = assign(conn, :hello, :world)
iex> conn.assigns[:hello]
:world
Link to this function async_assign(conn, key, fun)
async_assign(t(), atom(), (() -> term())) :: t()

Starts a task to assign a value to a key in the connection. await_assign/2 can be used to wait for the async task to complete and retrieve the resulting value. Behind the scenes, it uses Task.async/1.

Examples

iex> conn.assigns[:hello]
nil
iex> conn = async_assign(conn, :hello, fn -> :world end)
iex> conn.assigns[:hello]
%Task{...}
Link to this function await_assign(conn, key, timeout \\ 5000)
await_assign(t(), atom(), timeout()) :: t()

Awaits the completion of an async assign. Returns a connection with the value resulting from the async assignment placed under key in the :assigns field. Behind the scenes, it uses Task.await/2.

Examples

iex> conn.assigns[:hello]
nil
iex> conn = async_assign(conn, :hello, fn -> :world end)
iex> conn = await_assign(conn, :hello) # blocks until `conn.assigns[:hello]` is available
iex> conn.assigns[:hello]
:world
Link to this function halt(conn)
halt(t()) :: t()

Halts the Agala.Chain pipeline by preventing further chains downstream from being invoked. See the docs for Agala.Chain.Builder for more information on halting a Chain pipeline.

Link to this function put_private(conn, key, value)
put_private(t(), atom(), term()) :: t()

Assigns a new private key and value in the connection. This storage is meant to be used by libraries and frameworks to avoid writing to the user storage (the :assigns field). It is recommended for libraries/frameworks to prefix the keys with the library name. For example, if some plug needs to store a :hello key, it should do so as :plug_hello:

iex> conn.private[:plug_hello]
nil
iex> conn = put_private(conn, :plug_hello, :world)
iex> conn.private[:plug_hello]
:world
Link to this function send_to(conn, name)

Specifies the name for the bot, which will send the response back to side APIs.

Link to this function with_fallback(conn, fallback_callback)

Specifies the lambda function that will be called after the result of provider’s respponse to the bot’s response will appear. The lambda shuld have only one parameter - Agala.Conn.t for current connection. It’ll have request with request to bot, response with response from bot, and fallback with response sending results.