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