fej/src/ivago/controller/search.rs

30 lines
1007 B
Rust
Raw Normal View History

use super::structs::Street;
2021-04-04 13:03:08 +02:00
use crate::errors::FejError;
2021-03-15 14:48:20 +01:00
use reqwest::blocking as reqwest;
use std::collections::HashMap;
use std::convert::TryFrom;
/// Endpoint for the search feature
2021-04-02 21:20:36 +02:00
const SEARCH_URL: &str = "https://www.ivago.be/nl/particulier/autocomplete/garbage/streets";
2021-03-15 14:48:20 +01:00
/// Searches the Ivago API for streets in the given city
///
/// # Arguments
///
/// * `street` - name of the street
/// * `city` - city the street is in
2021-04-05 11:10:48 +02:00
pub fn search_streets(street_name: &str) -> Result<Vec<Street>, FejError> {
2021-03-15 14:48:20 +01:00
let client = reqwest::Client::new();
2021-04-02 21:20:36 +02:00
let response = client.get(SEARCH_URL).query(&[("q", street_name)]).send()?;
2021-03-15 14:48:20 +01:00
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())
2021-03-15 14:48:20 +01:00
}