Crate openweathermap_client
source ·Expand description
§Get An API Key
To obtain an API key, go to https://openweathermap.org/home/sign_in to sign in or create an account. Once logged in, select your user name from the top-right menu bar and then My API Keys. Use the Create key form to create a new key.
§Example #1
use openweathermap_client::models::{City, UnitSystem};
use openweathermap_client::{error::ClientError, Client, ClientOptions};
/// Gets the temperature in °C and description of the weather in Paris right now
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), ClientError> {
let options = ClientOptions {
units: UnitSystem::Metric,
language: "fr".to_string(),
..ClientOptions::default() // loads API_KEY env var
};
let client = Client::new(options)?;
let reading = client.fetch_weather(&City::new("Paris", "FR")).await?;
println!(
"The temperature and weather in France in French is {}, {}",
reading.main.temp, reading.weather[0].description
);
Ok(())
}
§Example #2
use openweathermap_client::models::{City, CityId, Coord};
use openweathermap_client::{error::ClientError, Client, ClientOptions, Query};
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), ClientError> {
// reuse a client for several readings (must be run in an async context)
let client = Client::new(ClientOptions::default())?; // loads API_KEY env var
let v: Vec<Box<dyn Query>> = vec![
Box::new(City::new("Lages", "BR")),
Box::new(CityId::new(3369157)),
Box::new(Coord::new(61.1595054, -45.4409551)),
];
for query in v {
let weather = client.fetch_weather(query.as_ref()).await?;
println!("The weather for {} is {:?}", query, weather);
}
Ok(())
}
Modules§
Structs§
- Options to configure the Client.
Traits§
- Abstraction of a query against the
OpenWeatherMap
API.