Skip to main content

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

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: app.bsky.feed.getFeed
3#![allow(clippy::pedantic, clippy::nursery, clippy::all)]
4
5use serde::{Deserialize, Serialize};
6
7/// Get a hydrated feed from an actor's selected feed generator. Implemented by App View.
8/// XRPC Query: app.bsky.feed.getFeed
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Params {
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub cursor: Option<String>,
14    pub feed: proto_blue_syntax::AtUri,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub limit: Option<i64>,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(rename_all = "camelCase")]
21pub struct Output {
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub cursor: Option<String>,
24    pub feed: Vec<crate::app::bsky::feed::defs::FeedViewPost>,
25}
26
27/// Errors a `call()` on this method can return.
28#[derive(Debug, thiserror::Error)]
29pub enum CallError {
30    #[error("UnknownFeed")]
31    UnknownFeed,
32    #[error("{0}")]
33    Xrpc(proto_blue_xrpc::XrpcError),
34    #[error(transparent)]
35    Transport(#[from] proto_blue_xrpc::Error),
36    #[error(transparent)]
37    Json(#[from] serde_json::Error),
38}
39
40fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
41    match err.error.as_deref() {
42        Some("UnknownFeed") => CallError::UnknownFeed,
43        _ => CallError::Xrpc(err),
44    }
45}
46
47fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
48    let mut qp = proto_blue_xrpc::QueryParams::new();
49    if let Some(v) = &p.cursor {
50        qp.insert(
51            "cursor".to_string(),
52            proto_blue_xrpc::QueryValue::String(v.to_string()),
53        );
54    }
55    {
56        let v = &p.feed;
57        qp.insert(
58            "feed".to_string(),
59            proto_blue_xrpc::QueryValue::String(v.to_string()),
60        );
61    }
62    if let Some(v) = &p.limit {
63        qp.insert(
64            "limit".to_string(),
65            proto_blue_xrpc::QueryValue::Integer(*v),
66        );
67    }
68    qp
69}
70
71/// Execute the query.
72pub async fn call(
73    client: &proto_blue_xrpc::XrpcClient,
74    params: Option<&Params>,
75    opts: Option<&proto_blue_xrpc::CallOptions>,
76) -> Result<Output, CallError> {
77    let qp = params.map(to_query_params);
78    let response = match client
79        .query("app.bsky.feed.getFeed", qp.as_ref(), opts)
80        .await
81    {
82        Ok(r) => r,
83        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
84        Err(e) => return Err(CallError::Transport(e)),
85    };
86    Ok(serde_json::from_value(response.data)?)
87}
88
89/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
90#[cfg(feature = "server")]
91pub fn register<F, Fut>(
92    server: proto_blue_xrpc::XrpcServer,
93    handler: F,
94) -> proto_blue_xrpc::XrpcServer
95where
96    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
97    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
98        + Send
99        + 'static,
100{
101    let handler = std::sync::Arc::new(handler);
102    server.query("app.bsky.feed.getFeed", move |ctx| {
103        let handler = handler.clone();
104        async move {
105            let params = params_from_ctx(&ctx);
106            let out = handler(ctx, params).await?;
107            let value = serde_json::to_value(&out).map_err(|e| {
108                proto_blue_xrpc::XrpcServerError::new(
109                    proto_blue_xrpc::ResponseType::InternalServerError,
110                    format!("output serialize: {e}"),
111                )
112            })?;
113            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
114        }
115    })
116}
117
118#[cfg(feature = "server")]
119fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
120    // Always construct a `Params` — required fields are
121    // validated upstream by the lexicon validator when enabled;
122    // missing values surface as runtime errors from the handler.
123    Some(Params {
124        cursor: ctx.params.get("cursor").cloned(),
125        feed: (ctx
126            .params
127            .get("feed")
128            .and_then(|v| proto_blue_syntax::AtUri::new(v).ok()))?,
129        limit: ctx.params.get("limit").and_then(|v| v.parse::<i64>().ok()),
130    })
131}