Skip to main content

proto_blue_api/generated/app/bsky/draft/
updateDraft.rs

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: app.bsky.draft.updateDraft
3
4use serde::{Deserialize, Serialize};
5
6/// Updates a draft using private storage (stash). If the draft ID points to a non-existing ID, the update will be silently ignored. This is done because updates don't enforce draft limit, so it accepts all writes, but will ignore invalid ones. Requires authentication.
7/// XRPC Procedure: app.bsky.draft.updateDraft
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct Input {
11    pub draft: crate::app::bsky::draft::defs::DraftWithId,
12}
13
14/// Errors a `call()` on this method can return.
15#[derive(Debug, thiserror::Error)]
16pub enum CallError {
17    #[error("{0}")]
18    Xrpc(proto_blue_xrpc::XrpcError),
19    #[error(transparent)]
20    Transport(#[from] proto_blue_xrpc::Error),
21    #[error(transparent)]
22    Json(#[from] serde_json::Error),
23}
24
25fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
26    CallError::Xrpc(err)
27}
28
29/// Execute the procedure.
30pub async fn call(
31    client: &proto_blue_xrpc::XrpcClient,
32    input: &Input,
33    opts: Option<&proto_blue_xrpc::CallOptions>,
34) -> Result<serde_json::Value, CallError> {
35    let qp_ref: Option<&proto_blue_xrpc::QueryParams> = None;
36    let body = proto_blue_xrpc::XrpcBody::Json(serde_json::to_value(input)?);
37    let response = match client
38        .procedure("app.bsky.draft.updateDraft", qp_ref, Some(body), opts)
39        .await
40    {
41        Ok(r) => r,
42        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
43        Err(e) => return Err(CallError::Transport(e)),
44    };
45    Ok(response.data)
46}
47
48/// Register a typed handler for this procedure on an [`proto_blue_xrpc::XrpcServer`].
49#[cfg(feature = "server")]
50pub fn register<F, Fut>(
51    server: proto_blue_xrpc::XrpcServer,
52    handler: F,
53) -> proto_blue_xrpc::XrpcServer
54where
55    F: Fn(proto_blue_xrpc::HandlerContext, Option<Input>) -> Fut + Send + Sync + 'static,
56    Fut: std::future::Future<Output = Result<serde_json::Value, proto_blue_xrpc::XrpcServerError>>
57        + Send
58        + 'static,
59{
60    let handler = std::sync::Arc::new(handler);
61    server.procedure("app.bsky.draft.updateDraft", move |ctx| {
62        let handler = handler.clone();
63        async move {
64            let input = match ctx.json_body()? {
65                Some(v) => Some(serde_json::from_value::<Input>(v).map_err(|e| {
66                    proto_blue_xrpc::XrpcServerError::new(
67                        proto_blue_xrpc::ResponseType::InvalidRequest,
68                        format!("input deserialize: {e}"),
69                    )
70                })?),
71                None => None,
72            };
73            let out = handler(ctx, input).await?;
74            Ok::<_, proto_blue_xrpc::XrpcServerError>(out)
75        }
76    })
77}