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