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    { let v = &p.cids; qp.insert("cids".to_string(), proto_blue_xrpc::QueryValue::Array(v.iter().map(|x| proto_blue_xrpc::QueryValue::String(x.clone())).collect())); }
50    { let v = &p.did; qp.insert("did".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
51    qp
52}
53
54/// Execute the query.
55pub async fn call(
56    client: &proto_blue_xrpc::XrpcClient,
57    params: Option<&Params>,
58    opts: Option<&proto_blue_xrpc::CallOptions>,
59) -> Result<serde_json::Value, CallError> {
60    let qp = params.map(to_query_params);
61    let response = match client.query("com.atproto.sync.getBlocks", qp.as_ref(), opts).await {
62        Ok(r) => r,
63        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
64        Err(e) => return Err(CallError::Transport(e)),
65    };
66    Ok(response.data)
67}
68
69/// Register a typed handler for this method on an [`XrpcServer`].
70#[cfg(feature = "server")]
71pub fn register<F, Fut>(
72server: proto_blue_xrpc::XrpcServer,
73handler: F,
74) -> proto_blue_xrpc::XrpcServer
75where
76    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
77    Fut: std::future::Future<Output = Result<serde_json::Value, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
78{
79    let handler = std::sync::Arc::new(handler);
80    server.query("com.atproto.sync.getBlocks", move |ctx| {
81        let handler = handler.clone();
82        async move {
83            let params = params_from_ctx(&ctx);
84            let out = handler(ctx, params).await?;
85            Ok::<_, proto_blue_xrpc::XrpcServerError>(out)
86        }
87    })
88}
89
90#[cfg(feature = "server")]
91fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
92    // Always construct a `Params` — required fields are
93    // validated upstream by the lexicon validator when enabled;
94    // missing values surface as runtime errors from the handler.
95    Some(Params {
96        cids: (Some(ctx.params.get("cids").map(|v| v.split(',').map(String::from).collect::<Vec<_>>()).unwrap_or_default()))?,
97        did: (ctx.params.get("did").cloned())?,
98    })
99}
100