monzo/endpoints/
feed_items.rs

1//! Create items in your account feed
2
3pub use basic::Request as Basic;
4
5pub(crate) mod basic {
6    use serde::Serialize;
7
8    use crate::{client, endpoints::Endpoint, Result};
9
10    /// A request to create a new basic feed item.
11    ///
12    /// Currently a 'basic' feed item is the only kind
13    /// of feed item which is supported
14    ///
15    /// Use the builder methods to set optional fields
16    #[derive(Debug)]
17    #[must_use]
18    pub struct Request<'a, C>
19    where
20        C: client::Inner,
21    {
22        client: &'a C,
23        payload: Payload<'a>,
24    }
25
26    impl<'a, C> Request<'a, C>
27    where
28        C: client::Inner,
29    {
30        pub(crate) const fn new(
31            client: &'a C,
32            account_id: &'a str,
33            title: &'a str,
34            image_url: &'a str,
35        ) -> Self {
36            let params = Params {
37                title,
38                image_url,
39                background_color: None,
40                body_color: None,
41                title_color: None,
42                body: None,
43            };
44
45            let payload = Payload {
46                account_id,
47                url: None,
48                r#type: "basic",
49                params,
50            };
51
52            Self { client, payload }
53        }
54
55        /// Set the url of the feed item.
56        ///
57        /// This is the url the user will be redirected to after
58        /// tapping on the feed item
59        pub const fn url(mut self, url: &'a str) -> Self {
60            self.payload.url = Some(url);
61            self
62        }
63
64        /// Set the title of the feed item.
65        pub const fn title(mut self, title: &'a str) -> Self {
66            self.payload.params.title = title;
67            self
68        }
69
70        /// Set the image of the feed item.
71        ///
72        /// # Note
73        /// *This doesn't currently seem to do anything*
74        pub const fn image_url(mut self, image_url: &'a str) -> Self {
75            self.payload.params.image_url = image_url;
76            self
77        }
78
79        /// Set the background colour of the feed item
80        pub const fn background_color(mut self, background_color: &'a str) -> Self {
81            self.payload.params.background_color = Some(background_color);
82            self
83        }
84
85        /// Set the body colour of the feed item
86        pub const fn body_color(mut self, body_color: &'a str) -> Self {
87            self.payload.params.body_color = Some(body_color);
88            self
89        }
90
91        /// Set the title colour of the feed item
92        pub const fn title_color(mut self, title_color: &'a str) -> Self {
93            self.payload.params.title_color = Some(title_color);
94            self
95        }
96
97        /// Set the body text of the feed item
98        pub const fn body(mut self, body: &'a str) -> Self {
99            self.payload.params.body = Some(body);
100            self
101        }
102
103        /// Consume and send the [`Request`].
104        pub async fn send(self) -> Result<()> {
105            self.client.handle_request(&self).await
106        }
107    }
108
109    impl<C> Endpoint for Request<'_, C>
110    where
111        C: client::Inner,
112    {
113        const METHOD: reqwest::Method = reqwest::Method::POST;
114
115        fn endpoint(&self) -> &'static str {
116            "/feed"
117        }
118
119        fn json(&self) -> Option<&dyn erased_serde::Serialize> {
120            Some(&self.payload)
121        }
122    }
123
124    #[derive(Debug, Serialize)]
125    struct Params<'a> {
126        #[serde(rename = "params[title]")]
127        title: &'a str,
128
129        #[serde(rename = "params[image_url]")]
130        image_url: &'a str,
131
132        #[serde(rename = "params[background_color]")]
133        #[serde(skip_serializing_if = "Option::is_none")]
134        background_color: Option<&'a str>,
135
136        #[serde(rename = "params[body_color]")]
137        #[serde(skip_serializing_if = "Option::is_none")]
138        body_color: Option<&'a str>,
139
140        #[serde(rename = "params[title_color]")]
141        #[serde(skip_serializing_if = "Option::is_none")]
142        title_color: Option<&'a str>,
143
144        #[serde(rename = "params[body]")]
145        #[serde(skip_serializing_if = "Option::is_none")]
146        body: Option<&'a str>,
147    }
148
149    #[derive(Debug, Serialize)]
150    struct Payload<'a> {
151        // required for all feed item requests
152        account_id: &'a str,
153        r#type: &'static str,
154
155        #[serde(skip_serializing_if = "Option::is_none")]
156        url: Option<&'a str>,
157
158        #[serde(flatten)]
159        params: Params<'a>,
160    }
161}