Skip to main content

proto_blue_api/generated/com/atproto/label/
queryLabels.rs

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: com.atproto.label.queryLabels
3
4use serde::{Deserialize, Serialize};
5
6/// Find labels relevant to the provided AT-URI patterns. Public endpoint for moderation services, though may return different or additional results with auth.
7/// XRPC Query: com.atproto.label.queryLabels
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct Params {
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub cursor: Option<String>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub limit: Option<i64>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub sources: Option<Vec<String>>,
17    pub uri_patterns: Vec<String>,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21#[serde(rename_all = "camelCase")]
22pub struct Output {
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub cursor: Option<String>,
25    pub labels: Vec<crate::com::atproto::label::defs::Label>,
26}
27
28/// Errors a `call()` on this method can return.
29#[derive(Debug, thiserror::Error)]
30pub enum CallError {
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    CallError::Xrpc(err)
41}
42
43fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
44    let mut qp = proto_blue_xrpc::QueryParams::new();
45    if let Some(v) = &p.cursor { qp.insert("cursor".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
46    if let Some(v) = &p.limit { qp.insert("limit".to_string(), proto_blue_xrpc::QueryValue::Integer(*v)); }
47    if let Some(v) = &p.sources { qp.insert("sources".to_string(), proto_blue_xrpc::QueryValue::Array(v.iter().map(|x| proto_blue_xrpc::QueryValue::String(x.clone())).collect())); }
48    { let v = &p.uri_patterns; qp.insert("uriPatterns".to_string(), proto_blue_xrpc::QueryValue::Array(v.iter().map(|x| proto_blue_xrpc::QueryValue::String(x.clone())).collect())); }
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.label.queryLabels", 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.label.queryLabels", 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        cursor: ctx.params.get("cursor").cloned(),
97        limit: ctx.params.get("limit").and_then(|v| v.parse::<i64>().ok()),
98        sources: Some(ctx.params.get("sources").map(|v| v.split(',').map(String::from).collect::<Vec<_>>()).unwrap_or_default()),
99        uri_patterns: (Some(ctx.params.get("uriPatterns").map(|v| v.split(',').map(String::from).collect::<Vec<_>>()).unwrap_or_default()))?,
100    })
101}
102