libduvet/http/core

Types

How a request authenticates: a person’s session, or a bot’s token. A bot sends Authorization: Bot <token> and has no session token, so a 401 ends the request instead of renewing a session it doesn’t have.

pub type AuthMode {
  Session
  Bot
}

Constructors

  • Session
  • Bot

A request’s body.

pub type Body {
  NoBody
  JsonBody(value: json.Json)
  MultipartBody(
    fields: List(#(String, String)),
    files: List(multipart.FilePart),
  )
}

Constructors

A request ready for a transport: what a transport needs, on any target.

pub type Built {
  Built(
    method: String,
    url: String,
    headers: List(#(String, String)),
    pieces: List(BitArray),
  )
}

Constructors

  • Built(
      method: String,
      url: String,
      headers: List(#(String, String)),
      pieces: List(BitArray),
    )

What every request needs: where the API is, how to reach it, whose tokens to send, how to authenticate them, and how long it may take.

pub type Core {
  Core(
    router: api_router.ApiRouter,
    transport: fn(transport.Request, fn(Int) -> Nil, fn() -> Nil) -> Result(
      transport.Response,
      transport.ExchangeError,
    ),
    session: session.Session,
    auth: AuthMode,
    timeout_ms: Int,
  )
}

Constructors

What a request may say beyond its method, path and body.

pub type Meta(a) {
  Meta(
    query: List(#(String, String)),
    renew: Bool,
    timeout: Int,
    on_progress: option.Option(fn(Int, Int) -> Nil),
    abort: option.Option(process.Subject(a)),
  )
}

Constructors

  • Meta(
      query: List(#(String, String)),
      renew: Bool,
      timeout: Int,
      on_progress: option.Option(fn(Int, Int) -> Nil),
      abort: option.Option(process.Subject(a)),
    )

    Arguments

    renew

    Whether a 401 renews the session and sends the request once more.

Values

pub fn assert_ok(
  response: transport.Response,
  action: String,
) -> Result(transport.Response, error.DuvetError)

Answers the response when it succeeded, and the failure the status means otherwise: a 401 or 403 is an auth error, anything else an API error.

pub fn auth_headers(
  token: String,
  session_token: String,
  mode: AuthMode,
) -> List(#(String, String))

The session’s headers: the access token as a bearer token, and the session token, each only when there is one; or a bot’s token, as the API reads it, with no session token.

pub fn body_text(response: transport.Response) -> String
pub fn build(
  router: api_router.ApiRouter,
  token: String,
  session_token: String,
  method: String,
  path: String,
  body: Body,
  query: List(#(String, String)),
  mode: AuthMode,
) -> Built
pub fn decode_json(
  response: transport.Response,
) -> Result(json.Json, error.DuvetError)

The response’s body as any JSON value.

pub fn decode_list(
  response: transport.Response,
  read: fn(dict.Dict(String, dynamic.Dynamic)) -> item,
) -> Result(List(item), error.DuvetError)

The response’s body as a list, null counting as none.

pub fn decode_map(
  response: transport.Response,
) -> Result(dict.Dict(String, dynamic.Dynamic), error.DuvetError)

The response’s body as a JSON object.

pub fn default_meta(core: Core) -> Meta(a)

The defaults: no query, renewal on, the client’s timeout, no progress, no abort.

pub fn request(
  core: Core,
  method: String,
  path: String,
  action: String,
  body: Body,
  meta: Meta(a),
) -> Result(transport.Response, error.DuvetError)

Sends a request to the API with the session’s headers, and fails unless it succeeds. Unless renewal is off, a 401 renews the session and sends the request once more, as does a 401 to a request sent before a renewal that has finished since.

pub fn start(
  transport: fn(transport.Request, fn(Int) -> Nil, fn() -> Nil) -> Result(
    transport.Response,
    transport.ExchangeError,
  ),
  router: api_router.ApiRouter,
  token: String,
  session_token: String,
  timeout_ms: Int,
  auth: AuthMode,
) -> Core
pub fn with_abort(
  meta: Meta(a),
  abort: process.Subject(b),
) -> Meta(b)
pub fn with_progress(
  meta: Meta(a),
  on_progress: fn(Int, Int) -> Nil,
) -> Meta(a)
pub fn with_query(
  meta: Meta(a),
  query: List(#(String, String)),
) -> Meta(a)
pub fn with_renew(meta: Meta(a), renew: Bool) -> Meta(a)
pub fn with_timeout(meta: Meta(a), timeout: Int) -> Meta(a)
✨ Search Document