Skip to main content

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

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: com.atproto.server.refreshSession
3
4use serde::{Deserialize, Serialize};
5
6/// Refresh an authentication session. Requires auth using the 'refreshJwt' (not the 'accessJwt').
7/// XRPC Procedure: com.atproto.server.refreshSession
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct Output {
11    pub access_jwt: String,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub active: Option<bool>,
14    pub did: String,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub did_doc: Option<serde_json::Value>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub email: Option<String>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub email_auth_factor: Option<bool>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub email_confirmed: Option<bool>,
23    pub handle: String,
24    pub refresh_jwt: String,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub status: Option<String>,
27}
28
29/// Errors a `call()` on this method can return.
30#[derive(Debug, thiserror::Error)]
31pub enum CallError {
32    #[error("AccountTakedown")]
33    AccountTakedown,
34    #[error("InvalidToken")]
35    InvalidToken,
36    #[error("ExpiredToken")]
37    ExpiredToken,
38    #[error("{0}")]
39    Xrpc(proto_blue_xrpc::XrpcError),
40    #[error(transparent)]
41    Transport(#[from] proto_blue_xrpc::Error),
42    #[error(transparent)]
43    Json(#[from] serde_json::Error),
44}
45
46fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
47    match err.error.as_deref() {
48        Some("AccountTakedown") => CallError::AccountTakedown,
49        Some("InvalidToken") => CallError::InvalidToken,
50        Some("ExpiredToken") => CallError::ExpiredToken,
51        _ => CallError::Xrpc(err),
52    }
53}
54
55/// Execute the procedure.
56pub async fn call(
57    client: &proto_blue_xrpc::XrpcClient,
58    opts: Option<&proto_blue_xrpc::CallOptions>,
59) -> Result<Output, CallError> {
60    let qp_ref: Option<&proto_blue_xrpc::QueryParams> = None;
61    let response = match client
62        .procedure("com.atproto.server.refreshSession", qp_ref, None, opts)
63        .await
64    {
65        Ok(r) => r,
66        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
67        Err(e) => return Err(CallError::Transport(e)),
68    };
69    Ok(serde_json::from_value(response.data)?)
70}
71
72/// Register a typed handler for this procedure on an [`proto_blue_xrpc::XrpcServer`].
73#[cfg(feature = "server")]
74pub fn register<F, Fut>(
75    server: proto_blue_xrpc::XrpcServer,
76    handler: F,
77) -> proto_blue_xrpc::XrpcServer
78where
79    F: Fn(proto_blue_xrpc::HandlerContext) -> Fut + Send + Sync + 'static,
80    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
81        + Send
82        + 'static,
83{
84    let handler = std::sync::Arc::new(handler);
85    server.procedure("com.atproto.server.refreshSession", move |ctx| {
86        let handler = handler.clone();
87        async move {
88            let out = handler(ctx).await?;
89            let value = serde_json::to_value(&out).map_err(|e| {
90                proto_blue_xrpc::XrpcServerError::new(
91                    proto_blue_xrpc::ResponseType::InternalServerError,
92                    format!("output serialize: {e}"),
93                )
94            })?;
95            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
96        }
97    })
98}