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