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    { let v = &p.uris; qp.insert("uris".to_string(), proto_blue_xrpc::QueryValue::Array(v.iter().map(|x| proto_blue_xrpc::QueryValue::String(x.clone())).collect())); }
38    qp
39}
40
41/// Execute the query.
42pub async fn call(
43    client: &proto_blue_xrpc::XrpcClient,
44    params: Option<&Params>,
45    opts: Option<&proto_blue_xrpc::CallOptions>,
46) -> Result<Output, CallError> {
47    let qp = params.map(to_query_params);
48    let response = match client.query("app.bsky.feed.getPosts", qp.as_ref(), opts).await {
49        Ok(r) => r,
50        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
51        Err(e) => return Err(CallError::Transport(e)),
52    };
53    Ok(serde_json::from_value(response.data)?)
54}
55
56/// Register a typed handler for this method on an [`XrpcServer`].
57#[cfg(feature = "server")]
58pub fn register<F, Fut>(
59server: proto_blue_xrpc::XrpcServer,
60handler: F,
61) -> proto_blue_xrpc::XrpcServer
62where
63    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
64    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
65{
66    let handler = std::sync::Arc::new(handler);
67    server.query("app.bsky.feed.getPosts", move |ctx| {
68        let handler = handler.clone();
69        async move {
70            let params = params_from_ctx(&ctx);
71            let out = handler(ctx, params).await?;
72            let value = serde_json::to_value(&out)
73                .map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InternalServerError, format!("output serialize: {e}")))?;
74            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
75        }
76    })
77}
78
79#[cfg(feature = "server")]
80fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
81    // Always construct a `Params` — required fields are
82    // validated upstream by the lexicon validator when enabled;
83    // missing values surface as runtime errors from the handler.
84    Some(Params {
85        uris: (Some(ctx.params.get("uris").map(|v| v.split(',').map(String::from).collect::<Vec<_>>()).unwrap_or_default()))?,
86    })
87}
88