kvv_efa_api/request/
stop_finder.rs

1use crate::response::StopFinderResponseData;
2
3use super::{Request, types, API_ENDPOINT};
4
5#[derive(Debug, Clone)]
6pub struct StopFinderRequest(String);
7
8impl Request for StopFinderRequest {
9    type Builder = StopFinderRequestBuilder<'static>;
10    type Response = StopFinderResponseData;
11
12    const REQUEST_TYPE: &'static str = "XSLT_STOPFINDER_REQUEST";
13
14    fn url(&self) -> &String {
15        &self.0
16    }
17
18    fn into_url(self) -> String {
19        self.0.into()
20    }
21
22    #[cfg(feature = "reqwest")]
23    async fn get(self) -> Result<Self::Response, reqwest::Error> {
24        let response = reqwest::get(self.url()).await?;
25        response.json().await
26    }
27}
28
29pub struct StopFinderRequestBuilder<'a> {
30    name: &'a str,
31    typ: types::Type,
32    limit: usize
33}
34
35impl<'a> Default for StopFinderRequestBuilder<'a> {
36    fn default() -> Self {
37        Self {
38            name: "",
39            typ: types::Type::Any,
40            limit: 10
41        }
42    }
43}
44
45impl<'a> StopFinderRequestBuilder<'a> {
46    const DEFAULT_OPTIONS: &'static str = "&coordOutputFormat=WGS84[dd.ddddd]";
47
48    pub fn build(self) -> StopFinderRequest {
49        let mut url = format!("{API_ENDPOINT}/{}?outputFormat=JSON{}", StopFinderRequest::REQUEST_TYPE, Self::DEFAULT_OPTIONS);
50        url.push_str(&format!("&name_sf={}", self.name));
51        url.push_str(&format!("&type_sf={}", self.typ));
52        url.push_str(&format!("&limit={}", self.limit));
53
54        StopFinderRequest(url)
55    }
56
57    pub fn name(mut self, name: &'a str) -> Self {
58        self.name = name;
59        self
60    }
61
62    pub fn typ(mut self, typ: types::Type) -> Self {
63        self.typ = typ;
64        self
65    }
66
67    pub fn limit(mut self, limit: usize) -> Self {
68        self.limit = limit;
69        self
70    }
71}