Skip to main content

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

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