coinglecko/request

Request building and response handling for the CoinGecko API.

This module is transport-agnostic. It builds HTTP requests and decodes responses, but does NOT perform HTTP calls. Users provide their own sender function (e.g., httpc.send on Erlang, fetch.send on JavaScript).

Types

A query parameter key-value pair.

pub type QueryParam {
  QueryParam(key: String, value: String)
}

Constructors

  • QueryParam(key: String, value: String)

A function that sends an HTTP request and returns the response. Users provide this to bridge the library to their HTTP client.

Erlang example

import gleam/httpc
let send = fn(req) { httpc.send(req) |> result.map_error(string.inspect) }
pub type Sender =
  fn(request.Request(String)) -> Result(
    response.Response(String),
    String,
  )

Values

pub fn bool_to_string(b: Bool) -> String

Convert a Bool to a lowercase string for query parameters.

pub fn build_get_request(
  client: client.Client,
  path path: String,
  query query: List(QueryParam),
) -> Result(request.Request(String), error.CoinGeckoError)

Build a GET request for the CoinGecko API. Returns a fully configured Request with auth headers and query params.

pub fn flexible_float() -> decode.Decoder(Float)

A decoder that accepts both floats and ints, converting ints to floats. CoinGecko sometimes returns integers where floats are expected.

pub fn handle_response(
  response resp: response.Response(String),
  decoder decoder: decode.Decoder(a),
) -> Result(a, error.CoinGeckoError)

Handle an HTTP response: check status code and decode JSON body. On non-200 status, tries to parse CoinGecko’s error JSON for a message.

pub fn optional_flexible_float() -> decode.Decoder(
  option.Option(Float),
)

A decoder for optional flexible floats (handles null, missing, int, or float).

pub fn optional_params(
  params: List(#(String, option.Option(String))),
) -> List(QueryParam)

Convert a list of optional query params to actual params, filtering out None values.

pub fn send_and_decode(
  request req: request.Request(String),
  decoder decoder: decode.Decoder(a),
  send sender: fn(request.Request(String)) -> Result(
    response.Response(String),
    String,
  ),
) -> Result(a, error.CoinGeckoError)

Send a request using the provided sender and decode the response. Convenience function that chains build -> send -> decode.

Search Document