proto-blue-api 0.2.5

AT Protocol high-level API: agent, rich text, moderation, generated types
Documentation
// Generated by atproto-codegen. Do not edit.
//! Lexicon: com.atproto.sync.getRepo

use serde::{Deserialize, Serialize};

/// Download a repository export as CAR file. Optionally only a 'diff' since a previous revision. Does not require auth; implemented by PDS.
/// XRPC Query: com.atproto.sync.getRepo
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Params {
    pub did: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub since: Option<String>,
}

/// Errors a `call()` on this method can return.
#[derive(Debug, thiserror::Error)]
pub enum CallError {
    #[error("RepoNotFound")]
    RepoNotFound,
    #[error("RepoTakendown")]
    RepoTakendown,
    #[error("RepoSuspended")]
    RepoSuspended,
    #[error("RepoDeactivated")]
    RepoDeactivated,
    #[error("{0}")]
    Xrpc(proto_blue_xrpc::XrpcError),
    #[error(transparent)]
    Transport(#[from] proto_blue_xrpc::Error),
    #[error(transparent)]
    Json(#[from] serde_json::Error),
}

fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
    match err.error.as_deref() {
        Some("RepoNotFound") => CallError::RepoNotFound,
        Some("RepoTakendown") => CallError::RepoTakendown,
        Some("RepoSuspended") => CallError::RepoSuspended,
        Some("RepoDeactivated") => CallError::RepoDeactivated,
        _ => CallError::Xrpc(err),
    }
}

fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
    let mut qp = proto_blue_xrpc::QueryParams::new();
    {
        let v = &p.did;
        qp.insert(
            "did".to_string(),
            proto_blue_xrpc::QueryValue::String(v.clone()),
        );
    }
    if let Some(v) = &p.since {
        qp.insert(
            "since".to_string(),
            proto_blue_xrpc::QueryValue::String(v.clone()),
        );
    }
    qp
}

/// Execute the query.
pub async fn call(
    client: &proto_blue_xrpc::XrpcClient,
    params: Option<&Params>,
    opts: Option<&proto_blue_xrpc::CallOptions>,
) -> Result<serde_json::Value, CallError> {
    let qp = params.map(to_query_params);
    let response = match client
        .query("com.atproto.sync.getRepo", qp.as_ref(), opts)
        .await
    {
        Ok(r) => r,
        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
        Err(e) => return Err(CallError::Transport(e)),
    };
    Ok(response.data)
}

/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
#[cfg(feature = "server")]
pub fn register<F, Fut>(
    server: proto_blue_xrpc::XrpcServer,
    handler: F,
) -> proto_blue_xrpc::XrpcServer
where
    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
    Fut: std::future::Future<Output = Result<serde_json::Value, proto_blue_xrpc::XrpcServerError>>
        + Send
        + 'static,
{
    let handler = std::sync::Arc::new(handler);
    server.query("com.atproto.sync.getRepo", move |ctx| {
        let handler = handler.clone();
        async move {
            let params = params_from_ctx(&ctx);
            let out = handler(ctx, params).await?;
            Ok::<_, proto_blue_xrpc::XrpcServerError>(out)
        }
    })
}

#[cfg(feature = "server")]
fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
    // Always construct a `Params` — required fields are
    // validated upstream by the lexicon validator when enabled;
    // missing values surface as runtime errors from the handler.
    Some(Params {
        did: (ctx.params.get("did").cloned())?,
        since: ctx.params.get("since").cloned(),
    })
}