use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Input {
#[serde(skip_serializing_if = "Option::is_none")]
pub allow_takendown: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub auth_factor_token: Option<String>,
pub identifier: String,
pub password: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Output {
pub access_jwt: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
pub did: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub did_doc: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email_auth_factor: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email_confirmed: Option<bool>,
pub handle: String,
pub refresh_jwt: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<String>,
}
#[derive(Debug, thiserror::Error)]
pub enum CallError {
#[error("AccountTakedown")]
AccountTakedown,
#[error("AuthFactorTokenRequired")]
AuthFactorTokenRequired,
#[error("{0}")]
Xrpc(proto_blue_xrpc::XrpcError),
#[error(transparent)]
Transport(#[from] proto_blue_xrpc::Error),
#[error(transparent)]
Json(#[from] serde_json::Error),
}
fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
match err.error.as_deref() {
Some("AccountTakedown") => CallError::AccountTakedown,
Some("AuthFactorTokenRequired") => CallError::AuthFactorTokenRequired,
_ => CallError::Xrpc(err),
}
}
pub async fn call(
client: &proto_blue_xrpc::XrpcClient,
input: &Input,
opts: Option<&proto_blue_xrpc::CallOptions>,
) -> Result<Output, CallError> {
let qp_ref: Option<&proto_blue_xrpc::QueryParams> = None;
let body = proto_blue_xrpc::XrpcBody::Json(serde_json::to_value(input)?);
let response = match client
.procedure("com.atproto.server.createSession", qp_ref, Some(body), opts)
.await
{
Ok(r) => r,
Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
Err(e) => return Err(CallError::Transport(e)),
};
Ok(serde_json::from_value(response.data)?)
}
#[cfg(feature = "server")]
pub fn register<F, Fut>(
server: proto_blue_xrpc::XrpcServer,
handler: F,
) -> proto_blue_xrpc::XrpcServer
where
F: Fn(proto_blue_xrpc::HandlerContext, Option<Input>) -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
+ Send
+ 'static,
{
let handler = std::sync::Arc::new(handler);
server.procedure("com.atproto.server.createSession", move |ctx| {
let handler = handler.clone();
async move {
let input = match ctx.json_body()? {
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}"),
)
})?),
None => None,
};
let out = handler(ctx, input).await?;
let value = serde_json::to_value(&out).map_err(|e| {
proto_blue_xrpc::XrpcServerError::new(
proto_blue_xrpc::ResponseType::InternalServerError,
format!("output serialize: {e}"),
)
})?;
Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
}
})
}