kvv_efa_api/request/
departure_monitor.rs1use super::{Request, API_ENDPOINT, types};
2use crate::response::DepartureMonitorResponseData;
3
4#[derive(Clone, Debug)]
5pub struct DepartureMonitorRequest(String);
6
7impl Request for DepartureMonitorRequest {
8 type Builder = DepartureMonitorRequestBuilder;
9 type Response = DepartureMonitorResponseData;
10 const REQUEST_TYPE: &'static str = "XSLT_DM_REQUEST";
11
12 fn url(&self) -> &String {
13 &self.0
14 }
15
16 fn into_url(self) -> String {
17 self.0
18 }
19
20 #[cfg(feature = "reqwest")]
21 async fn get(self) -> Result<Self::Response, reqwest::Error> {
22 let response = reqwest::get(self.url()).await?;
23 response.json().await
24 }
25}
26
27pub struct DepartureMonitorRequestBuilder {
28 name: types::StationId,
29 typ: types::Type,
30 use_realtime: bool,
31 limit: usize
32}
33
34impl Default for DepartureMonitorRequestBuilder {
35 fn default() -> Self {
36 Self {
37 name: 0,
38 typ: types::Type::Stop,
39 use_realtime: true,
40 limit: 10
41 }
42 }
43}
44
45impl DepartureMonitorRequestBuilder {
46 const DEFAULT_OPTIONS: &'static str = "&coordOutputFormat=WGS84[dd.ddddd]&depType=stopEvents&locationServerActive=1&mode=direct&useOnlyStops=1";
47
48 pub fn build(self) -> DepartureMonitorRequest {
49 let mut url = format!("{API_ENDPOINT}/{}?outputFormat=JSON{}", DepartureMonitorRequest::REQUEST_TYPE, Self::DEFAULT_OPTIONS);
50 url.push_str(&format!("&name_dm={}", self.name));
51 url.push_str(&format!("&type_dm={}", self.typ));
52 url.push_str(&format!("&useRealtime={}", self.use_realtime as i32));
53 url.push_str(&format!("&limit={}", self.limit));
54
55 DepartureMonitorRequest(url)
56 }
57
58 pub fn name(mut self, name: types::StationId) -> Self {
59 self.name = name;
60 self
61 }
62
63 pub fn typ(mut self, typ: types::Type) -> Self {
64 self.typ = typ;
65 self
66 }
67
68 pub fn realtime(mut self, realtime: bool) -> Self {
69 self.use_realtime = realtime;
70 self
71 }
72
73 pub fn limit(mut self, limit: usize) -> Self {
74 self.limit = limit;
75 self
76 }
77}
78