proto_blue_api/generated/com/atproto/sync/
listHosts.rs1use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7#[serde(rename_all = "camelCase")]
8pub struct Host {
9 #[serde(skip_serializing_if = "Option::is_none")]
10 pub account_count: Option<i64>,
11 pub hostname: String,
12 #[serde(skip_serializing_if = "Option::is_none")]
13 pub seq: Option<i64>,
14 #[serde(skip_serializing_if = "Option::is_none")]
15 pub status: Option<crate::com::atproto::sync::defs::HostStatus>,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
21#[serde(rename_all = "camelCase")]
22pub struct Params {
23 #[serde(skip_serializing_if = "Option::is_none")]
24 pub cursor: Option<String>,
25 #[serde(skip_serializing_if = "Option::is_none")]
26 pub limit: Option<i64>,
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(rename_all = "camelCase")]
31pub struct Output {
32 #[serde(skip_serializing_if = "Option::is_none")]
33 pub cursor: Option<String>,
34 pub hosts: Vec<Host>,
35}
36
37#[derive(Debug, thiserror::Error)]
39pub enum CallError {
40 #[error("{0}")]
41 Xrpc(proto_blue_xrpc::XrpcError),
42 #[error(transparent)]
43 Transport(#[from] proto_blue_xrpc::Error),
44 #[error(transparent)]
45 Json(#[from] serde_json::Error),
46}
47
48fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
49 CallError::Xrpc(err)
50}
51
52fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
53 let mut qp = proto_blue_xrpc::QueryParams::new();
54 if let Some(v) = &p.cursor { qp.insert("cursor".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
55 if let Some(v) = &p.limit { qp.insert("limit".to_string(), proto_blue_xrpc::QueryValue::Integer(*v)); }
56 qp
57}
58
59pub 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.query("com.atproto.sync.listHosts", qp.as_ref(), opts).await {
67 Ok(r) => r,
68 Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
69 Err(e) => return Err(CallError::Transport(e)),
70 };
71 Ok(serde_json::from_value(response.data)?)
72}
73
74#[cfg(feature = "server")]
76pub fn register<F, Fut>(
77server: proto_blue_xrpc::XrpcServer,
78handler: F,
79) -> proto_blue_xrpc::XrpcServer
80where
81 F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
82 Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
83{
84 let handler = std::sync::Arc::new(handler);
85 server.query("com.atproto.sync.listHosts", move |ctx| {
86 let handler = handler.clone();
87 async move {
88 let params = params_from_ctx(&ctx);
89 let out = handler(ctx, params).await?;
90 let value = serde_json::to_value(&out)
91 .map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InternalServerError, format!("output serialize: {e}")))?;
92 Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
93 }
94 })
95}
96
97#[cfg(feature = "server")]
98fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
99 Some(Params {
103 cursor: ctx.params.get("cursor").cloned(),
104 limit: ctx.params.get("limit").and_then(|v| v.parse::<i64>().ok()),
105 })
106}
107