Skip to main content

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

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: com.atproto.sync.requestCrawl
3
4use serde::{Deserialize, Serialize};
5
6/// Request a service to persistently crawl hosted repos. Expected use is new PDS instances declaring their existence to Relays. Does not require auth.
7/// XRPC Procedure: com.atproto.sync.requestCrawl
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct Input {
11    pub hostname: String,
12}
13
14/// Errors a `call()` on this method can return.
15#[derive(Debug, thiserror::Error)]
16pub enum CallError {
17    #[error("HostBanned")]
18    HostBanned,
19    #[error("{0}")]
20    Xrpc(proto_blue_xrpc::XrpcError),
21    #[error(transparent)]
22    Transport(#[from] proto_blue_xrpc::Error),
23    #[error(transparent)]
24    Json(#[from] serde_json::Error),
25}
26
27fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
28    match err.error.as_deref() {
29        Some("HostBanned") => CallError::HostBanned,
30        _ => CallError::Xrpc(err),
31    }
32}
33
34/// Execute the procedure.
35pub async fn call(
36    client: &proto_blue_xrpc::XrpcClient,
37    input: &Input,
38    opts: Option<&proto_blue_xrpc::CallOptions>,
39) -> Result<serde_json::Value, CallError> {
40    let qp_ref: Option<&proto_blue_xrpc::QueryParams> = None;
41    let body = proto_blue_xrpc::XrpcBody::Json(serde_json::to_value(input)?);
42    let response = match client.procedure("com.atproto.sync.requestCrawl", qp_ref, Some(body), opts).await {
43        Ok(r) => r,
44        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
45        Err(e) => return Err(CallError::Transport(e)),
46    };
47    Ok(response.data)
48}
49
50/// Register a typed handler for this procedure on an [`XrpcServer`].
51#[cfg(feature = "server")]
52pub fn register<F, Fut>(
53server: proto_blue_xrpc::XrpcServer,
54handler: F,
55) -> proto_blue_xrpc::XrpcServer
56where
57    F: Fn(proto_blue_xrpc::HandlerContext, Option<Input>) -> Fut + Send + Sync + 'static,
58    Fut: std::future::Future<Output = Result<serde_json::Value, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
59{
60    let handler = std::sync::Arc::new(handler);
61    server.procedure("com.atproto.sync.requestCrawl", move |ctx| {
62        let handler = handler.clone();
63        async move {
64            let input = match ctx.json_body()? {
65                Some(v) => Some(serde_json::from_value::<Input>(v).map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InvalidRequest, format!("input deserialize: {e}")))?),
66                None => None,
67            };
68            let out = handler(ctx, input).await?;
69            Ok::<_, proto_blue_xrpc::XrpcServerError>(out)
70        }
71    })
72}
73