Crates.io | openweathermap_client |
lib.rs | openweathermap_client |
version | 0.4.0 |
source | src |
created_at | 2022-10-07 19:04:50.251424 |
updated_at | 2024-05-31 03:21:21.804758 |
description | A client for querying the weather from the free OpenWeatherMap API. |
homepage | https://github.com/evaneaston/openweathermap |
repository | https://github.com/evaneaston/openweathermap |
max_upload_size | |
id | 683030 |
size | 65,931 |
openweathermap_client
is a rust library that provides a client for querying OpenWeatherMap's free version 2.5 weather API. |
Standard
, Metric
, or Imperial
unit systems.To obtain an OpenWeatherMap API Key, see this section.
cargo add openweathermap_client
Get the temperature in °C and description of the weather in Paris right now.
use openweathermap_client::models::{City, UnitSystem};
use openweathermap_client::{error::ClientError, Client, ClientOptions};
#[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(())
}
Reuse a client to obtain readings at several different locations with different query types.
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(())
}
See openweathermap_exporter for a service that allow collecting weather data for a multiple locations and sharing this with metrics aggregators by publishing the readings via a prometheus exposition formatted exporter.