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