Skip to main content

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

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: com.atproto.sync.getBlob
3#![allow(clippy::pedantic, clippy::nursery, clippy::all)]
4
5use serde::{Deserialize, Serialize};
6
7/// Get a blob associated with a given account. Returns the full blob as originally uploaded. Does not require auth; implemented by PDS.
8/// XRPC Query: com.atproto.sync.getBlob
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Params {
12    pub cid: 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("BlobNotFound")]
20    BlobNotFound,
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("BlobNotFound") => CallError::BlobNotFound,
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.cid;
52        qp.insert(
53            "cid".to_string(),
54            proto_blue_xrpc::QueryValue::String(v.to_string()),
55        );
56    }
57    {
58        let v = &p.did;
59        qp.insert(
60            "did".to_string(),
61            proto_blue_xrpc::QueryValue::String(v.to_string()),
62        );
63    }
64    qp
65}
66
67/// Execute the query.
68pub async fn call(
69    client: &proto_blue_xrpc::XrpcClient,
70    params: Option<&Params>,
71    opts: Option<&proto_blue_xrpc::CallOptions>,
72) -> Result<serde_json::Value, CallError> {
73    let qp = params.map(to_query_params);
74    let response = match client
75        .query("com.atproto.sync.getBlob", qp.as_ref(), opts)
76        .await
77    {
78        Ok(r) => r,
79        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
80        Err(e) => return Err(CallError::Transport(e)),
81    };
82    Ok(response.data)
83}
84
85/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
86#[cfg(feature = "server")]
87pub fn register<F, Fut>(
88    server: proto_blue_xrpc::XrpcServer,
89    handler: F,
90) -> proto_blue_xrpc::XrpcServer
91where
92    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
93    Fut: std::future::Future<Output = Result<serde_json::Value, proto_blue_xrpc::XrpcServerError>>
94        + Send
95        + 'static,
96{
97    let handler = std::sync::Arc::new(handler);
98    server.query("com.atproto.sync.getBlob", move |ctx| {
99        let handler = handler.clone();
100        async move {
101            let params = params_from_ctx(&ctx);
102            let out = handler(ctx, params).await?;
103            Ok::<_, proto_blue_xrpc::XrpcServerError>(out)
104        }
105    })
106}
107
108#[cfg(feature = "server")]
109fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
110    // Always construct a `Params` — required fields are
111    // validated upstream by the lexicon validator when enabled;
112    // missing values surface as runtime errors from the handler.
113    Some(Params {
114        cid: (ctx.params.get("cid").cloned())?,
115        did: (ctx
116            .params
117            .get("did")
118            .and_then(|v| proto_blue_syntax::Did::new(v).ok()))?,
119    })
120}