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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
//! This crate is a small wrapper around the [buerostatus](https://github.com/fsr/buerostatus) API //! of the [@fsr](https://github.com/fsr) that tells you whether someone is in our office or not. //! //! # Example //! ``` //! # use buerostatus::*; //! if let Ok(is_open) = get_buerostatus() { //! if is_open { println!("Someone's inside!"); } //! else { println!("No one is there..."); } //! } //! else { //! println!("An error occured!"); //! } //! ``` //! //! The function `get_buerostatus()` offers a precise error message in case of an error. See the enum //! `ApiError` for more information. extern crate reqwest; use reqwest::StatusCode; use std::io::Read; /// A set of errors that may occur during the API call. #[derive(Debug)] pub enum ApiError { /// An error originating from the `reqwest` crate such as "No Internet connection". Network(reqwest::Error), /// The Return Code of the website is not _200_. StatusNotOk(reqwest::StatusCode), /// The API call delivered a wrong message. WrongMessage } /// Gets the buerostatus from ifsr.de. /// /// Returns `true`, when the office is open, `false` if not and an `ApiError` if any form of error is occured during execution. /// /// # Example /// ``` /// # use buerostatus::*; /// if let Ok(is_open) = get_buerostatus() { /// if is_open { /// println!("Someone's inside!"); /// } /// } /// else { /// // Error Handling... /// } /// ``` pub fn get_buerostatus() -> Result<bool, ApiError> { let url = "https://www.ifsr.de/buerostatus/output.php"; // Make the request to ifsr.de let mut res = match reqwest::get(url) { Ok(resp) => resp, Err(err) => return Err(ApiError::Network(err)) }; // Check if response from Server is Status 200. if *res.status() != StatusCode::Ok { return Err(ApiError::StatusNotOk(*res.status())); } let mut message = String::new(); res.read_to_string(&mut message).unwrap(); // Check the content of the response match message.as_ref() { "0" => Ok(false), "1" => Ok(true), _ => Err(ApiError::WrongMessage), // just in case, the server _should_ always return 1 or 0. } }