1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/// This module provides wrappers for top-level (ie, not referencing a specific entity) API methods
pub mod api_handlers {

    use serde_derive::{Deserialize, Serialize};

    use crate::{
        api_client::{ApiError, Client},
        api_models::{
            collections::Collection,
            posts::{Post, PostCreation, PostCreationBuilder},
            users::User,
        },
    };

    #[derive(Clone, Debug)]
    /// Handler for [User] methods
    pub struct UserHandler {
        client: Client,
        current: Option<User>,
    }

    impl UserHandler {
        /// Creates a new [UserHandler] instance, and preloads the authenticated user info if available.
        pub async fn new(client: Client) -> Self {
            if client.is_authenticated() {
                UserHandler {
                    client: client.clone(),
                    current: match client.api().get::<User>("/me").await {
                        Ok(user) => Some(user),
                        Err(_) => None,
                    },
                }
            } else {
                UserHandler {
                    client: client.clone(),
                    current: None,
                }
            }
        }

        /// Returns the current [User] if available
        pub fn info(&self) -> Option<User> {
            self.current.clone()
        }

        /// Returns all [Post]s associated with the authenticated [User]
        pub async fn posts(&self) -> Result<Vec<Post>, ApiError> {
            if self.client.is_authenticated() {
                self.client
                    .api()
                    .get::<Vec<Post>>("/me/posts")
                    .await
                    .and_then(|mut v| {
                        Ok(v.iter_mut()
                            .map(|x| x.with_client(self.client.clone()))
                            .collect())
                    })
            } else {
                Err(ApiError::LoggedOut {})
            }
        }

        /// Returns the specified [Post]
        pub async fn post(&self, id: &str) -> Result<Post, ApiError> {
            if self.client.is_authenticated() {
                self.client
                    .api()
                    .get::<Post>(format!("/posts/{id}").as_str())
                    .await
                    .and_then(|mut v| Ok(v.with_client(self.client.clone())))
            } else {
                Err(ApiError::LoggedOut {})
            }
        }

        /// Returns all [Collection]s associated with the authenticated [User]
        pub async fn collections(&self) -> Result<Vec<Collection>, ApiError> {
            if self.client.is_authenticated() {
                self.client
                    .api()
                    .get::<Vec<Collection>>("/me/collections")
                    .await
                    .and_then(|mut v| {
                        Ok(v.iter_mut()
                            .map(|x| x.with_client(self.client.clone()))
                            .collect())
                    })
            } else {
                Err(ApiError::LoggedOut {})
            }
        }

        /// Returns the specified [Collection]
        pub async fn collection(&self, alias: &str) -> Result<Collection, ApiError> {
            if self.client.is_authenticated() {
                self.client
                    .api()
                    .get::<Collection>(format!("/collections/{alias}").as_str())
                    .await
                    .and_then(|mut v| Ok(v.with_client(self.client.clone())))
            } else {
                Err(ApiError::LoggedOut {})
            }
        }
    }

    #[derive(Clone, Debug)]
    /// Handler for [Post] methods
    pub struct PostHandler {
        client: Client,
    }

    impl PostHandler {
        /// Creates a new [PostHandler] with a [Client] instance
        pub fn new(client: Client) -> Self {
            PostHandler {
                client: client.clone(),
            }
        }

        /// Gets a specific [Post] by ID
        pub async fn get(&self, id: &str) -> Result<Post, ApiError> {
            self.client
                .api()
                .get::<Post>(format!("/posts/{id}").as_str())
                .await
                .and_then(|mut p| Ok(p.with_client(self.client.clone())))
        }

        /// Creates a [PostCreationBuilder] with the desired body.
        pub fn create(&self, body: String) -> PostCreationBuilder {
            PostCreationBuilder::default()
                .client(Some(self.client.clone()))
                .body(body)
                .clone()
        }

        /// Publishes a previously-made [PostCreation] instance
        pub async fn publish(&self, post: PostCreation) -> Result<Post, ApiError> {
            if let Some(collection) = post.collection.clone() {
                self.client
                    .api()
                    .post::<Post, PostCreation>(format!("/collections/{collection}/post").as_str(), Some(post))
                    .await
                    .and_then(|mut p| Ok(p.with_client(self.client.clone())))
            } else {
                self.client
                    .api()
                    .post::<Post, PostCreation>("/posts", Some(post))
                    .await
                    .and_then(|mut p| Ok(p.with_client(self.client.clone())))
            }
        }
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    struct CollectionParameters {
        pub alias: Option<String>,
        pub title: Option<String>,
    }

    #[derive(Clone, Debug)]
    /// Handler for [Collection] methods
    pub struct CollectionHandler {
        client: Client,
    }

    impl CollectionHandler {
        /// Creates a new [CollectionHandler] with a [Client] instance
        pub fn new(client: Client) -> Self {
            CollectionHandler {
                client: client.clone(),
            }
        }

        /// Creates a new [Collection]. At least one of `alias` and `title` must be specified.
        pub async fn create(
            &self,
            alias: Option<String>,
            title: Option<String>,
        ) -> Result<Collection, ApiError> {
            if alias.is_none() && title.is_none() {
                return Err(ApiError::UsageError {});
            }

            if !self.client.is_authenticated() {
                return Err(ApiError::LoggedOut {});
            }

            let params = CollectionParameters { alias, title };
            self.client
                .api()
                .post::<Collection, CollectionParameters>("/collections", Some(params))
                .await
                .and_then(|mut v| Ok(v.with_client(self.client.clone())))
        }

        /// Retrieves a [Collection] by its alias.
        pub async fn get(&self, alias: &str) -> Result<Collection, ApiError> {
            self.client
                .api()
                .get::<Collection>(format!("/collections/{alias}").as_str())
                .await
                .and_then(|mut v| Ok(v.with_client(self.client.clone())))
        }
    }
}