Finally fixed broken ivago search
This commit is contained in:
parent
4032800d64
commit
43eb20457f
7 changed files with 498 additions and 48 deletions
|
|
@ -1,4 +0,0 @@
|
|||
pub enum Error {
|
||||
BackendError(String),
|
||||
BadResponseError(String),
|
||||
}
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
use reqwest::blocking as reqwest;
|
||||
use crate::errors::Error;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryFrom;
|
||||
use rocket::http::Status;
|
||||
use std::error::Error;
|
||||
|
||||
mod structs;
|
||||
pub mod structs;
|
||||
use structs::{Street, Date, PickupTime};
|
||||
|
||||
|
||||
|
|
@ -19,25 +20,24 @@ const SEARCH_URL: &str ="https://www.ivago.be/nl/particulier/autocomplete/garbag
|
|||
/// * `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();
|
||||
pub fn search_streets(street_name: &String) -> Result<Vec<Street>, Box<Error>> {
|
||||
let client = reqwest::ClientBuilder::new().build()?;
|
||||
let response = client.get(SEARCH_URL)
|
||||
.query(&[("q", street_name)])
|
||||
.send()?;
|
||||
let data: Vec<HashMap<String, String>> = response.json()?;
|
||||
|
||||
// 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."))),
|
||||
};
|
||||
// let client = reqwest::Client::new();
|
||||
// let request = client.get(SEARCH_URL)
|
||||
// .query(&[("q", street_name)]).build();
|
||||
// println!("{}", request.url());
|
||||
// let response = client.execute(request);
|
||||
|
||||
// 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 data: Vec<HashMap<String, String>> = match response.json() {
|
||||
// Ok(res) => res,
|
||||
// Err(_) => return Err(Status::InternalServerError),
|
||||
// };
|
||||
|
||||
let mut output: Vec<Street> = Vec::new();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use std::convert::TryFrom;
|
||||
use serde::ser::{Serialize, Serializer, SerializeStruct};
|
||||
|
||||
|
||||
/// Represents a street
|
||||
|
|
@ -7,6 +8,18 @@ pub struct Street {
|
|||
city: String,
|
||||
}
|
||||
|
||||
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 = ();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
#[cfg(test)] mod tests;
|
||||
|
||||
use rocket_contrib::json::Json;
|
||||
|
||||
mod controller;
|
||||
use controller as ctrl;
|
||||
use ctrl::structs::Street;
|
||||
|
||||
pub fn routes() -> Vec<rocket::Route> {
|
||||
routes![
|
||||
|
|
@ -10,9 +13,15 @@ pub fn routes() -> Vec<rocket::Route> {
|
|||
}
|
||||
|
||||
// URL: https://www.ivago.be/nl/particulier/autocomplete/garbage/streets?q=Lange
|
||||
// TODO make this async
|
||||
// TODO change this so it can return errors instead of empty json
|
||||
#[get("/search?<street>", format="json")]
|
||||
pub fn search_streets(street: String) -> String {
|
||||
|
||||
// let response = send(request.body(()).unwrap());
|
||||
"".to_string()
|
||||
pub fn search_streets(street: String) -> Json<Vec<Street>> {
|
||||
match ctrl::search_streets(&street) {
|
||||
Ok(streets) => Json(streets),
|
||||
Err(err) => {
|
||||
println!("{:?}", err);
|
||||
Json(Vec::new())
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
// Route modules
|
||||
mod hello;
|
||||
mod ivago;
|
||||
mod errors;
|
||||
|
||||
fn rocket() -> rocket::Rocket {
|
||||
rocket::ignite()
|
||||
|
|
|
|||
Reference in a new issue