Passby.Conn (passby v0.2.0)

Copy Markdown View Source

Represents an HTTP connection in Passby.

This struct and its helper functions provide a familiar, lightweight API compatible with Plug.Conn and Bypass handler functions without requiring Plug as a dependency.

Summary

Functions

Fetches query parameters from the query string and stores them on the conn.

Gets the values of the specified request header.

Sets or adds a response header.

Sets the response status and body without immediately transmitting it.

Sends the response configured in the connection struct over the socket.

Sends the response with the given status and body over the underlying socket.

Types

params()

@type params() :: %{optional(String.t()) => term()}

t()

@type t() :: %Passby.Conn{
  adapter: {module(), any()},
  host: String.t(),
  method: String.t(),
  params: params(),
  path_info: [String.t()],
  path_params: params(),
  port: :inet.port_number() | nil,
  query_params: params(),
  query_string: String.t(),
  req_body: binary(),
  req_headers: [{String.t(), String.t()}],
  request_path: String.t(),
  resp_body: binary() | nil,
  resp_headers: [{String.t(), String.t()}],
  state: :unset | :set | :sent,
  status: integer() | nil
}

Functions

fetch_query_params(conn, opts \\ [])

@spec fetch_query_params(
  t(),
  keyword()
) :: t()

Fetches query parameters from the query string and stores them on the conn.

Populates conn.query_params with the decoded query string and merges the result into conn.params. Any params already present on the conn (for example path params) take precedence over query params on key conflicts.

Bracket notation is decoded the same way Plug.Conn.Query decodes it, so "filter[name]=Manuel" becomes %{"filter" => %{"name" => "Manuel"}}.

The optional second argument is accepted for Plug.Conn.fetch_query_params/2 compatibility and is currently ignored.

Examples

iex> conn = %Passby.Conn{query_string: "filter[name]=Manuel"}
iex> conn = Passby.Conn.fetch_query_params(conn)
iex> conn.query_params
%{"filter" => %{"name" => "Manuel"}}
iex> conn.params
%{"filter" => %{"name" => "Manuel"}}

get_req_header(conn, header_name)

@spec get_req_header(t(), String.t()) :: [String.t()]

Gets the values of the specified request header.

Header names are case-insensitive. Returns a list of string values matching the header name (or an empty list if not present).

Examples

iex> conn = %Passby.Conn{req_headers: [{"soapaction", "sessionOpen"}]}
iex> Passby.Conn.get_req_header(conn, "SoapAction")
["sessionOpen"]

put_resp_header(conn, key, value)

@spec put_resp_header(t(), String.t(), String.t()) :: t()

Sets or adds a response header.

Examples

iex> conn = %Passby.Conn{}
iex> conn = Passby.Conn.put_resp_header(conn, "content-type", "application/json")
iex> conn.resp_headers
[{"content-type", "application/json"}]

resp(conn, status, body)

@spec resp(t(), integer(), binary()) :: t()

Sets the response status and body without immediately transmitting it.

Examples

iex> conn = %Passby.Conn{}
iex> conn = Passby.Conn.resp(conn, 200, "Hello World")
iex> conn.status
200
iex> conn.resp_body
"Hello World"

send_resp(conn)

@spec send_resp(t()) :: t()

Sends the response configured in the connection struct over the socket.

send_resp(conn, status, body)

@spec send_resp(t(), integer(), binary()) :: t()

Sends the response with the given status and body over the underlying socket.

If no status and body are passed, it sends the response configured in the struct.