Packages

A small, transport-agnostic atproto client for Gleam: XRPC, identity, OAuth discovery, blobs, and repo CRUD.

Current section

Files

Jump to
atproto_client src atproto blob.gleam
Raw

src/atproto/blob.gleam

//// The atproto blob reference, as embedded in records and returned by
//// `com.atproto.repo.uploadBlob`:
//// `{"$type":"blob","ref":{"$link":<cid>},"mimeType":...,"size":...}`.
import gleam/dynamic/decode
import gleam/json.{type Json}
import gleam/uri
pub type Blob {
Blob(cid: String, mime_type: String, size: Int)
}
pub fn encode_blob(value: Blob) -> Json {
json.object([
#("$type", json.string("blob")),
#("ref", json.object([#("$link", json.string(value.cid))])),
#("mimeType", json.string(value.mime_type)),
#("size", json.int(value.size)),
])
}
pub fn blob_decoder() -> decode.Decoder(Blob) {
use cid <- decode.field("ref", decode.at(["$link"], decode.string))
use mime_type <- decode.field("mimeType", decode.string)
use size <- decode.field("size", decode.int)
decode.success(Blob(cid:, mime_type:, size:))
}
/// The public URL a PDS serves this blob's bytes from.
pub fn public_url(pds: String, did: String, blob: Blob) -> String {
pds
<> "/xrpc/com.atproto.sync.getBlob?"
<> uri.query_to_string([#("did", did), #("cid", blob.cid)])
}