Skip to main content

nerva/clients/donki/
cme.rs

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