Skip to main content

proto_blue_api/generated/com/atproto/sync/
getBlocks.rs

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: com.atproto.sync.getBlocks
3
4use serde::{Deserialize, Serialize};
5
6/// Get data blocks from a given repo, by CID. For example, intermediate MST nodes, or records. Does not require auth; implemented by PDS.
7/// XRPC Query: com.atproto.sync.getBlocks
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct Params {
11    pub cids: Vec<String>,
12    pub did: String,
13}
14
15/// Errors a `call()` on this method can return.
16#[derive(Debug, thiserror::Error)]
17pub enum CallError {
18    #[error("BlockNotFound")]
19    BlockNotFound,
20    #[error("RepoNotFound")]
21    RepoNotFound,
22    #[error("RepoTakendown")]
23    RepoTakendown,
24    #[error("RepoSuspended")]
25    RepoSuspended,
26    #[error("RepoDeactivated")]
27    RepoDeactivated,
28    #[error("{0}")]
29    Xrpc(proto_blue_xrpc::XrpcError),
30    #[error(transparent)]
31    Transport(#[from] proto_blue_xrpc::Error),
32    #[error(transparent)]
33    Json(#[from] serde_json::Error),
34}
35
36fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
37    match err.error.as_deref() {
38        Some("BlockNotFound") => CallError::BlockNotFound,
39        Some("RepoNotFound") => CallError::RepoNotFound,
40        Some("RepoTakendown") => CallError::RepoTakendown,
41        Some("RepoSuspended") => CallError::RepoSuspended,
42        Some("RepoDeactivated") => CallError::RepoDeactivated,
43        _ => CallError::Xrpc(err),
44    }
45}
46
47fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
48    let mut qp = proto_blue_xrpc::QueryParams::new();
49    {
50        let v = &p.cids;
51        qp.insert(
52            "cids".to_string(),
53            proto_blue_xrpc::QueryValue::Array(
54                v.iter()
55                    .map(|x| proto_blue_xrpc::QueryValue::String(x.clone()))
56                    .collect(),
57            ),
58        );
59    }
60    {
61        let v = &p.did;
62        qp.insert(
63            "did".to_string(),
64            proto_blue_xrpc::QueryValue::String(v.clone()),
65        );
66    }
67    qp
68}
69
70/// Execute the query.
71pub async fn call(
72    client: &proto_blue_xrpc::XrpcClient,
73    params: Option<&Params>,
74    opts: Option<&proto_blue_xrpc::CallOptions>,
75) -> Result<serde_json::Value, CallError> {
76    let qp = params.map(to_query_params);
77    let response = match client
78        .query("com.atproto.sync.getBlocks", qp.as_ref(), opts)
79        .await
80    {
81        Ok(r) => r,
82        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
83        Err(e) => return Err(CallError::Transport(e)),
84    };
85    Ok(response.data)
86}
87
88/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
89#[cfg(feature = "server")]
90pub fn register<F, Fut>(
91    server: proto_blue_xrpc::XrpcServer,
92    handler: F,
93) -> proto_blue_xrpc::XrpcServer
94where
95    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
96    Fut: std::future::Future<Output = Result<serde_json::Value, proto_blue_xrpc::XrpcServerError>>
97        + Send
98        + 'static,
99{
100    let handler = std::sync::Arc::new(handler);
101    server.query("com.atproto.sync.getBlocks", move |ctx| {
102        let handler = handler.clone();
103        async move {
104            let params = params_from_ctx(&ctx);
105            let out = handler(ctx, params).await?;
106            Ok::<_, proto_blue_xrpc::XrpcServerError>(out)
107        }
108    })
109}
110
111#[cfg(feature = "server")]
112fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
113    // Always construct a `Params` — required fields are
114    // validated upstream by the lexicon validator when enabled;
115    // missing values surface as runtime errors from the handler.
116    Some(Params {
117        cids: (Some(
118            ctx.params
119                .get("cids")
120                .map(|v| v.split(',').map(String::from).collect::<Vec<_>>())
121                .unwrap_or_default(),
122        ))?,
123        did: (ctx.params.get("did").cloned())?,
124    })
125}