owm_rs/
lib.rs

1mod owm;
2#[cfg(test)]
3mod tests;
4mod utils;
5
6pub mod owm_structs {
7    pub use crate::owm::geocoding::structures::*;
8    pub use crate::owm::weather::structures::*;
9}
10
11pub mod owm_api {
12    use crate::{owm, owm_structs};
13
14    /// Gets the coordinates of a city by its name
15    ///
16    /// # Arguments
17    /// * `city_name` - The city name to look for
18    ///
19    /// * `api_key` - Your OWM API key
20    pub async fn get_city_coordinates(
21        city_name: String,
22        api_key: String,
23    ) -> Result<owm_structs::Coordinates, reqwest::Error> {
24        owm::geocoding::api::get_coordinates_by_location_name(city_name, api_key).await
25    }
26
27    /// Gets the weather data given input coordinates.
28    ///
29    /// # Arguments
30    /// * `latitude` - The latitude of the target location
31    ///
32    /// * `longitude` - The longitude of the target location
33    ///
34    /// * `api_key` - Your API key
35    pub async fn get_weather_by_coordinates(
36        latitude: f32,
37        longitude: f32,
38        api_key: String,
39    ) -> Result<owm_structs::WeatherData, reqwest::Error> {
40        owm::weather::api::get_weather_for_coordinates(latitude, longitude, api_key).await
41    }
42
43    /// Combines searching for the coordinates of a city by name + Fetching the weather data for the location
44    ///
45    /// # Arguments
46    /// * `city_name` - The city name to look for
47    ///
48    /// * `api_key` - Your OWM API key
49    pub async fn get_weather_by_city(
50        city_name: String,
51        api_key: String,
52    ) -> Result<owm_structs::WeatherData, reqwest::Error> {
53        let coordinates: owm::geocoding::structures::Coordinates =
54            owm::geocoding::api::get_coordinates_by_location_name(city_name, api_key.clone())
55                .await?;
56        owm::weather::api::get_weather_for_coordinates(
57            coordinates.get_latitude(),
58            coordinates.get_longitude(),
59            api_key,
60        )
61        .await
62    }
63
64    pub mod blocking {
65        use crate::owm_structs;
66        use tokio::runtime::Runtime;
67
68        fn get_runtime() -> Runtime {
69            tokio::runtime::Builder::new_multi_thread()
70                .enable_all()
71                .build()
72                .expect("Could not build tokio instance.")
73        }
74
75        /// Gets the coordinates of a city by its name
76        ///
77        /// # Arguments
78        /// * `city_name` - The city name to look for
79        ///
80        /// * `api_key` - Your OWM API key
81        pub fn get_city_coordinates(
82            city_name: String,
83            api_key: String,
84        ) -> Result<owm_structs::Coordinates, reqwest::Error> {
85            get_runtime().block_on(async move {
86                crate::owm_api::get_city_coordinates(city_name, api_key).await
87            })
88        }
89
90        /// Gets the weather data given input coordinates.
91        ///
92        /// # Arguments
93        /// * `latitude` - The latitude of the target location
94        ///
95        /// * `longitude` - The longitude of the target location
96        ///
97        /// * `api_key` - Your API key
98        pub fn get_weather_by_coordinates(
99            latitude: f32,
100            longitude: f32,
101            api_key: String,
102        ) -> Result<owm_structs::WeatherData, reqwest::Error> {
103            get_runtime().block_on(async move {
104                crate::owm_api::get_weather_by_coordinates(latitude, longitude, api_key).await
105            })
106        }
107
108        /// Combines searching for the coordinates of a city by name + Fetching the weather data for the location
109        ///
110        /// # Arguments
111        /// * `city_name` - The city name to look for
112        ///
113        /// * `api_key` - Your OWM API key
114        pub fn get_weather_by_city(
115            city_name: String,
116            api_key: String,
117        ) -> Result<owm_structs::WeatherData, reqwest::Error> {
118            get_runtime().block_on(async move {
119                crate::owm_api::get_weather_by_city(city_name, api_key).await
120            })
121        }
122    }
123}
124
125pub mod owm_utils {
126    pub mod convert {
127        pub use crate::utils::convert::*;
128    }
129}
130
131pub mod prelude {
132    // Main
133    pub use crate::owm_api::*;
134    pub use crate::owm_structs::*;
135    // Features
136    #[cfg(feature = "utils")]
137    pub use crate::owm_utils::*;
138}