Skip to main content

synd_client/client/graphql/
feed.rs

1use synd_feed::types::FeedUrl;
2use tracing::instrument;
3
4use super::GraphqlRequest;
5use crate::{
6    Client, SyndApiError,
7    payload::{
8        SubscribeFeedInput, SubscribeFeedPayload, SubscriptionPayload, UnsubscribeFeedPayload,
9    },
10};
11
12const FETCH_SUBSCRIPTION_QUERY: &str = include_str!("query/fetch_subscription.gql");
13const SUBSCRIBE_FEED_MUTATION: &str = include_str!("query/subscribe_feed.gql");
14const UNSUBSCRIBE_FEED_MUTATION: &str = include_str!("query/unsubscribe_feed.gql");
15
16#[derive(Debug, serde::Serialize)]
17struct FetchSubscriptionVariables {
18    after: Option<String>,
19    first: Option<i64>,
20}
21
22#[derive(Debug, serde::Deserialize)]
23struct FetchSubscriptionData {
24    output: SubscriptionPayload,
25}
26
27impl From<FetchSubscriptionData> for SubscriptionPayload {
28    fn from(data: FetchSubscriptionData) -> Self {
29        data.output
30    }
31}
32
33#[derive(Debug, serde::Serialize)]
34struct SubscribeFeedVariables {
35    input: SubscribeFeedInput,
36}
37
38#[derive(Debug, serde::Deserialize)]
39#[serde(rename_all = "camelCase")]
40struct SubscribeFeedData {
41    subscribe_feed: SubscribeFeedPayload,
42}
43
44impl From<SubscribeFeedData> for SubscribeFeedPayload {
45    fn from(data: SubscribeFeedData) -> Self {
46        data.subscribe_feed
47    }
48}
49
50#[derive(Debug, serde::Serialize)]
51struct UnsubscribeFeedVariables {
52    input: UnsubscribeFeedInput,
53}
54
55#[derive(Debug, serde::Serialize)]
56struct UnsubscribeFeedInput {
57    url: FeedUrl,
58}
59
60#[derive(Debug, serde::Deserialize)]
61#[serde(rename_all = "camelCase")]
62struct UnsubscribeFeedData {
63    unsubscribe_feed: UnsubscribeFeedPayload,
64}
65
66impl From<UnsubscribeFeedData> for UnsubscribeFeedPayload {
67    fn from(data: UnsubscribeFeedData) -> Self {
68        data.unsubscribe_feed
69    }
70}
71
72impl Client {
73    #[instrument(skip(self))]
74    pub async fn fetch_subscription(
75        &self,
76        after: Option<String>,
77        first: Option<i64>,
78    ) -> Result<SubscriptionPayload, SyndApiError> {
79        let data: FetchSubscriptionData = self
80            .execute_graphql(&GraphqlRequest::new(
81                FETCH_SUBSCRIPTION_QUERY,
82                FetchSubscriptionVariables { after, first },
83            ))
84            .await?
85            .require_complete()?;
86        Ok(data.into())
87    }
88
89    #[instrument(skip(self))]
90    pub async fn subscribe_feed(
91        &self,
92        input: SubscribeFeedInput,
93    ) -> Result<SubscribeFeedPayload, SyndApiError> {
94        let data: SubscribeFeedData = self
95            .execute_graphql(&GraphqlRequest::new(
96                SUBSCRIBE_FEED_MUTATION,
97                SubscribeFeedVariables { input },
98            ))
99            .await?
100            .require_complete()?;
101        Ok(data.into())
102    }
103
104    #[instrument(skip(self))]
105    pub async fn unsubscribe_feed(
106        &self,
107        url: FeedUrl,
108    ) -> Result<UnsubscribeFeedPayload, SyndApiError> {
109        let data: UnsubscribeFeedData = self
110            .execute_graphql(&GraphqlRequest::new(
111                UNSUBSCRIBE_FEED_MUTATION,
112                UnsubscribeFeedVariables {
113                    input: UnsubscribeFeedInput { url },
114                },
115            ))
116            .await?
117            .require_complete()?;
118        Ok(data.into())
119    }
120}