use curl::easy::Easy;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
fn curl(url: &str) -> String {
let mut dst = Vec::new();
let mut easy = Easy::new();
easy.url(url).unwrap();
{
let mut transfer = easy.transfer();
transfer
.write_function(|data| {
dst.extend_from_slice(data);
Ok(data.len())
})
.unwrap();
transfer.perform().unwrap();
}
String::from_utf8_lossy(&dst).to_string()
}
pub fn overview(ags: &str) -> Result<Vec<AGSOverviewResult>, serde_json::Error> {
let url = format!("https://warnung.bund.de/api31/dashboard/{}.json", ags);
let json = curl(&url);
let overview: Vec<AGSOverviewResult> = serde_json::from_str(&json)?;
Ok(overview)
}
pub fn mowas() -> Result<Vec<MapWarning>, serde_json::Error> {
let json = curl("https://warnung.bund.de/api31/mowas/mapData.json");
let map_warnings: Vec<MapWarning> = serde_json::from_str(&json)?;
Ok(map_warnings)
}
pub fn katwarn() -> Result<Vec<MapWarning>, serde_json::Error> {
let json = curl("https://warnung.bund.de/api31/katwarn/mapData.json");
let map_warnings: Vec<MapWarning> = serde_json::from_str(&json)?;
Ok(map_warnings)
}
pub fn biwapp() -> Result<Vec<MapWarning>, serde_json::Error> {
let json = curl("https://warnung.bund.de/api31/biwapp/mapData.json");
let map_warnings: Vec<MapWarning> = serde_json::from_str(&json)?;
Ok(map_warnings)
}
pub fn covidrules(ags: &str) -> Result<AGSCovidRules, serde_json::Error> {
let url = format!(
"https://warnung.bund.de/api31/appdata/covid/covidrules/DE/{}.json",
ags
);
let json = curl(&url);
let covidrules: AGSCovidRules = serde_json::from_str(&json)?;
Ok(covidrules)
}
#[derive(Serialize, Deserialize, Debug)]
pub enum Severity {
Minor,
Severe,
}
#[derive(Serialize, Deserialize, Debug)]
pub enum Type {
Alert,
Update,
Cancel,
}
#[derive(Serialize, Deserialize, Debug)]
pub enum OverviewType {
ALERT,
UPDATE,
CANCEL,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct MapWarning {
pub id: String,
pub version: i32,
#[serde(rename = "startDate")]
pub start_date: String,
pub severity: Severity,
pub r#type: Type,
#[serde(rename = "i18nTitle")]
pub i18n_title: HashMap<String, String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Level {
pub headline: String,
pub range: String,
#[serde(rename = "backgroundColor")]
pub background_color: String,
#[serde(rename = "textColor")]
pub text_color: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub enum Source {
LAND,
BUND,
KREIS,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Rules {
pub id: String,
pub caption: String,
pub text: String,
pub source: Source,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Common {
pub id: String,
pub caption: String,
pub text: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AGSCovidRules {
pub key: String,
pub level: Level,
#[serde(rename = "generalInfo")]
pub general_info: String,
pub rules: Vec<Rules>,
pub common: Vec<Common>,
}
#[derive(Serialize, Deserialize, Debug)]
pub enum Provider {
MOWAS,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Data {
headline: String,
provider: Provider,
severity: Severity,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Payload {
pub version: i32,
pub r#type: OverviewType,
pub id: String,
pub hash: String,
pub data: Data,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AGSOverviewResult {
pub id: String,
pub payload: Payload,
#[serde(rename = "i18nTitle")]
pub i18n_title: HashMap<String, String>,
pub sent: String,
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn test_overview() {
let overview = overview("091620000000").unwrap();
println!("{:#?}", overview);
}
#[test]
fn test_mowas() {
let mowas = mowas().unwrap();
println!("{:#?}", mowas);
}
#[test]
fn test_katwarn() {
let katwarn = katwarn().unwrap();
println!("{:#?}", katwarn);
}
#[test]
fn test_biwapp() {
let biwapp = biwapp().unwrap();
println!("{:#?}", biwapp);
}
#[test]
fn test_covidrules() {
let covidrules = covidrules("091620000000").unwrap();
println!("{:#?}", covidrules);
}
}
#[cfg(doctest)]
mod test_readme {
macro_rules! external_doc_test {
($x:expr) => {
#[doc = $x]
extern "C" {}
};
}
external_doc_test!(include_str!("../README.md"));
}