Skip to main content

proto_blue_api/generated/com/atproto/server/
getServiceAuth.rs

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: com.atproto.server.getServiceAuth
3#![allow(clippy::pedantic, clippy::nursery, clippy::all)]
4
5use serde::{Deserialize, Serialize};
6
7/// Get a signed token on behalf of the requesting DID for the requested service.
8/// XRPC Query: com.atproto.server.getServiceAuth
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Params {
12    pub aud: proto_blue_syntax::Did,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub exp: Option<i64>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub lxm: Option<proto_blue_syntax::Nsid>,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(rename_all = "camelCase")]
21pub struct Output {
22    pub token: String,
23}
24
25/// Errors a `call()` on this method can return.
26#[derive(Debug, thiserror::Error)]
27pub enum CallError {
28    /// Indicates that the requested expiration date is not a valid. May be in the past or may be reliant on the requested scopes.
29    #[error("BadExpiration")]
30    BadExpiration,
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("BadExpiration") => CallError::BadExpiration,
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    {
49        let v = &p.aud;
50        qp.insert(
51            "aud".to_string(),
52            proto_blue_xrpc::QueryValue::String(v.to_string()),
53        );
54    }
55    if let Some(v) = &p.exp {
56        qp.insert("exp".to_string(), proto_blue_xrpc::QueryValue::Integer(*v));
57    }
58    if let Some(v) = &p.lxm {
59        qp.insert(
60            "lxm".to_string(),
61            proto_blue_xrpc::QueryValue::String(v.to_string()),
62        );
63    }
64    qp
65}
66
67/// Execute the query.
68pub async fn call(
69    client: &proto_blue_xrpc::XrpcClient,
70    params: Option<&Params>,
71    opts: Option<&proto_blue_xrpc::CallOptions>,
72) -> Result<Output, CallError> {
73    let qp = params.map(to_query_params);
74    let response = match client
75        .query("com.atproto.server.getServiceAuth", qp.as_ref(), opts)
76        .await
77    {
78        Ok(r) => r,
79        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
80        Err(e) => return Err(CallError::Transport(e)),
81    };
82    Ok(serde_json::from_value(response.data)?)
83}
84
85/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
86#[cfg(feature = "server")]
87pub fn register<F, Fut>(
88    server: proto_blue_xrpc::XrpcServer,
89    handler: F,
90) -> proto_blue_xrpc::XrpcServer
91where
92    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
93    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
94        + Send
95        + 'static,
96{
97    let handler = std::sync::Arc::new(handler);
98    server.query("com.atproto.server.getServiceAuth", move |ctx| {
99        let handler = handler.clone();
100        async move {
101            let params = params_from_ctx(&ctx);
102            let out = handler(ctx, params).await?;
103            let value = serde_json::to_value(&out).map_err(|e| {
104                proto_blue_xrpc::XrpcServerError::new(
105                    proto_blue_xrpc::ResponseType::InternalServerError,
106                    format!("output serialize: {e}"),
107                )
108            })?;
109            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
110        }
111    })
112}
113
114#[cfg(feature = "server")]
115fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
116    // Always construct a `Params` — required fields are
117    // validated upstream by the lexicon validator when enabled;
118    // missing values surface as runtime errors from the handler.
119    Some(Params {
120        aud: (ctx
121            .params
122            .get("aud")
123            .and_then(|v| proto_blue_syntax::Did::new(v).ok()))?,
124        exp: ctx.params.get("exp").and_then(|v| v.parse::<i64>().ok()),
125        lxm: ctx
126            .params
127            .get("lxm")
128            .and_then(|v| proto_blue_syntax::Nsid::new(v).ok()),
129    })
130}