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
43        .procedure("com.atproto.sync.requestCrawl", qp_ref, Some(body), opts)
44        .await
45    {
46        Ok(r) => r,
47        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
48        Err(e) => return Err(CallError::Transport(e)),
49    };
50    Ok(response.data)
51}
52
53/// Register a typed handler for this procedure on an [`proto_blue_xrpc::XrpcServer`].
54#[cfg(feature = "server")]
55pub fn register<F, Fut>(
56    server: proto_blue_xrpc::XrpcServer,
57    handler: F,
58) -> proto_blue_xrpc::XrpcServer
59where
60    F: Fn(proto_blue_xrpc::HandlerContext, Option<Input>) -> Fut + Send + Sync + 'static,
61    Fut: std::future::Future<Output = Result<serde_json::Value, proto_blue_xrpc::XrpcServerError>>
62        + Send
63        + 'static,
64{
65    let handler = std::sync::Arc::new(handler);
66    server.procedure("com.atproto.sync.requestCrawl", move |ctx| {
67        let handler = handler.clone();
68        async move {
69            let input = match ctx.json_body()? {
70                Some(v) => Some(serde_json::from_value::<Input>(v).map_err(|e| {
71                    proto_blue_xrpc::XrpcServerError::new(
72                        proto_blue_xrpc::ResponseType::InvalidRequest,
73                        format!("input deserialize: {e}"),
74                    )
75                })?),
76                None => None,
77            };
78            let out = handler(ctx, input).await?;
79            Ok::<_, proto_blue_xrpc::XrpcServerError>(out)
80        }
81    })
82}