1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
pub mod open_weather_types;
use open_weather_types::*;
use reqwest::Client;
type Error = Box<dyn std::error::Error>;
type Result<T, E = Error> = std::result::Result<T, E>;
pub async fn get_weather(appid: String, zip: String, unit: String) -> Result<OpenWeather, Error> {
let client = Client::new();
let uri = format!(
"https://api.openweathermap.org/data/2.5/weather?appid={}&zip={}&units={}",
appid,
zip,
unit
);
let req = client.get(&uri);
let res = req.send().await?;
let body = res.text().await?;
let weather: OpenWeather = serde_json::from_str(&body)?;
Ok(weather)
}