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