Skip to main content

proto_blue_api/generated/app/bsky/notification/
registerPush.rs

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