Crates.io | openweathermap |
lib.rs | openweathermap |
version | 0.2.4 |
source | src |
created_at | 2021-05-16 15:55:28.60958 |
updated_at | 2021-05-27 20:11:32.757149 |
description | easy access current weather data from OpenWeatherMap |
homepage | https://openweathermap.thats-software.com |
repository | |
max_upload_size | |
id | 398229 |
size | 232,704 |
...is a rust crate which lets you easily access current weather data from OpenWeatherMap. This is an unofficial extension I have made to learn rust a little but I hope you have fun with it.
First add this crate to your dependencies in you Cargo.toml
file:
[dependencies]
openweathermap = "0.2.3"
Then use the crate in your rust source file by calling openweathermap::init()
which returns a receiver object.
You can then use this receiver object to call openweathermap::update()
to get weather updates like in the following example:
extern crate openweathermap;
use openweathermap::{init,update};
fn main() {
// start our observatory via OWM
let receiver = &init("Berlin,DE", "metric", "en", "<APIKEY>", 10);
loop {
match update(receiver) {
Some(response) => match response {
Ok(current) => println!(
"Today's weather in {} is {}",
current.name.as_str(),
current.weather[0].main.as_str()
),
Err(e) => println!("Could not fetch weather because: {}", e),
},
None => (),
}
}
}
init()
spawns a thread which then will periodically poll OpenWeatherMap for the latest current weather report.
You then can use update()
to ask for it.
There are three possible kinds of result you get from update()
which you will have to face:
None
update()
returns None
if there is currently no new update available.
Which means: You wont get any update twice!
In other words: update()
is not caching the last weather update for you.
CurrentWeather
If a new update was downloaded by the polling thread update()
returns some CurrentWeather
object.
CurrentWeather
is a nested struct
with the already parsed json properties.
Those are well described here.
Err
On error update()
returns some String
object which includes a brief error description.
Errors may occur...
Err
which includes exactly the String "loading..."
(predefined in openweathermap::LOADING
).401 Unauthorized
if an invalid API key was used).If you need the weather just once you may use the method weather()
which envelopes init()
and update()
into one single synchronous or asynchronous call.
After the first successful weather update the spawned thread will stop immediately and you get the result in return.
extern crate openweathermap;
use openweathermap::blocking::weather;
fn main() {
// start our observatory via OWM
match &weather("Berlin,DE", "metric", "en", "<APIKEY>") {
Ok(current) => println!(
"Today's weather in {} is {}",
current.name.as_str(),
current.weather[0].main.as_str()
),
Err(e) => println!("Could not fetch weather because: {}", e),
}
}
There is a blocking and a non-blocking variant of weather()
:
openweathermap::blocking::weather
which wont return until there is a new update.openweathermap::weather
and asynchronously await the result until there is any.Beside this introduction there is a reference documentation which can be found here.
This README tastes better at openweathermap.thats-software.com.
For the source code see this repository at github.com.
Published at crates.io.
openweathermap is licensed under the MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)