lotr_api/request/
mod.rs

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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! This module contains the structs that are used to make a request to the API.
//! Here we define the [`Request`] struct and the [`RequestBuilder`] struct, which
//! are the center of the custom request system.

use reqwest::header::{self, HeaderMap, HeaderValue};

use crate::{Error, ItemType};

use self::{filter::Filter, pagination::Pagination, sort::Sort};

pub mod attributes;
pub mod filter;
pub mod pagination;
pub mod sort;

/// This trait is implemented by all structs that can be used to make a request to the API.
/// It is used to get the url for the request.
pub trait GetUrl {
    /// Returns the url that represents the struct's part of the request.
    fn get_url(&self) -> String;
}

/// This struct is used to build a [`Request`].
///
/// # Example
///
/// ```
/// use lotr_api::{Request, RequestBuilder, ItemType,
///     sort::{Sort, SortOrder},
///     request::GetUrl,
///     attribute::{Attribute, BookAttribute}};
///
/// let request = RequestBuilder::new(ItemType::Book)
///    .sort(Sort::new(
///    SortOrder::Ascending,
///    Attribute::Book(BookAttribute::Name),
///    ))
///    .build()
///    .unwrap();
///
/// assert_eq!(request.get_url(), "book?sort=name:asc");
/// ```
pub struct RequestBuilder {
    request: Request,
}

impl RequestBuilder {
    pub fn new(item_type: ItemType) -> Self {
        Self {
            request: Request::new(item_type),
        }
    }

    /// Sets the id of the request. This is used to get a specific item.
    pub fn id(mut self, id: String) -> Self {
        self.request.id = Some(id);
        self
    }

    /// Sets the secondary item type of the request. If you wish
    /// to get a secondary item type, you need to set the id of the request.\
    /// If not the `build` function will return an error.
    ///
    /// # Example
    /// ```
    /// use lotr_api::{ItemType, Request, RequestBuilder,
    ///     request::GetUrl};
    ///
    /// let request = RequestBuilder::new(ItemType::Character)
    ///     .id("123".to_string())
    ///     .secondary_item_type(ItemType::Quote)
    ///     .build()
    ///     .unwrap();
    ///
    /// assert_eq!(request.get_url(), "character/123/quote");
    ///   ```
    ///
    pub fn secondary_item_type(mut self, secondary_item_type: ItemType) -> Self {
        self.request.secondary_item_type = Some(secondary_item_type);
        self
    }

    /// Sets the sort of the request. If you wish to sort the results
    /// of the request, the `sort_by` attribute of the `Sort` struct
    /// must be of the same type as the item type of the request ( or the
    /// secondary item type if it is set).
    ///
    /// # Example
    /// ```
    /// use lotr_api::{ItemType, Request, RequestBuilder,
    ///     attribute::{Attribute, BookAttribute},
    ///     request::GetUrl,
    ///     sort::{Sort, SortOrder}};
    ///
    /// let request = RequestBuilder::new(ItemType::Book)
    ///     .sort(Sort::new(
    ///         SortOrder::Ascending,
    ///         Attribute::Book(BookAttribute::Name),
    ///     ))
    ///     .build()
    ///     .unwrap();
    ///
    /// assert_eq!(request.get_url(), "book?sort=name:asc");
    /// ```
    /// Failing to match the item type of the request results in an error.
    /// ```
    /// use lotr_api::{ItemType, Request, RequestBuilder,
    ///     attribute::{Attribute, BookAttribute},
    ///     request::GetUrl,
    ///     sort::{ Sort, SortOrder}};
    ///
    ///  let request = RequestBuilder::new(ItemType::Character)
    ///     .id("123".to_string())
    ///     .secondary_item_type(ItemType::Quote)
    ///     .sort(Sort::new(
    ///         SortOrder::Ascending,
    ///         Attribute::Book(BookAttribute::Name),
    ///     ))
    ///     .build();
    ///
    /// assert!(request.is_err());
    /// ```
    ///
    pub fn sort(mut self, sort: Sort) -> Self {
        self.request.sort = Some(sort);
        self
    }

    /// Sets the filter of the request. If you wish to filter the results
    /// of the request, the `filter_by` attribute of the `Filter` struct
    /// must be of the same type as the item type of the request ( or the
    /// secondary item type if it is set).
    ///
    /// # Example
    /// ```
    /// use lotr_api::{ItemType, Request, RequestBuilder,
    ///     attribute::{Attribute, BookAttribute},
    ///     request::GetUrl,
    ///     filter::{Filter, Operator}};
    ///
    /// let request = RequestBuilder::new(ItemType::Book)
    ///     .filter(Filter::Match(
    ///         Attribute::Book(BookAttribute::Name),
    ///         Operator::Eq,
    ///         vec!["The Fellowship of the Ring".to_string()],
    ///     ))
    ///     .build()
    ///     .unwrap();
    ///
    /// assert_eq!(request.get_url(), "book?name=The Fellowship of the Ring");
    /// ```
    ///
    /// Failing to match the item type of the request results in an error.
    ///
    /// ```
    /// use lotr_api::{ItemType, Request, RequestBuilder,
    ///     attribute::{Attribute, BookAttribute},
    ///     request::GetUrl,
    ///     filter::{Filter, Operator}};
    ///
    /// let request = RequestBuilder::new(ItemType::Character)
    ///     .id("123".to_string())
    ///     .secondary_item_type(ItemType::Quote)
    ///     .filter(Filter::Match(
    ///         Attribute::Book(BookAttribute::Name),
    ///         Operator::Eq,
    ///         vec!["The Fellowship of the Ring".to_string()],
    ///     ))
    ///     .build();
    ///
    /// assert!(request.is_err());
    /// ```
    pub fn filter(mut self, filter: Filter) -> Self {
        self.request.filter = Some(filter);
        self
    }

    /// Sets the pagination of the request.
    pub fn pagination(mut self, pagination: Pagination) -> Self {
        self.request.pagination = Some(pagination);
        self
    }

    /// Builds the request. If the request is invalid, an error is returned.
    ///
    /// # Errors
    ///
    /// A request is invalid if:
    /// - The secondary item type is set but the id is not.
    /// - The sort is set but the item type of the sort does not match the item type of the request.
    /// - The filter is set but the item type of the filter does not match the item type of the request.
    pub fn build(self) -> Result<Request, Error> {
        let item_type = self.request.get_item_type();
        if let Some(sort) = &self.request.sort {
            if sort.get_item_type() != item_type {
                return Err(Error::InvalidSort);
            }
        }
        if let Some(filter) = &self.request.filter {
            if filter.get_item_type() != item_type {
                return Err(Error::InvalidFilter);
            }
        }
        // Every secondary item type needs an id.
        if self.request.secondary_item_type.is_some() && self.request.id.is_none() {
            return Err(Error::InvalidSecondaryItemType);
        }

        Ok(self.request)
    }
}

/// This struct represents a request to the API.
/// It should be created with the [`RequestBuilder`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Request {
    item_type: ItemType,
    id: Option<String>,
    secondary_item_type: Option<ItemType>,
    sort: Option<Sort>,
    filter: Option<Filter>,
    pagination: Option<Pagination>,
}

impl Request {
    pub(crate) fn new(item_type: ItemType) -> Self {
        Self {
            item_type,
            id: None,
            secondary_item_type: None,
            sort: None,
            filter: None,
            pagination: None,
        }
    }

    pub(crate) fn get_item_type(&self) -> ItemType {
        if let Some(secondary_item_type) = &self.secondary_item_type {
            secondary_item_type.clone()
        } else {
            self.item_type.clone()
        }
    }
}

impl GetUrl for Request {
    fn get_url(&self) -> String {
        let mut url = self.item_type.get_url();
        if let Some(id) = &self.id {
            url.push_str(&format!("/{}", id));
        }
        if let Some(secondary_item_type) = &self.secondary_item_type {
            url.push_str(&format!("/{}", secondary_item_type.get_url()));
        }

        let mut aditional_url = vec![];
        if let Some(sort) = &self.sort {
            aditional_url.push(sort.get_url());
        }
        if let Some(filter) = &self.filter {
            aditional_url.push(filter.get_url());
        }
        if let Some(pagination) = &self.pagination {
            aditional_url.push(pagination.get_url());
        }

        if !aditional_url.is_empty() {
            url.push('?');
            url.push_str(&aditional_url.join("&"));
        }

        url
    }
}

/// Wrapper for the [`reqwest::Client`] struct that contains the token
/// and the actual url that is used to make the request.
/// It is used to make requests to the API.
pub(crate) struct Requester {
    token: String,
}

impl Requester {
    pub(crate) fn new(token: String) -> Self {
        Self { token }
    }

    pub(crate) async fn get(&self, url: &str) -> Result<String, reqwest::Error> {
        let mut headers = HeaderMap::new();
        headers.insert(
            header::ACCEPT,
            HeaderValue::from_str("application/json")
                .expect("Failed to convert header to header value"),
        );
        headers.insert(
            header::AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {}", self.token))
                .expect("Failed to convert header to header value"),
        );

        let client = reqwest::Client::new();
        match client
            .get(format!("https://the-one-api.dev/v2/{}", url))
            .headers(headers)
            .send()
            .await
        {
            Ok(response) => {
                let response = response.error_for_status()?;
                response.text().await
            }
            Err(e) => Err(e),
        }
    }

    pub(crate) async fn get_from_request(
        &self,
        request: Request,
    ) -> Result<String, reqwest::Error> {
        let url = request.get_url();
        self.get(&url).await
    }
}

#[cfg(test)]
mod tests {

    use crate::{
        attribute::{Attribute, BookAttribute, QuoteAttribute},
        filter::Operator,
        request::sort::SortOrder,
    };

    use super::*;

    #[test]
    fn test_simple_request_url() {
        let request = RequestBuilder::new(ItemType::Book).build().unwrap();
        assert_eq!(request.get_url(), "book");
    }

    #[test]
    fn test_request_with_id_url() {
        let request = RequestBuilder::new(ItemType::Book)
            .id("123".to_string())
            .build()
            .unwrap();
        assert_eq!(request.get_url(), "book/123");
    }

    #[test]
    fn test_request_with_secondary_item_type_url() {
        let request = RequestBuilder::new(ItemType::Book)
            .secondary_item_type(ItemType::Chapter)
            .build();
        assert!(request.is_err());

        let request = RequestBuilder::new(ItemType::Character)
            .id("123".to_string())
            .secondary_item_type(ItemType::Quote)
            .build()
            .unwrap();

        assert_eq!(request.get_url(), "character/123/quote");
    }

    #[test]
    fn test_request_with_sort_url() {
        let request = RequestBuilder::new(ItemType::Book)
            .sort(Sort::new(
                SortOrder::Ascending,
                Attribute::Book(BookAttribute::Name),
            ))
            .build()
            .unwrap();

        assert_eq!(request.get_url(), "book?sort=name:asc");
    }

    #[test]
    fn test_request_with_filter_url() {
        let request = RequestBuilder::new(ItemType::Book)
            .filter(Filter::Match(
                Attribute::Book(BookAttribute::Name),
                Operator::Eq,
                vec!["The Fellowship of the Ring".to_string()],
            ))
            .build()
            .unwrap();

        assert_eq!(request.get_url(), "book?name=The Fellowship of the Ring");
    }

    #[test]
    fn test_request_with_pagination_url() {
        let request = RequestBuilder::new(ItemType::Book)
            .pagination(Pagination::new(10, 10, 2))
            .build()
            .unwrap();

        assert_eq!(request.get_url(), "book?limit=10&offset=10&page=2");
    }

    #[test]
    fn test_full_request_url() {
        let request = RequestBuilder::new(ItemType::Character)
            .id("123".to_string())
            .secondary_item_type(ItemType::Quote)
            .sort(Sort::new(
                SortOrder::Ascending,
                Attribute::Quote(QuoteAttribute::Dialog),
            ))
            .filter(Filter::Match(
                Attribute::Quote(QuoteAttribute::Dialog),
                Operator::Eq,
                vec!["Deagol!".to_string()],
            ))
            .pagination(Pagination::new(10, 10, 2))
            .build()
            .unwrap();

        assert_eq!(
            request.get_url(),
            "character/123/quote?sort=dialog:asc&dialog=Deagol!&limit=10&offset=10&page=2"
        );
    }
}