Skip to main content

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

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