Latch.HTTP (latch v0.2.0)

Copy Markdown

HTTP transport boundary for atproto identify resolution.

Fetches raw bodies and decodes JSON explicity, so behavior does not depend on server-provided content types (DID documents are sometimes served as application/did+ld+json).

Summary

Functions

GETs url, required a 2xx response, and decodes the body as a JSON object.

GETs url, requires a 2xx response, and returns the raw response body.

POSTs form as application/x-www-form-urlencoded with headers, returning the raw status, body, and response headers.

Performs an HTTP request with the given method, headers, and optional body, returning the raw status, body, and response headers.

Types

body()

@type body() :: nil | {:json, map()} | {:raw, binary(), String.t()}

Functions

get_json(url, opts \\ [])

GETs url, required a 2xx response, and decodes the body as a JSON object.

get_text(url)

@spec get_text(String.t()) ::
  {:ok, String.t()}
  | {:error, Latch.Error.Transport.t() | Latch.Error.InvalidResponse.t()}

GETs url, requires a 2xx response, and returns the raw response body.

post_form(url, form, headers \\ [])

@spec post_form(String.t(), keyword() | map(), [{String.t(), String.t()}]) ::
  {:ok,
   %{
     status: pos_integer(),
     body: binary(),
     headers: %{optional(binary()) => [binary()]}
   }}
  | {:error, Latch.Error.Transport.t()}

POSTs form as application/x-www-form-urlencoded with headers, returning the raw status, body, and response headers.

Unlike get_json/1 this neither treats non-2xx as an error nor decodes the body: atproto OAuth endpoints return meaningful JSON and a DPoP-Nonce header on 4xx responses, which the caller must inspect to drive the none retry.

request(method, url, headers, body \\ nil)

@spec request(String.t(), String.t(), [{String.t(), String.t()}], body()) ::
  {:ok,
   %{
     status: pos_integer(),
     body: binary(),
     headers: %{optional(binary()) => [binary()]}
   }}
  | {:error, Latch.Error.Transport.t()}

Performs an HTTP request with the given method, headers, and optional body, returning the raw status, body, and response headers.