Skip to main content

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

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: com.atproto.sync.getHostStatus
3#![allow(clippy::pedantic, clippy::nursery, clippy::all)]
4
5use serde::{Deserialize, Serialize};
6
7/// Returns information about a specified upstream host, as consumed by the server. Implemented by relays.
8/// XRPC Query: com.atproto.sync.getHostStatus
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Params {
12    pub hostname: String,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize)]
16#[serde(rename_all = "camelCase")]
17pub struct Output {
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub account_count: Option<i64>,
20    pub hostname: String,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub seq: Option<i64>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub status: Option<crate::com::atproto::sync::defs::HostStatus>,
25}
26
27/// Errors a `call()` on this method can return.
28#[derive(Debug, thiserror::Error)]
29pub enum CallError {
30    #[error("HostNotFound")]
31    HostNotFound,
32    #[error("{0}")]
33    Xrpc(proto_blue_xrpc::XrpcError),
34    #[error(transparent)]
35    Transport(#[from] proto_blue_xrpc::Error),
36    #[error(transparent)]
37    Json(#[from] serde_json::Error),
38}
39
40fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
41    match err.error.as_deref() {
42        Some("HostNotFound") => CallError::HostNotFound,
43        _ => CallError::Xrpc(err),
44    }
45}
46
47fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
48    let mut qp = proto_blue_xrpc::QueryParams::new();
49    {
50        let v = &p.hostname;
51        qp.insert(
52            "hostname".to_string(),
53            proto_blue_xrpc::QueryValue::String(v.to_string()),
54        );
55    }
56    qp
57}
58
59/// Execute the query.
60pub async fn call(
61    client: &proto_blue_xrpc::XrpcClient,
62    params: Option<&Params>,
63    opts: Option<&proto_blue_xrpc::CallOptions>,
64) -> Result<Output, CallError> {
65    let qp = params.map(to_query_params);
66    let response = match client
67        .query("com.atproto.sync.getHostStatus", qp.as_ref(), opts)
68        .await
69    {
70        Ok(r) => r,
71        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
72        Err(e) => return Err(CallError::Transport(e)),
73    };
74    Ok(serde_json::from_value(response.data)?)
75}
76
77/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
78#[cfg(feature = "server")]
79pub fn register<F, Fut>(
80    server: proto_blue_xrpc::XrpcServer,
81    handler: F,
82) -> proto_blue_xrpc::XrpcServer
83where
84    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
85    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
86        + Send
87        + 'static,
88{
89    let handler = std::sync::Arc::new(handler);
90    server.query("com.atproto.sync.getHostStatus", move |ctx| {
91        let handler = handler.clone();
92        async move {
93            let params = params_from_ctx(&ctx);
94            let out = handler(ctx, params).await?;
95            let value = serde_json::to_value(&out).map_err(|e| {
96                proto_blue_xrpc::XrpcServerError::new(
97                    proto_blue_xrpc::ResponseType::InternalServerError,
98                    format!("output serialize: {e}"),
99                )
100            })?;
101            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
102        }
103    })
104}
105
106#[cfg(feature = "server")]
107fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
108    // Always construct a `Params` — required fields are
109    // validated upstream by the lexicon validator when enabled;
110    // missing values surface as runtime errors from the handler.
111    Some(Params {
112        hostname: (ctx.params.get("hostname").cloned())?,
113    })
114}