proto_blue_api/generated/app/bsky/feed/
getPosts.rs1#![allow(clippy::pedantic, clippy::nursery, clippy::all)]
4
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Params {
12 pub uris: Vec<proto_blue_syntax::AtUri>,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize)]
16#[serde(rename_all = "camelCase")]
17pub struct Output {
18 pub posts: Vec<crate::app::bsky::feed::defs::PostView>,
19}
20
21#[derive(Debug, thiserror::Error)]
23pub enum CallError {
24 #[error("{0}")]
25 Xrpc(proto_blue_xrpc::XrpcError),
26 #[error(transparent)]
27 Transport(#[from] proto_blue_xrpc::Error),
28 #[error(transparent)]
29 Json(#[from] serde_json::Error),
30}
31
32fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
33 CallError::Xrpc(err)
34}
35
36fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
37 let mut qp = proto_blue_xrpc::QueryParams::new();
38 {
39 let v = &p.uris;
40 qp.insert(
41 "uris".to_string(),
42 proto_blue_xrpc::QueryValue::Array(
43 v.iter()
44 .map(|x| proto_blue_xrpc::QueryValue::String(x.to_string()))
45 .collect(),
46 ),
47 );
48 }
49 qp
50}
51
52pub async fn call(
54 client: &proto_blue_xrpc::XrpcClient,
55 params: Option<&Params>,
56 opts: Option<&proto_blue_xrpc::CallOptions>,
57) -> Result<Output, CallError> {
58 let qp = params.map(to_query_params);
59 let response = match client
60 .query("app.bsky.feed.getPosts", qp.as_ref(), opts)
61 .await
62 {
63 Ok(r) => r,
64 Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
65 Err(e) => return Err(CallError::Transport(e)),
66 };
67 Ok(serde_json::from_value(response.data)?)
68}
69
70#[cfg(feature = "server")]
72pub fn register<F, Fut>(
73 server: proto_blue_xrpc::XrpcServer,
74 handler: F,
75) -> proto_blue_xrpc::XrpcServer
76where
77 F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
78 Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
79 + Send
80 + 'static,
81{
82 let handler = std::sync::Arc::new(handler);
83 server.query("app.bsky.feed.getPosts", move |ctx| {
84 let handler = handler.clone();
85 async move {
86 let params = params_from_ctx(&ctx);
87 let out = handler(ctx, params).await?;
88 let value = serde_json::to_value(&out).map_err(|e| {
89 proto_blue_xrpc::XrpcServerError::new(
90 proto_blue_xrpc::ResponseType::InternalServerError,
91 format!("output serialize: {e}"),
92 )
93 })?;
94 Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
95 }
96 })
97}
98
99#[cfg(feature = "server")]
100fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
101 Some(Params {
105 uris: (ctx.params.get("uris").and_then(|v| {
106 v.split(',')
107 .map(proto_blue_syntax::AtUri::new)
108 .collect::<Result<Vec<_>, _>>()
109 .ok()
110 }))?,
111 })
112}