google_youtube_api_v3/httpc

Execute Request values with gleam_httpc, bindings to Erlang’s built-in httpc client.

Erlang target only. This module is a worked-example transport over the Sans-IO core, maintained as source (not generated): render a Request purely with to_url for logging, convert it with to_httpc_request, send it with send, then decode the body with request.parse_response and a generated decoder().

import google_youtube_api_v3/httpc
import google_youtube_api_v3/request
import youtube/api/videos
import youtube/video_list_response

pub fn popular(api_key: String) {
  let req =
    videos.list(["snippet"])
    |> videos.list_with_chart(videos.ListChartMostPopular)
    |> videos.list_with_max_results(10)
    |> videos.list_request
  use response <- result.try(
    httpc.send(req, request.ApiKey(api_key))
    |> result.map_error(fn(_) { Nil }),
  )
  request.parse_response(response.status, response.body, video_list_response.decoder())
  |> result.map_error(fn(_) { Nil })
}

Types

What can go wrong outside the API itself. API-level failures come back as Ok(Response) with a non-2xx status; decode those with request.parse_response.

pub type HttpError {
  TransportFailed(httpc.HttpError)
}

Constructors

An executed exchange: status code, headers, and body as a string. Feed status and body into request.parse_response.

pub type Response {
  Response(
    status: Int,
    headers: List(#(String, String)),
    body: String,
  )
}

Constructors

  • Response(
      status: Int,
      headers: List(#(String, String)),
      body: String,
    )

Values

pub fn send(
  request: request.Request,
  credentials: request.Credentials,
) -> Result(Response, HttpError)

Send one request with httpc and collect the response. Only JSON bodies are supported; media uploads need a resumable-upload client instead.

pub fn to_httpc_request(
  request: request.Request,
) -> request.Request(String)

Convert a Sans-IO request into a gleam_http request record ready for httpc.send. Pure.

pub fn to_url(request: request.Request) -> String

Render a request’s full URL for display and logging: root + path + encoded query string. Pure: test request builders through this without any I/O. The transport itself goes through to_httpc_request, which lets gleam_http own the encoding.

✨ Search Document