Skip to main content

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

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