Skip to main content

nerva/clients/donki/
gst.rs

1use crate::prelude::{Client, Params};
2use std::error::Error;
3
4#[derive(Copy, Clone, Debug, PartialEq)]
5pub enum GSTParams<'p>
6{
7    StartDate(&'p str),
8    EndDate(&'p str),
9    Empty,
10}
11
12impl Default for GSTParams<'_>
13{
14    fn default() -> Self { GSTParams::Empty }
15}
16
17impl<'p> Into<String> for GSTParams<'p>
18{
19    fn into(self) -> String
20    {
21        match self
22        {
23            GSTParams::StartDate(date) => format!("startDate={}", date),
24            GSTParams::EndDate(date) => format!("endDate={}", date),
25            GSTParams::Empty => String::new(),
26        }
27    }
28}
29
30impl<'p> Params for GSTParams<'p> {}
31
32#[derive(Debug, Clone)]
33pub struct GST {}
34
35impl Default for GST
36{
37    fn default() -> Self { GST {} }
38}
39
40impl GST
41{
42    pub fn new() -> GST { GST::default() }
43}
44
45impl<'p, PARAMS> Client<PARAMS> for GST
46where
47    PARAMS: Params,
48{
49    const BASE_URL: &'static str = "https://api.nasa.gov/DONKI/GST";
50    type Response = serde_json::Value;
51
52    fn get(&self, params: PARAMS) -> Result<Self::Response, Box<dyn Error>>
53    {
54        let base_url = <GST as Client<PARAMS>>::BASE_URL;
55        let url_with_params = format!("{}?{}", base_url, params.into());
56        let url_with_key = crate::prelude::keys::include(&url_with_params)?;
57        if cfg!(test)
58        {
59            println!("URL: {}", url_with_key)
60        };
61        let response = ureq::get(&url_with_key).call()?;
62        let json_str = serde_json::json!(response.into_string()?);
63        return Ok(json_str);
64    }
65}
66
67#[cfg(test)]
68mod gst_test
69{
70    use super::*;
71
72    #[test]
73    fn test_gst()
74    {
75        let gst = GST::new();
76        let params = GSTParams::default();
77        let response = gst.get(params);
78        match response
79        {
80            Ok(json) => println!("{:#?}", json),
81            Err(e) =>
82            {
83                println!("{}", e);
84                panic!()
85            }
86        }
87    }
88}