1 | use super::BaseApi; |
2 | use crate::{Client, Query, Language, Realtime}; |
3 | |
4 | pub struct RealtimeApi<'a> { |
5 | client: &'a Client, |
6 | query: Option<Query>, |
7 | aqi: bool, |
8 | lang: Option<Language> |
9 | } |
10 | |
11 | impl<'a> RealtimeApi<'a> { |
12 | /// Use [`crate::Client`] |
13 | pub fn new(client: &'a Client) -> Self { |
14 | Self { |
15 | client, |
16 | query: None, |
17 | aqi: false, |
18 | lang: None |
19 | } |
20 | } |
21 | |
22 | /// Set up the query |
23 | /// |
24 | /// Query parameter based on which data is sent back |
25 | pub fn query(mut self, query: Query) -> Self { |
26 | self.query = Some(query); |
27 | self |
28 | } |
29 | |
30 | /// Set up Air Quality Data |
31 | /// |
32 | /// Air quality data is not passed by default. |
33 | pub fn aqi(mut self) -> Self { |
34 | self.aqi = true; |
35 | self |
36 | } |
37 | |
38 | /// Set up language |
39 | /// |
40 | /// `condition:text` field in API in the desired language |
41 | pub fn lang(mut self, lang: Language) -> Self { |
42 | self.lang = Some(lang); |
43 | self |
44 | } |
45 | } |
46 | |
47 | impl<'a> BaseApi<'a> for RealtimeApi<'a> { |
48 | type Model = Realtime; |
49 | |
50 | fn path(&self) -> &str { |
51 | "current" |
52 | } |
53 | |
54 | fn client(&self) -> &'a Client { |
55 | self.client |
56 | } |
57 | |
58 | fn params(&self) -> Vec<(&str, String)> { |
59 | let query = self.query.as_ref().unwrap(); |
60 | let mut params = vec![ |
61 | ("key" , self.client.api_key.clone()), ("q" , query.to_string()), |
62 | ("aqi" , if self.aqi { "yes" .to_string() } else { "no" .to_string() }) |
63 | ]; |
64 | |
65 | if let Some(lang) = &self.lang { |
66 | params.push(("lang" , lang.to_string())) |
67 | } |
68 | |
69 | params |
70 | } |
71 | } |
72 | |