Skip to main content

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

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: com.atproto.server.createInviteCode
3
4use serde::{Deserialize, Serialize};
5
6/// Create an invite code.
7/// XRPC Procedure: com.atproto.server.createInviteCode
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct Input {
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub for_account: Option<String>,
13    pub use_count: i64,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17#[serde(rename_all = "camelCase")]
18pub struct Output {
19    pub code: String,
20}
21
22/// Errors a `call()` on this method can return.
23#[derive(Debug, thiserror::Error)]
24pub enum CallError {
25    #[error("{0}")]
26    Xrpc(proto_blue_xrpc::XrpcError),
27    #[error(transparent)]
28    Transport(#[from] proto_blue_xrpc::Error),
29    #[error(transparent)]
30    Json(#[from] serde_json::Error),
31}
32
33fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
34    CallError::Xrpc(err)
35}
36
37/// Execute the procedure.
38pub async fn call(
39    client: &proto_blue_xrpc::XrpcClient,
40    input: &Input,
41    opts: Option<&proto_blue_xrpc::CallOptions>,
42) -> Result<Output, CallError> {
43    let qp_ref: Option<&proto_blue_xrpc::QueryParams> = None;
44    let body = proto_blue_xrpc::XrpcBody::Json(serde_json::to_value(input)?);
45    let response = match client.procedure("com.atproto.server.createInviteCode", qp_ref, Some(body), opts).await {
46        Ok(r) => r,
47        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
48        Err(e) => return Err(CallError::Transport(e)),
49    };
50    Ok(serde_json::from_value(response.data)?)
51}
52
53/// Register a typed handler for this procedure on an [`XrpcServer`].
54#[cfg(feature = "server")]
55pub fn register<F, Fut>(
56server: proto_blue_xrpc::XrpcServer,
57handler: F,
58) -> proto_blue_xrpc::XrpcServer
59where
60    F: Fn(proto_blue_xrpc::HandlerContext, Option<Input>) -> Fut + Send + Sync + 'static,
61    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
62{
63    let handler = std::sync::Arc::new(handler);
64    server.procedure("com.atproto.server.createInviteCode", move |ctx| {
65        let handler = handler.clone();
66        async move {
67            let input = match ctx.json_body()? {
68                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}")))?),
69                None => None,
70            };
71            let out = handler(ctx, input).await?;
72            let value = serde_json::to_value(&out)
73                .map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InternalServerError, format!("output serialize: {e}")))?;
74            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
75        }
76    })
77}
78