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.procedure("com.atproto.server.createSession", qp_ref, Some(body), opts).await {
72        Ok(r) => r,
73        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
74        Err(e) => return Err(CallError::Transport(e)),
75    };
76    Ok(serde_json::from_value(response.data)?)
77}
78
79/// Register a typed handler for this procedure on an [`XrpcServer`].
80#[cfg(feature = "server")]
81pub fn register<F, Fut>(
82server: proto_blue_xrpc::XrpcServer,
83handler: F,
84) -> proto_blue_xrpc::XrpcServer
85where
86    F: Fn(proto_blue_xrpc::HandlerContext, Option<Input>) -> Fut + Send + Sync + 'static,
87    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
88{
89    let handler = std::sync::Arc::new(handler);
90    server.procedure("com.atproto.server.createSession", move |ctx| {
91        let handler = handler.clone();
92        async move {
93            let input = match ctx.json_body()? {
94                Some(v) => Some(serde_json::from_value::<Input>(v).map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InvalidRequest, format!("input deserialize: {e}")))?),
95                None => None,
96            };
97            let out = handler(ctx, input).await?;
98            let value = serde_json::to_value(&out)
99                .map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InternalServerError, format!("output serialize: {e}")))?;
100            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
101        }
102    })
103}
104