scruffy/client

The plumbing every function in scruffy/client/* is built from: deciding whether a response succeeded, and decoding its body.

scruffy never performs I/O itself, so it never picks an HTTP client for you. Each endpoint in scruffy/client/* instead comes as a pair of plain functions: a *_request that builds a Request(String), and a *_response that turns the Response(String) you got back – from whatever client you sent that request with – into typed data. Send the request however suits your project (gleam_httpc on Erlang, gleam_fetch on JavaScript, or anything else that speaks gleam/http), then hand the response to the matching *_response function.

import gleam/httpc
import scruffy/client/cards

let assert Ok(resp) =
  cards.get_card_by_id_request("bd8fa327-dd41-4737-8f19-2cf5eb1f7cdd")
  |> httpc.send
let assert Ok(card) = cards.card_response(resp)

See https://scryfall.com/docs/api for the upstream reference.

Types

Everything that can go wrong decoding a response from Scryfall. Sending the request itself is on you and your own HTTP client – this only covers what happens once you have a Response(String) back.

pub type ClientError {
  ApiError(error.ScryfallError)
  DecodeError(json.DecodeError)
}

Constructors

  • Scryfall responded with a non-2xx status and an accompanying error object. See https://scryfall.com/docs/api/errors.

  • DecodeError(json.DecodeError)

    Scryfall returned a successful response, but its body didn’t decode into the shape it was expected to have.

Values

pub fn decode_response(
  resp: response.Response(String),
  then schema: glon.JsonSchema(t),
) -> Result(t, ClientError)

Decode a Response(String) from Scryfall with the given schema.

A non-2xx response is decoded as a ScryfallError instead of schema, and returned as Error(ApiError(_)). Every *_response function in scruffy/client/* is built from this.

Search Document