Skip to content

Commit

Permalink
/Line/Route endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Shell1010 committed Jan 21, 2024
1 parent 26b1275 commit 41bafa5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 46 deletions.
62 changes: 39 additions & 23 deletions lines/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

use reqwest;
use serde_json::{Value, from_str, from_value};
use crate::datastructs::{DataStruct, QuerySearch, Version, LineData};
use crate::datastructs::{DataStruct, QuerySearch, Version, LineRoute};
use crate::lines::Line;

#[derive(Debug)]
Expand Down Expand Up @@ -71,38 +71,54 @@ impl Client {
Ok(text) => {
// return Ok(from_str(&text).unwrap())
if let Ok(real_data) = from_str::<Value>(&text) {
let chec: String = real_data["$type"].to_string();
println!("{chec}");

match chec.as_str() {
r#""Tfl.Api.Presentation.Entities.RouteSearchResponse, Tfl.Api.Presentation.Entities""# => {
let data: Result<QuerySearch, serde_json::Error> = from_value(real_data);
if let Ok(data) = data {
return Ok(DataStruct::from(data))

match real_data {

Value::Array(_) => {
if real_data[0]["$type"] == "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities" {
for data in real_data.as_array().unwrap() {
let data: Result<LineRoute, serde_json::Error> = from_value(data.to_owned());
if let Ok(data) = data {
return Ok(DataStruct::from(data))
}
}
}
},
r#""Tfl.Api.Common.ApiVersionInfo, Tfl.Api.Common""# => {

let data: Result<Version, serde_json::Error> = from_value(real_data);
if let Ok(data) = data {
return Ok(DataStruct::from(data))
}
}
_ => (),
}

r#""Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities""# => {
let data: Result<LineData, serde_json::Error> = from_value(real_data);
if let Ok(data) = data {
return Ok(DataStruct::from(data))
match &real_data["$type"] {

Value::String(v) => {
match v.as_str() {
"Tfl.Api.Presentation.Entities.RouteSearchResponse, Tfl.Api.Presentation.Entities" => {
let data: Result<QuerySearch, serde_json::Error> = from_value(real_data);
if let Ok(data) = data {
return Ok(DataStruct::from(data))
}
},
"Tfl.Api.Common.ApiVersionInfo, Tfl.Api.Common" => {
let data: Result<Version, serde_json::Error> = from_value(real_data);
if let Ok(data) = data {
return Ok(DataStruct::from(data))
}
},
"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities" => {
let data: Result<LineRoute, serde_json::Error> = from_value(real_data.clone());
if let Ok(data) = data {
return Ok(DataStruct::from(data))
}
}
_ => return Err(TflError::ApiError(format!("Couldn't deserialize: {real_data:#?}")))
}

}

_ => return Err(TflError::ApiError(format!("Couldn't deserialize: {real_data}")))
_ => return Err(TflError::ApiError(format!("Couldn't deserialize: {real_data:#?}")))
}
}

},
Err(e) => return Err(TflError::HttpError(e))

}
},
Err(e) => return Err(TflError::HttpError(e))
Expand Down
23 changes: 3 additions & 20 deletions lines/src/datastructs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct SearchMatch {
pub mode: String,
pub line_name: String,
pub line_route_section: Vec<LineRouteSection>,
pub matched_route_sections: Vec<Value>,
pub matched_route_sections: Vec<RouteSection>,
pub matched_stops: Vec<Value>,
}

Expand Down Expand Up @@ -205,23 +205,7 @@ pub struct Children {
pub stop_letter: Option<String>,
}

// Line/<name>
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LineData {
#[serde(rename = "$type")]
pub type_field: String,
pub id: String,
pub name: String,
pub mode_name: String,
pub disruptions: Vec<Value>,
pub created: String,
pub modified: String,
pub line_statuses: Vec<Value>,
pub route_sections: Vec<Value>,
pub service_types: Vec<ServiceType>,
pub crowding: Crowding,
}




Expand All @@ -243,7 +227,7 @@ impl JsonTrait for LineModeGroup {}
impl JsonTrait for AdditionalProperty {}
impl JsonTrait for Children {}

impl JsonTrait for LineData {}


#[derive(Debug, Deserialize, Serialize)]
#[enum_dispatch(JsonTrait)]
Expand All @@ -262,7 +246,6 @@ pub enum DataStruct {
LineModeGroup(LineModeGroup),
AdditionalProperty(AdditionalProperty),
Children(Children),
LineData(LineData)

}

Expand Down
16 changes: 14 additions & 2 deletions lines/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,20 @@ mod tests {
#[test]
fn line_test() {
let line = Line::Jubilee;
let resp = Client::new("abcd1234").line(&line.route()).fetch().unwrap();
if let DataStruct::LineData(data) = resp {
let resp = Client::new("abcd1234").line(&line.line()).fetch().unwrap();
if let DataStruct::LineRoute(data) = resp {

assert!("Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities" == data.type_field, "Type Returned: {:?}", data)
} else {
assert!(false, "{:?}", resp)
}
}

#[test]
fn route_test() {
let line = Line::Bakerloo;
let resp = Client::new("abcd1234").route(&line.route()).fetch().unwrap();
if let DataStruct::LineRoute(data) = resp {

assert!("Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities" == data.type_field, "Type Returned: {}", data.type_field)
} else {
Expand Down
4 changes: 3 additions & 1 deletion lines/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use datastructs::{DataStruct, QuerySearch, SearchMatch};

fn main() {
let client = Client::new("abcd");
let res: DataStruct = client.version().fetch().unwrap();
let res: DataStruct = client.route(Line::Central.line()).fetch().unwrap();

match res {
DataStruct::QuerySearch(data) => {
Expand All @@ -21,6 +21,8 @@ fn main() {
println!("{}", data.type_field)
},

DataStruct::LineRoute(data) => println!("{}", data.type_field),

_ => ()
}

Expand Down

0 comments on commit 41bafa5

Please sign in to comment.