Skip to main content

nerva/clients/
neo.rs

1use crate::core::{Client, Params};
2use std::error::Error;
3
4/// Retrieve a list of Asteroids based on their closest approach date to Earth
5pub mod feed
6{
7    use super::{Client, Error, Params};
8
9    /// Params for the Neo <feed> API
10    #[derive(Clone, Copy, Debug, PartialEq)]
11    pub enum NeoFeedParams<'p>
12    {
13        #[doc = "Start date of the interval"]
14        StartDate(&'p str),
15        #[doc = "End date of the interval"]
16        EndDate(&'p str),
17    }
18
19    impl<'p> Into<String> for NeoFeedParams<'p>
20    {
21        fn into(self) -> String
22        {
23            match self
24            {
25                NeoFeedParams::StartDate(date) => format!("start_date={}", date),
26                NeoFeedParams::EndDate(date) => format!("end_date={}", date),
27            }
28        }
29    }
30
31    impl<'p> Default for NeoFeedParams<'p>
32    {
33        fn default() -> Self { return NeoFeedParams::StartDate("2023-01-01") }
34    }
35
36    impl<'p> Params for NeoFeedParams<'p> {}
37
38    /// Neo <feed> client
39    #[derive(Clone, Debug)]
40    pub struct NeoFeed {}
41
42    #[allow(missing_docs)]
43    impl NeoFeed
44    {
45        pub fn new() -> Self { return NeoFeed::default() }
46    }
47
48    impl Default for NeoFeed
49    {
50        fn default() -> Self { return NeoFeed {} }
51    }
52
53    impl<'p, PARAMS> Client<PARAMS> for NeoFeed
54    where
55        PARAMS: Params,
56    {
57        const BASE_URL: &'static str = "https://api.nasa.gov/neo/rest/v1/feed";
58        type Response = serde_json::Value;
59
60        fn get(&self, params: PARAMS) -> Result<Self::Response, Box<dyn Error>>
61        where
62            PARAMS: Params,
63        {
64            let base_url = <NeoFeed as Client<PARAMS>>::BASE_URL;
65            let url_with_params = format!("{}?{}", base_url, params.into());
66            let url_with_key = crate::prelude::keys::include(&url_with_params)?;
67            let response = ureq::get(&url_with_key).call()?;
68            let json = serde_json::json!(response.into_string()?);
69            Ok(json)
70        }
71    }
72
73    #[cfg(test)]
74    mod tests
75    {
76        use super::{NeoFeed, NeoFeedParams};
77        use crate::core::Client;
78
79        #[test]
80        fn test_neo()
81        {
82            let neo = NeoFeed::default();
83            let params: String = NeoFeedParams::default().into();
84            println!("params: {}", params);
85            let response = neo.get(NeoFeedParams::default());
86            match response
87            {
88                Ok(json) => println!("{:#?}", json),
89                Err(e) =>
90                {
91                    println!("{}", e);
92                    panic!();
93                }
94            }
95        }
96    }
97}
98
99/// Lookup a specific Asteroid based on its NASA JPL small body (SPK-ID) ID
100pub mod lookup
101{
102    use super::{Client, Error, Params};
103
104    /// Params for the Neo <lookup> API
105    #[derive(Clone, Copy, Debug, PartialEq)]
106    pub enum NeoLookupParams<'p>
107    {
108        #[doc = "Asteroid SPK-ID correlates to the NASA JPL small body"]
109        AsteroidID(&'p str),
110    }
111
112    impl<'p> Into<String> for NeoLookupParams<'p>
113    {
114        fn into(self) -> String
115        {
116            match self
117            {
118                NeoLookupParams::AsteroidID(id) => format!("asteroid_id={}", id),
119            }
120        }
121    }
122
123    impl<'p> Default for NeoLookupParams<'p>
124    {
125        fn default() -> Self { return NeoLookupParams::AsteroidID("2021277") }
126    }
127
128    impl<'p> Params for NeoLookupParams<'p> {}
129
130    /// Neo <lookup> client
131    #[derive(Clone, Debug)]
132    pub struct NeoLookup {}
133
134    impl Default for NeoLookup
135    {
136        fn default() -> Self { return NeoLookup {} }
137    }
138
139    #[allow(missing_docs)]
140    impl NeoLookup
141    {
142        pub fn new() -> Self { return NeoLookup::default() }
143    }
144
145    impl<'p, PARAMS> Client<PARAMS> for NeoLookup
146    where
147        PARAMS: Params,
148    {
149        const BASE_URL: &'static str = "https://api.nasa.gov/neo/rest/v1/neo";
150        type Response = serde_json::Value;
151
152        fn get(&self, params: PARAMS) -> Result<Self::Response, Box<dyn Error>>
153        where
154            PARAMS: Params,
155        {
156            let base_url = <NeoLookup as Client<PARAMS>>::BASE_URL;
157            let url_with_params = format!("{}?{}", base_url, params.into());
158            let url_with_key = crate::prelude::keys::include(&url_with_params)?;
159            let response = ureq::get(&url_with_key).call()?;
160            let json = serde_json::json!(response.into_string()?);
161            Ok(json)
162        }
163    }
164}