proto_blue_api/generated/app/bsky/graph/
getBlocks.rs1use serde::{Deserialize, Serialize};
5
6#[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#[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 {
43 qp.insert(
44 "cursor".to_string(),
45 proto_blue_xrpc::QueryValue::String(v.clone()),
46 );
47 }
48 if let Some(v) = &p.limit {
49 qp.insert(
50 "limit".to_string(),
51 proto_blue_xrpc::QueryValue::Integer(*v),
52 );
53 }
54 qp
55}
56
57pub async fn call(
59 client: &proto_blue_xrpc::XrpcClient,
60 params: Option<&Params>,
61 opts: Option<&proto_blue_xrpc::CallOptions>,
62) -> Result<Output, CallError> {
63 let qp = params.map(to_query_params);
64 let response = match client
65 .query("app.bsky.graph.getBlocks", qp.as_ref(), opts)
66 .await
67 {
68 Ok(r) => r,
69 Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
70 Err(e) => return Err(CallError::Transport(e)),
71 };
72 Ok(serde_json::from_value(response.data)?)
73}
74
75#[cfg(feature = "server")]
77pub fn register<F, Fut>(
78 server: proto_blue_xrpc::XrpcServer,
79 handler: F,
80) -> proto_blue_xrpc::XrpcServer
81where
82 F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
83 Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
84 + Send
85 + 'static,
86{
87 let handler = std::sync::Arc::new(handler);
88 server.query("app.bsky.graph.getBlocks", move |ctx| {
89 let handler = handler.clone();
90 async move {
91 let params = params_from_ctx(&ctx);
92 let out = handler(ctx, params).await?;
93 let value = serde_json::to_value(&out).map_err(|e| {
94 proto_blue_xrpc::XrpcServerError::new(
95 proto_blue_xrpc::ResponseType::InternalServerError,
96 format!("output serialize: {e}"),
97 )
98 })?;
99 Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
100 }
101 })
102}
103
104#[cfg(feature = "server")]
105fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
106 Some(Params {
110 cursor: ctx.params.get("cursor").cloned(),
111 limit: ctx.params.get("limit").and_then(|v| v.parse::<i64>().ok()),
112 })
113}