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
use chrono::{Date, Utc};
use serde::{ser::SerializeMap, Serialize, Serializer};

use crate::day::Day;
use crate::error::RequestError;
use crate::BASE_URL;

/// Struct to create and then issue requests to see a range of opening days.
/// # Example
/// ```rust
/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
/// use openmensa_rs::request::DayRequest;
///
/// // As an example we just use the canteen with id `1` here
/// let req = DayRequest::new(1);
/// let list_of_days = req.build().await.unwrap();
/// println!("{:?}", list_of_days);
/// # })
/// ```
#[derive(Clone)]
pub struct DayRequest {
    start: Option<Date<Utc>>,
    canteen_id: u16,
}

impl Serialize for DayRequest {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serializer.serialize_map(Some(1)).unwrap();
        let date_string: Option<String> = self.start.map(|e| e.format("%Y-%m-%d").to_string());
        map.serialize_entry("start", &date_string)?;
        map.end()
    }
}

impl DayRequest {
    /// Create a new instance to get opening days from a specific canteen.
    /// If you want to issue multiple requests, this struct implements `Clone` so before you issue a request simply `.clone()` if you need it later on.
    pub fn new(canteen_id: u16) -> Self {
        Self {
            start: None,
            canteen_id,
        }
    }

    /// Specfiy a start date from which opening days should be returned from.
    /// By default this is the current date, but you may specify another if you want e.g. to know the dates in a week or two.
    pub fn with_start_date(mut self, start_date: Date<Utc>) -> Self {
        self.start = Some(start_date);
        self
    }

    /// Send the request and wait for the response.
    /// May return an error if the parameters cannot be serialized or the response cannot be deserialized.
    ///
    /// # Example
    /// ```rust
    /// # tokio::runtime::Runtime::new().unwrap().block_on(async {
    /// use openmensa_rs::request::DayRequest;
    ///
    /// let canteen_id = 1;
    /// let open_days = DayRequest::new(canteen_id)
    ///     .with_start_date(
    ///         chrono::Utc::today()
    ///     )
    ///     .build()
    ///     .await
    ///     .unwrap();
    /// # })
    /// ```
    pub async fn build(self) -> Result<Vec<Day>, RequestError> {
        let list_json = reqwest::Client::new()
            .get(url::Url::parse(
                format!("{}/canteens/{}/days", BASE_URL, self.canteen_id).as_str(),
            )?)
            .query(&self)
            .send()
            .await?
            .text()
            .await?;
        let canteens: Vec<Day> = serde_json::from_str(&list_json)?;
        Ok(canteens)
    }
}