[closes #8] modularized ivago endpoint module
parent
89bd29dc78
commit
ea72231612
|
@ -1,4 +1,6 @@
|
|||
mod pickup_times;
|
||||
mod search;
|
||||
pub use pickup_times::{get_pickup_times, BasicDate, PickupTime};
|
||||
pub use search::{search_streets, Street};
|
||||
pub mod structs;
|
||||
|
||||
pub use pickup_times::get_pickup_times;
|
||||
pub use search::search_streets;
|
||||
|
|
|
@ -1,106 +1,12 @@
|
|||
use super::search::Street;
|
||||
use super::structs::{BasicDate, PickupTime, Street};
|
||||
use crate::errors::FejError;
|
||||
use chrono::{FixedOffset, TimeZone};
|
||||
use regex::Regex;
|
||||
use reqwest::blocking as reqwest;
|
||||
use rocket::http::RawStr;
|
||||
use rocket::request::FromFormValue;
|
||||
use serde::ser::{SerializeStruct, Serializer};
|
||||
use serde::Serialize;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::{From, TryFrom};
|
||||
|
||||
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";
|
||||
|
||||
/// Represents a very simple Timezoneless date. Considering the timezone will
|
||||
/// always be CEST (aka Belgium's timezone), this is good enough. I use this
|
||||
/// instead of a NaiveDate to avoid E0117.
|
||||
pub struct BasicDate {
|
||||
year: u32,
|
||||
month: u8,
|
||||
day: u8,
|
||||
}
|
||||
|
||||
/// This allows us to use BasicDate as a query parameter in our routes.
|
||||
impl<'v> FromFormValue<'v> for BasicDate {
|
||||
type Error = &'v RawStr;
|
||||
|
||||
fn from_form_value(form_value: &'v RawStr) -> Result<BasicDate, Self::Error> {
|
||||
match BasicDate::try_from(form_value.as_str()) {
|
||||
Err(_) => Err(form_value),
|
||||
Ok(date) => Ok(date),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// We need this when deserializing BasicDate.
|
||||
impl ToString for BasicDate {
|
||||
fn to_string(&self) -> String {
|
||||
format!("{}-{}-{}", self.year, self.month, self.day)
|
||||
}
|
||||
}
|
||||
|
||||
/// This is used to serialize BasicDate.
|
||||
impl TryFrom<&str> for BasicDate {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(s: &str) -> Result<BasicDate, Self::Error> {
|
||||
let re = Regex::new(r"^(\d{4})-(\d{2})-(\d{2})$").unwrap();
|
||||
|
||||
match re.captures(s) {
|
||||
None => Err(()),
|
||||
Some(caps) => Ok(BasicDate {
|
||||
// TODO change this to ? operator if possible
|
||||
year: caps.get(1).unwrap().as_str().parse().unwrap(),
|
||||
month: caps.get(2).unwrap().as_str().parse().unwrap(),
|
||||
day: caps.get(3).unwrap().as_str().parse().unwrap(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for BasicDate {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(&self.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BasicDate> for i64 {
|
||||
fn from(date: BasicDate) -> i64 {
|
||||
// Timezone of Brussels is UTC + 2 hours in the western hemisphere
|
||||
FixedOffset::west(7_200)
|
||||
.ymd(date.year as i32, date.month as u32, date.day as u32)
|
||||
// For some reason, I couldn't get .timestamp() to work on a Date
|
||||
// without a time component, even though the docs seemed to
|
||||
// indicate this was possible
|
||||
.and_hms(0, 0, 0)
|
||||
.timestamp()
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for PickupTime {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut s = serializer.serialize_struct("PickupTime", 2)?;
|
||||
s.serialize_field("date", &self.date)?;
|
||||
s.serialize_field("label", &self.label)?;
|
||||
s.end()
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a pickup time instance. All fields are a direct map of the
|
||||
/// original API
|
||||
pub struct PickupTime {
|
||||
date: BasicDate,
|
||||
label: String,
|
||||
}
|
||||
|
||||
pub fn get_pickup_times(
|
||||
street: Street,
|
||||
number: u32,
|
||||
|
|
|
@ -1,71 +1,12 @@
|
|||
use super::structs::Street;
|
||||
use crate::errors::FejError;
|
||||
use regex::Regex;
|
||||
use reqwest::blocking as reqwest;
|
||||
use rocket::http::RawStr;
|
||||
use rocket::request::FromFormValue;
|
||||
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||
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";
|
||||
|
||||
impl From<Street> for String {
|
||||
fn from(street: Street) -> String {
|
||||
format!("{} ({})", street.name, street.city)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for Street {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut s = serializer.serialize_struct("Street", 2)?;
|
||||
s.serialize_field("name", &self.name)?;
|
||||
s.serialize_field("city", &self.city)?;
|
||||
s.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<String> for Street {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
if let Some(index) = value.find('(') {
|
||||
Ok(Street {
|
||||
name: (value[0..index - 1].trim()).to_string(),
|
||||
city: (value[index + 1..value.len() - 1].trim()).to_string(),
|
||||
})
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'v> FromFormValue<'v> for Street {
|
||||
type Error = &'v RawStr;
|
||||
|
||||
fn from_form_value(form_value: &'v RawStr) -> Result<Street, Self::Error> {
|
||||
// This regex is pretty loose tbh, but not sure how I can make it more
|
||||
// strict right now
|
||||
let re = Regex::new(r"^(.+) \((.+)\)$").unwrap();
|
||||
match re.captures(&form_value.url_decode_lossy()) {
|
||||
None => Err(form_value),
|
||||
Some(caps) => Ok(Street {
|
||||
name: String::from(caps.get(1).unwrap().as_str()),
|
||||
city: String::from(caps.get(2).unwrap().as_str()),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a street
|
||||
pub struct Street {
|
||||
pub name: String,
|
||||
pub city: String,
|
||||
}
|
||||
|
||||
/// Searches the Ivago API for streets in the given city
|
||||
///
|
||||
/// # Arguments
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
use chrono::{FixedOffset, TimeZone};
|
||||
use regex::Regex;
|
||||
use rocket::http::RawStr;
|
||||
use rocket::request::FromFormValue;
|
||||
use serde::ser::Serializer;
|
||||
use serde::Serialize;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
/// Represents a very simple Timezoneless date. Considering the timezone will
|
||||
/// always be CEST (aka Belgium's timezone), this is good enough. I use this
|
||||
/// instead of a NaiveDate to avoid E0117.
|
||||
pub struct BasicDate {
|
||||
year: u32,
|
||||
month: u8,
|
||||
day: u8,
|
||||
}
|
||||
|
||||
/// This allows us to use BasicDate as a query parameter in our routes.
|
||||
impl<'v> FromFormValue<'v> for BasicDate {
|
||||
type Error = &'v RawStr;
|
||||
|
||||
fn from_form_value(form_value: &'v RawStr) -> Result<BasicDate, Self::Error> {
|
||||
match BasicDate::try_from(form_value.as_str()) {
|
||||
Err(_) => Err(form_value),
|
||||
Ok(date) => Ok(date),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// We need this when deserializing BasicDate.
|
||||
impl ToString for BasicDate {
|
||||
fn to_string(&self) -> String {
|
||||
format!("{}-{}-{}", self.year, self.month, self.day)
|
||||
}
|
||||
}
|
||||
|
||||
/// This is used to serialize BasicDate.
|
||||
impl TryFrom<&str> for BasicDate {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(s: &str) -> Result<BasicDate, Self::Error> {
|
||||
let re = Regex::new(r"^(\d{4})-(\d{2})-(\d{2})$").unwrap();
|
||||
|
||||
match re.captures(s) {
|
||||
None => Err(()),
|
||||
Some(caps) => Ok(BasicDate {
|
||||
// TODO change this to ? operator if possible
|
||||
year: caps.get(1).unwrap().as_str().parse().unwrap(),
|
||||
month: caps.get(2).unwrap().as_str().parse().unwrap(),
|
||||
day: caps.get(3).unwrap().as_str().parse().unwrap(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for BasicDate {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(&self.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BasicDate> for i64 {
|
||||
fn from(date: BasicDate) -> i64 {
|
||||
// Timezone of Brussels is UTC + 2 hours in the western hemisphere
|
||||
FixedOffset::west(7_200)
|
||||
.ymd(date.year as i32, date.month as u32, date.day as u32)
|
||||
// For some reason, I couldn't get .timestamp() to work on a Date
|
||||
// without a time component, even though the docs seemed to
|
||||
// indicate this was possible
|
||||
.and_hms(0, 0, 0)
|
||||
.timestamp()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
mod basic_date;
|
||||
mod pickup_time;
|
||||
mod street;
|
||||
|
||||
pub use basic_date::BasicDate;
|
||||
pub use pickup_time::PickupTime;
|
||||
pub use street::Street;
|
|
@ -0,0 +1,21 @@
|
|||
use super::BasicDate;
|
||||
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||
|
||||
impl Serialize for PickupTime {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut s = serializer.serialize_struct("PickupTime", 2)?;
|
||||
s.serialize_field("date", &self.date)?;
|
||||
s.serialize_field("label", &self.label)?;
|
||||
s.end()
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a pickup time instance. All fields are a direct map of the
|
||||
/// original API
|
||||
pub struct PickupTime {
|
||||
pub date: BasicDate,
|
||||
pub label: String,
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
use regex::Regex;
|
||||
use rocket::http::RawStr;
|
||||
use rocket::request::FromFormValue;
|
||||
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||
use std::convert::TryFrom;
|
||||
|
||||
/// Represents a street
|
||||
pub struct Street {
|
||||
pub name: String,
|
||||
pub city: String,
|
||||
}
|
||||
|
||||
impl From<Street> for String {
|
||||
fn from(street: Street) -> String {
|
||||
format!("{} ({})", street.name, street.city)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for Street {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut s = serializer.serialize_struct("Street", 2)?;
|
||||
s.serialize_field("name", &self.name)?;
|
||||
s.serialize_field("city", &self.city)?;
|
||||
s.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<String> for Street {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
if let Some(index) = value.find('(') {
|
||||
Ok(Street {
|
||||
name: (value[0..index - 1].trim()).to_string(),
|
||||
city: (value[index + 1..value.len() - 1].trim()).to_string(),
|
||||
})
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'v> FromFormValue<'v> for Street {
|
||||
type Error = &'v RawStr;
|
||||
|
||||
fn from_form_value(form_value: &'v RawStr) -> Result<Street, Self::Error> {
|
||||
// This regex is pretty loose tbh, but not sure how I can make it more
|
||||
// strict right now
|
||||
let re = Regex::new(r"^(.+) \((.+)\)$").unwrap();
|
||||
match re.captures(&form_value.url_decode_lossy()) {
|
||||
None => Err(form_value),
|
||||
Some(caps) => Ok(Street {
|
||||
name: String::from(caps.get(1).unwrap().as_str()),
|
||||
city: String::from(caps.get(2).unwrap().as_str()),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,6 +2,8 @@ mod controller;
|
|||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use controller::structs::{BasicDate, PickupTime, Street};
|
||||
use controller::{get_pickup_times, search_streets};
|
||||
use rocket::http::Status;
|
||||
use rocket_contrib::json::Json;
|
||||
|
||||
|
@ -10,18 +12,18 @@ pub fn routes() -> Vec<rocket::Route> {
|
|||
}
|
||||
|
||||
#[get("/search?<street>")]
|
||||
pub fn route_search_streets(street: String) -> Result<Json<Vec<controller::Street>>, Status> {
|
||||
Ok(Json(controller::search_streets(&street)?))
|
||||
pub fn route_search_streets(street: String) -> Result<Json<Vec<Street>>, Status> {
|
||||
Ok(Json(search_streets(&street)?))
|
||||
}
|
||||
|
||||
#[get("/?<street>&<number>&<start_date>&<end_date>")]
|
||||
pub fn route_get_pickup_times(
|
||||
street: controller::Street,
|
||||
street: Street,
|
||||
number: u32,
|
||||
start_date: controller::BasicDate,
|
||||
end_date: controller::BasicDate,
|
||||
) -> Result<Json<Vec<controller::PickupTime>>, Status> {
|
||||
Ok(Json(controller::get_pickup_times(
|
||||
start_date: BasicDate,
|
||||
end_date: BasicDate,
|
||||
) -> Result<Json<Vec<PickupTime>>, Status> {
|
||||
Ok(Json(get_pickup_times(
|
||||
street, number, start_date, end_date,
|
||||
)?))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue