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