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.procedure("com.atproto.server.refreshSession", qp_ref, None, opts).await {
62        Ok(r) => r,
63        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
64        Err(e) => return Err(CallError::Transport(e)),
65    };
66    Ok(serde_json::from_value(response.data)?)
67}
68
69/// Register a typed handler for this procedure on an [`XrpcServer`].
70#[cfg(feature = "server")]
71pub fn register<F, Fut>(
72server: proto_blue_xrpc::XrpcServer,
73handler: F,
74) -> proto_blue_xrpc::XrpcServer
75where
76    F: Fn(proto_blue_xrpc::HandlerContext) -> Fut + Send + Sync + 'static,
77    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
78{
79    let handler = std::sync::Arc::new(handler);
80    server.procedure("com.atproto.server.refreshSession", move |ctx| {
81        let handler = handler.clone();
82        async move {
83            let out = handler(ctx).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