Skip to main content

sbb_api/
connections.rs

1use chrono::{Utc};
2use simple_error::SimpleError;
3
4use crate::make_request;
5use crate::models::location::LocationType;
6use crate::models::results::VerbindungenResults;
7
8pub async fn get_connections(from: &str, from_type: LocationType, to: &str, to_type: LocationType,
9                             on: &chrono::DateTime<Utc>) -> Result<VerbindungenResults, SimpleError> {
10    let base_path = "/unauth/fahrplanservice/v1/verbindungen/";
11
12    // s/Zurich/s/Bern/ab/2019-09-20/10-14/";
13    let date = on.format("%Y-%m-%d");
14    let time = on.format("%H-%M");
15
16    let addition = format!("{}/{}/{}/{}/ab/{}/{}/", from_type,
17                           from,
18                           to_type,
19                           to,
20                           date,
21                           time);
22    let path = format!("{}{}", base_path, addition);
23
24    let resp = make_request(&path).await.expect("Invalid request");
25
26    if !resp.status().is_success() {
27        bail!("status is not success")
28    }
29
30    let response = &resp.text().await.unwrap();
31
32    Ok(serde_json::from_str(
33        response
34    ).unwrap())
35}
36
37mod tests {
38    use chrono::{Datelike, TimeZone, Utc};
39    use crate::connections::get_connections;
40    use crate::models::location::LocationType;
41
42    #[actix_rt::test]
43    pub async fn test_get_connection() {
44        let today = chrono::offset::Local::now();
45        let date = chrono::Utc.ymd(today.year(), today.month(), today.day()).and_hms(12, 0, 0);
46        let conn = get_connections("Zürich HB",
47                                   LocationType::Station,
48                                   "Basel",
49                                   LocationType::Station,
50                                   &date);
51        let conn_res = conn.await;
52        assert!(conn_res.is_ok());
53        let verbindungen_res = conn_res.unwrap();
54        assert!(verbindungen_res.verbindungen.len() > 0);
55        println!("Connections = {:?}", verbindungen_res);
56    }
57
58    #[actix_rt::test]
59    pub async fn test_get_connection2() {
60        let today = chrono::offset::Local::now();
61        let date = Utc.ymd(today.year(), today.month(), today.day()).and_hms(12, 0, 0);
62        let conn = get_connections("Chiasso",
63                                   LocationType::Station,
64                                   "Zürich HB",
65                                   LocationType::Station,
66                                   &date);
67        let conn_res = conn.await;
68        assert!(conn_res.is_ok());
69        let verbindungen_res = conn_res.unwrap();
70        assert!(verbindungen_res.verbindungen.len() > 0);
71        println!("Connections = {:?}", verbindungen_res);
72    }
73
74    #[actix_rt::test]
75    pub async fn test_get_connections3() {
76        let today = chrono::offset::Local::now();
77        let date =
78            Utc.ymd(today.year(), today.month(), today.day())
79                .and_hms(12, 0, 0);
80        let conn = get_connections(
81            "Zürich HB",
82            LocationType::Station,
83            "Dübendorf, Bahnof",
84            LocationType::Station,
85            &date,
86        );
87
88        let conn_res = conn.await;
89        assert!(conn_res.is_ok());
90        let verbindungen_res = conn_res.unwrap();
91        println!("Connections = {}", verbindungen_res)
92    }
93
94    #[actix_rt::test]
95    pub async fn test_get_connections4() {
96        let today = chrono::offset::Local::now();
97        let date =
98            Utc.ymd(today.year(), today.month(), today.day())
99                .and_hms(12, 0, 0);
100        let conn = get_connections("Chiasso",
101                                   LocationType::Address,
102                                   "Zürich",
103                                   LocationType::Address,
104                                   &date,
105        );
106
107        let conn_res = conn.await;
108        assert!(conn_res.is_ok());
109        let verbindungen_res = conn_res.unwrap();
110        println!("Connections = {}", verbindungen_res);
111
112        for c in verbindungen_res.verbindungen {
113            println!("Conn: {}, Duration = {:?}", c, c.duration());
114        }
115    }
116
117    #[actix_rt::test]
118    pub async fn test_get_connections5() {
119        let today = chrono::offset::Local::now();
120        let date =
121            Utc.ymd(today.year(), today.month(), today.day())
122                .and_hms(12, 0, 0);
123        let conn = get_connections("Im Tiergraten, 8055 Zürich, Zürich",
124                                   LocationType::Address,
125                                   "8005 Zürich, Hardturmstrasse 3",
126                                   LocationType::Address,
127                                   &date,
128        );
129
130        let conn_res = conn.await;
131        assert!(conn_res.is_ok());
132        let verbindungen_res = conn_res.unwrap();
133        println!("Connections = {}", verbindungen_res);
134
135        for c in verbindungen_res.verbindungen {
136            println!("Conn: {}, Duration = {:?}", c, c.duration());
137        }
138    }
139}