Skip to main content

proto_blue_api/generated/app/bsky/graph/
getBlocks.rs

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: app.bsky.graph.getBlocks
3#![allow(clippy::pedantic, clippy::nursery, clippy::all)]
4
5use serde::{Deserialize, Serialize};
6
7/// Enumerates which accounts the requesting account is currently blocking. Requires auth.
8/// XRPC Query: app.bsky.graph.getBlocks
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Params {
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub cursor: Option<String>,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub limit: Option<i64>,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19#[serde(rename_all = "camelCase")]
20pub struct Output {
21    pub blocks: Vec<crate::app::bsky::actor::defs::ProfileView>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub cursor: Option<String>,
24}
25
26/// Errors a `call()` on this method can return.
27#[derive(Debug, thiserror::Error)]
28pub enum CallError {
29    #[error("{0}")]
30    Xrpc(proto_blue_xrpc::XrpcError),
31    #[error(transparent)]
32    Transport(#[from] proto_blue_xrpc::Error),
33    #[error(transparent)]
34    Json(#[from] serde_json::Error),
35}
36
37fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
38    CallError::Xrpc(err)
39}
40
41fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
42    let mut qp = proto_blue_xrpc::QueryParams::new();
43    if let Some(v) = &p.cursor {
44        qp.insert(
45            "cursor".to_string(),
46            proto_blue_xrpc::QueryValue::String(v.to_string()),
47        );
48    }
49    if let Some(v) = &p.limit {
50        qp.insert(
51            "limit".to_string(),
52            proto_blue_xrpc::QueryValue::Integer(*v),
53        );
54    }
55    qp
56}
57
58/// Execute the query.
59pub async fn call(
60    client: &proto_blue_xrpc::XrpcClient,
61    params: Option<&Params>,
62    opts: Option<&proto_blue_xrpc::CallOptions>,
63) -> Result<Output, CallError> {
64    let qp = params.map(to_query_params);
65    let response = match client
66        .query("app.bsky.graph.getBlocks", qp.as_ref(), opts)
67        .await
68    {
69        Ok(r) => r,
70        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
71        Err(e) => return Err(CallError::Transport(e)),
72    };
73    Ok(serde_json::from_value(response.data)?)
74}
75
76/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
77#[cfg(feature = "server")]
78pub fn register<F, Fut>(
79    server: proto_blue_xrpc::XrpcServer,
80    handler: F,
81) -> proto_blue_xrpc::XrpcServer
82where
83    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
84    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
85        + Send
86        + 'static,
87{
88    let handler = std::sync::Arc::new(handler);
89    server.query("app.bsky.graph.getBlocks", move |ctx| {
90        let handler = handler.clone();
91        async move {
92            let params = params_from_ctx(&ctx);
93            let out = handler(ctx, params).await?;
94            let value = serde_json::to_value(&out).map_err(|e| {
95                proto_blue_xrpc::XrpcServerError::new(
96                    proto_blue_xrpc::ResponseType::InternalServerError,
97                    format!("output serialize: {e}"),
98                )
99            })?;
100            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
101        }
102    })
103}
104
105#[cfg(feature = "server")]
106fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
107    // Always construct a `Params` — required fields are
108    // validated upstream by the lexicon validator when enabled;
109    // missing values surface as runtime errors from the handler.
110    Some(Params {
111        cursor: ctx.params.get("cursor").cloned(),
112        limit: ctx.params.get("limit").and_then(|v| v.parse::<i64>().ok()),
113    })
114}