~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zat

atproto utils for zig · mirror of https://tangled.org/zat.dev/zat

0
0
73 lines · 2.7 KB Raw History

read a record

fetch one record from a PDS with com.atproto.repo.getRecord — a plain, unauthenticated GET.

const std = @import("std");
const zat = @import("zat");

pub fn main(init: std.process.Init) !void {
    const io = init.io;
    const allocator = init.gpa;

    const did = "did:plc:z72i7hdynmk6r22z27h6tvur"; // bsky.app

    // find the account's PDS (see the "resolve an identity" recipe for this chain)
    var did_resolver = zat.DidResolver.init(io, allocator);
    defer did_resolver.deinit();
    var doc = try did_resolver.resolve(zat.Did.parse(did).?);
    defer doc.deinit();

    const endpoint = doc.pdsEndpoint() orelse return error.NoPdsEndpoint;

    // read one record by (repo, collection, rkey) — a plain GET, no auth needed
    var client = zat.XrpcClient.init(io, allocator, endpoint);
    defer client.deinit();

    var params = std.StringHashMap([]const u8).init(allocator);
    defer params.deinit();
    try params.put("repo", did);
    try params.put("collection", "app.bsky.actor.profile");
    try params.put("rkey", "self");

    const method = zat.Nsid.parse("com.atproto.repo.getRecord").?;
    var resp = try client.query(method, params);
    defer resp.deinit();

    // the record is JSON; zat.json pulls fields by dotted path
    var parsed = try resp.json();
    defer parsed.deinit();
    const record = parsed.value;
    const name = zat.json.getString(record, "value.displayName") orelse "(none)";
    const desc = zat.json.getString(record, "value.description") orelse "(none)";
    std.debug.print("displayName: {s}\n", .{name});
    std.debug.print("description: {s}\n", .{desc});
}
displayName: Bluesky
description: official Bluesky account (check username👆)
...

This is examples/03_read_a_record.zig verbatim — copy it and zig build example-read-a-record.

A record is just JSON addressed by (repo, collection, rkey). The collection NSID (app.bsky.actor.profile) is defined by whichever app owns that lexicon — the PDS only stores and serves it, it doesn't interpret it.

This GET trusts the PDS to hand back the record faithfully — fine for reading, but the bytes aren't self-authenticating on their own. Checking a record against the repo's signed commit is what the "walk a repo" recipe sets up.

See also: the getRecord lexicon and the repository spec. Record browsers and appviews are built on reads like this.

wiring zat into your own <code>build.zig</code>
const zat = b.dependency("zat", .{}).module("zat");
exe.root_module.addImport("zat", zat);

after zig fetch --save https://tangled.org/zat.dev/zat/archive/main.