fej/src/ivago/controller/mod.rs

55 lines
1.5 KiB
Rust

use reqwest::blocking as reqwest;
use std::collections::HashMap;
use std::convert::TryFrom;
use rocket::http::Status;
use std::error::Error;
pub mod structs;
use structs::{Street, Date, PickupTime};
/// The base URL where their API starts
const BASE_URL: &str = "https://www.ivago.be/nl/particulier/afval/ophaling";
const SEARCH_URL: &str ="https://www.ivago.be/nl/particulier/autocomplete/garbage/streets";
/// Searches the Ivago API for streets in the given city
///
/// # Arguments
///
/// * `street` - name of the street
/// * `city` - city the street is in
// TODO find out how to do this async
pub fn search_streets(street_name: &String) -> Result<Vec<Street>, Box<dyn Error>> {
let client = reqwest::Client::new();
let response = client.get(SEARCH_URL)
.query(&[("q", street_name)])
.send()?;
let data: Vec<HashMap<String, String>> = response.json()?;
let mut output: Vec<Street> = Vec::new();
// We iterate over every item and extract the needed data
for map in data.iter() {
if let Some(value) = map.get("value") {
match Street::try_from(value) {
Ok(street) => output.push(street),
Err(_) => continue,
}
}
}
Ok(output)
}
/// Return the known pickup times for the given street and/or city
///
/// # Arguments
///
/// * `street` - name of the street
/// * `city` - city the street is in
pub fn get_pickup_times(street: Street) -> Vec<PickupTime> {
Vec::new()
}