1 | use std::fmt::Display; |
2 | use chrono::{DateTime, TimeZone}; |
3 | |
4 | use super::BaseApi; |
5 | use crate::{Client, Query, Language, Future}; |
6 | |
7 | pub struct FutureApi<'a, Tz: TimeZone> |
8 | where |
9 | Tz::Offset: Display |
10 | { |
11 | client: &'a Client, |
12 | query: Option<Query>, |
13 | dt: Option<DateTime<Tz>>, |
14 | lang: Option<Language> |
15 | } |
16 | |
17 | impl<'a, Tz: TimeZone> FutureApi<'a, Tz> |
18 | where |
19 | Tz::Offset: Display |
20 | { |
21 | /// Use [`crate::Client`] |
22 | pub fn new(client: &'a Client) -> Self { |
23 | Self { |
24 | client, |
25 | query: None, |
26 | dt: None, |
27 | lang: None |
28 | } |
29 | } |
30 | |
31 | /// Set up the query |
32 | /// |
33 | /// Query parameter based on which data is sent back |
34 | pub fn query(mut self, query: Query) -> Self { |
35 | self.query = Some(query); |
36 | self |
37 | } |
38 | |
39 | /// Set up a datetime |
40 | /// |
41 | /// `dt` should be between 14 days and 300 days from today in the future |
42 | pub fn dt(mut self, dt: DateTime<Tz>) -> Self { |
43 | self.dt = Some(dt); |
44 | self |
45 | } |
46 | |
47 | /// Set up language |
48 | /// |
49 | /// `condition:text` field in API in the desired language |
50 | pub fn lang(mut self, lang: Language) -> Self { |
51 | self.lang = Some(lang); |
52 | self |
53 | } |
54 | } |
55 | |
56 | impl<'a, Tz: TimeZone> BaseApi<'a> for FutureApi<'a, Tz> |
57 | where |
58 | Tz::Offset: Display |
59 | { |
60 | type Model = Future; |
61 | |
62 | fn path(&self) -> &str { |
63 | "future" |
64 | } |
65 | |
66 | fn client(&self) -> &'a Client { |
67 | self.client |
68 | } |
69 | |
70 | fn params(&self) -> Vec<(&str, String)> { |
71 | let query = self.query.as_ref().unwrap(); |
72 | let dt = self.dt.as_ref().unwrap(); |
73 | |
74 | let mut params = vec![ |
75 | ("key" , self.client.api_key.clone()), ("q" , query.to_string()), |
76 | ("dt" , format!(" {}" , dt.format("%Y-%m-%d" ))), |
77 | ]; |
78 | |
79 | if let Some(lang) = &self.lang { |
80 | params.push(("lang" , lang.to_string())) |
81 | } |
82 | |
83 | params |
84 | } |
85 | } |
86 | |