fej/src/ivago/controller/search.rs

30 lines
1007 B
Rust

use super::structs::Street;
use crate::errors::FejError;
use reqwest::blocking as reqwest;
use std::collections::HashMap;
use std::convert::TryFrom;
/// Endpoint for the search feature
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
pub fn search_streets(street_name: &str) -> Result<Vec<Street>, FejError> {
let client = reqwest::Client::new();
let response = client.get(SEARCH_URL).query(&[("q", street_name)]).send()?;
let data: Vec<HashMap<String, String>> = response.json()?;
// This is pretty cool, filter_map first does get() on all the maps, and
// then filters out any None values
// Then, we do the same thing for streets
Ok(data
.iter()
.filter_map(|m| m.get("value"))
.filter_map(|v| Street::try_from(v.as_str()).ok())
.collect())
}