Messy first attempt at ivago search

This commit is contained in:
Jef Roosens 2021-03-11 23:05:30 +01:00
parent 5c62300e6e
commit ab31a5e471
Signed by: Jef Roosens
GPG key ID: B580B976584B5F30
7 changed files with 670 additions and 50 deletions

View file

@ -1,32 +0,0 @@
use chrono::NaiveDate;
/// 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: String, city: Option<String>) -> Vec<String> {
Vec::new()
}
/// Represents a pickup time instance. All fields are a direct map of the
/// original API
pub struct PickupTime {
date: NaiveDate,
label: String,
classes: Vec<String>,
url: String
}
/// 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: String, city: String) -> Vec<PickupTime> {
Vec::new()
}

View file

@ -0,0 +1,65 @@
use reqwest::blocking as reqwest;
use crate::errors::Error;
use std::collections::HashMap;
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>, Error> {
let client = reqwest::Client::new();
let response = client.get(SEARCH_URL).query(
&[("street", &street_name)]).send();
// Get the request, and handle failed requests
let response = match response {
Ok(res) => res,
Err(error) => return Err(Error::BackendError(
String::from("Ivago API failed to respond."))),
};
// Get the JSON
let data: Vec<HashMap<String, String>> = match response.json() {
Ok(res) => res,
Err(error) => return Err(Error::BadResponseError(
String::from("Ivago API responsed with bad JSON.")
)),
};
let output: Vec<Street> = Vec::new();
// We iterate over every item and extract the needed data
for map in data.iter() {
let value = map.get("value");
let index = value.find("(");
match index {
None
}
let street = value[ ..index ].trim();
}
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()
}

View file

@ -0,0 +1,30 @@
use std::convert::From;
/// Represents a street
pub struct Street {
name: String,
city: String,
}
impl From<String> for Street {
}
/// Represents a timezoneless date
pub struct Date {
day: u8,
month: u8,
year: u32,
}
/// Represents a pickup time instance. All fields are a direct map of the
/// original API
pub struct PickupTime {
date: Date,
label: String,
classes: Vec<String>,
url: String
}