Merge branch '9-better-errors' into develop
commit
bf36263cef
|
@ -0,0 +1,23 @@
|
||||||
|
use reqwest::Error;
|
||||||
|
use rocket::http::Status;
|
||||||
|
|
||||||
|
pub enum FejError {
|
||||||
|
InvalidArgument,
|
||||||
|
FailedRequest,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<FejError> for Status {
|
||||||
|
fn from(err: FejError) -> Status {
|
||||||
|
match err {
|
||||||
|
FejError::InvalidArgument => Status::BadRequest,
|
||||||
|
FejError::FailedRequest => Status::InternalServerError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO make this more advanced where possible
|
||||||
|
impl From<Error> for FejError {
|
||||||
|
fn from(_: Error) -> FejError {
|
||||||
|
FejError::FailedRequest
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +1,14 @@
|
||||||
use super::search::Street;
|
use super::search::Street;
|
||||||
|
use crate::errors::FejError;
|
||||||
use chrono::{FixedOffset, TimeZone};
|
use chrono::{FixedOffset, TimeZone};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use reqwest::blocking as reqwest;
|
use reqwest::blocking as reqwest;
|
||||||
use rocket::http::RawStr;
|
use rocket::http::RawStr;
|
||||||
use rocket::request::FromFormValue;
|
use rocket::request::FromFormValue;
|
||||||
use serde::ser::{SerializeStruct, Serializer};
|
use serde::ser::{SerializeStruct, Serializer};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::Serialize;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::convert::{From, TryFrom};
|
use std::convert::{From, TryFrom};
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
const BASE_URL: &str = "https://www.ivago.be/nl/particulier/afval/ophaling";
|
const BASE_URL: &str = "https://www.ivago.be/nl/particulier/afval/ophaling";
|
||||||
const CAL_URL: &str = "https://www.ivago.be/nl/particulier/garbage/pick-up/pickups";
|
const CAL_URL: &str = "https://www.ivago.be/nl/particulier/garbage/pick-up/pickups";
|
||||||
|
@ -108,7 +108,7 @@ pub fn get_pickup_times(
|
||||||
number: u32,
|
number: u32,
|
||||||
start_date: BasicDate,
|
start_date: BasicDate,
|
||||||
end_date: BasicDate,
|
end_date: BasicDate,
|
||||||
) -> Result<Vec<PickupTime>, Box<dyn Error>> {
|
) -> Result<Vec<PickupTime>, FejError> {
|
||||||
let client = reqwest::Client::builder().cookie_store(true).build()?;
|
let client = reqwest::Client::builder().cookie_store(true).build()?;
|
||||||
|
|
||||||
// This populates the cookies with the necessary values
|
// This populates the cookies with the necessary values
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::errors::FejError;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use reqwest::blocking as reqwest;
|
use reqwest::blocking as reqwest;
|
||||||
use rocket::http::RawStr;
|
use rocket::http::RawStr;
|
||||||
|
@ -5,7 +6,6 @@ use rocket::request::FromFormValue;
|
||||||
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
/// Endpoint for the search feature
|
/// Endpoint for the search feature
|
||||||
const SEARCH_URL: &str = "https://www.ivago.be/nl/particulier/autocomplete/garbage/streets";
|
const SEARCH_URL: &str = "https://www.ivago.be/nl/particulier/autocomplete/garbage/streets";
|
||||||
|
@ -73,7 +73,7 @@ pub struct Street {
|
||||||
/// * `street` - name of the street
|
/// * `street` - name of the street
|
||||||
/// * `city` - city the street is in
|
/// * `city` - city the street is in
|
||||||
// TODO find out how to do this async
|
// TODO find out how to do this async
|
||||||
pub fn search_streets(street_name: &String) -> Result<Vec<Street>, Box<dyn Error>> {
|
pub fn search_streets(street_name: &String) -> Result<Vec<Street>, FejError> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let response = client.get(SEARCH_URL).query(&[("q", street_name)]).send()?;
|
let response = client.get(SEARCH_URL).query(&[("q", street_name)]).send()?;
|
||||||
let data: Vec<HashMap<String, String>> = response.json()?;
|
let data: Vec<HashMap<String, String>> = response.json()?;
|
||||||
|
|
|
@ -9,13 +9,9 @@ pub fn routes() -> Vec<rocket::Route> {
|
||||||
routes![route_search_streets, route_get_pickup_times]
|
routes![route_search_streets, route_get_pickup_times]
|
||||||
}
|
}
|
||||||
|
|
||||||
// URL: https://www.ivago.be/nl/particulier/autocomplete/garbage/streets?q=Lange
|
|
||||||
#[get("/search?<street>")]
|
#[get("/search?<street>")]
|
||||||
pub fn route_search_streets(street: String) -> Result<Json<Vec<controller::Street>>, Status> {
|
pub fn route_search_streets(street: String) -> Result<Json<Vec<controller::Street>>, Status> {
|
||||||
match controller::search_streets(&street) {
|
Ok(Json(controller::search_streets(&street)?))
|
||||||
Ok(streets) => Ok(Json(streets)),
|
|
||||||
Err(_) => Err(Status::InternalServerError),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/?<street>&<number>&<start_date>&<end_date>")]
|
#[get("/?<street>&<number>&<start_date>&<end_date>")]
|
||||||
|
@ -25,9 +21,7 @@ pub fn route_get_pickup_times(
|
||||||
start_date: controller::BasicDate,
|
start_date: controller::BasicDate,
|
||||||
end_date: controller::BasicDate,
|
end_date: controller::BasicDate,
|
||||||
) -> Result<Json<Vec<controller::PickupTime>>, Status> {
|
) -> Result<Json<Vec<controller::PickupTime>>, Status> {
|
||||||
match controller::get_pickup_times(street, number, start_date, end_date) {
|
Ok(Json(controller::get_pickup_times(
|
||||||
// TODO provide more meaningful status codes here
|
street, number, start_date, end_date,
|
||||||
Err(_) => Err(Status::InternalServerError),
|
)?))
|
||||||
Ok(times) => Ok(Json(times)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
// Route modules
|
// Route modules
|
||||||
|
mod errors;
|
||||||
mod hello;
|
mod hello;
|
||||||
mod ivago;
|
mod ivago;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue