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