Skip to main content

proto_blue_api/generated/app/bsky/feed/
getPosts.rs

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