Skip to content

Commit

Permalink
Fixed issue when Amtrak changed schema
Browse files Browse the repository at this point in the history
Zip is now a u32. Train number is now a String. I don't know why, I am
just following whatever they do on the backend.
  • Loading branch information
StefanBossbaly committed May 10, 2024
1 parent 9b68f7c commit ba716c8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,11 @@ impl Client {
/// [`TrainResponse`]: responses::TrainResponse
/// [`train_id`]: responses::Train::train_id
/// [`train_num`]: responses::Train::train_num
pub async fn train(&self, train_identifier: &str) -> Result<responses::TrainResponse> {
let url = format!("{}/trains/{}", self.base_url, train_identifier);
pub async fn train<S: AsRef<str>>(
&self,
train_identifier: S,
) -> Result<responses::TrainResponse> {
let url = format!("{}/trains/{}", self.base_url, train_identifier.as_ref());

let response = reqwest::Client::new()
.get(url)
Expand Down Expand Up @@ -320,8 +323,11 @@ impl Client {
///
/// [`StationResponse`]: responses::StationResponse
/// [`code`]: responses::TrainStation::code
pub async fn station(&self, station_code: &str) -> Result<responses::StationResponse> {
let url = format!("{}/stations/{}", self.base_url, station_code);
pub async fn station<S: AsRef<str>>(
&self,
station_code: S,
) -> Result<responses::StationResponse> {
let url = format!("{}/stations/{}", self.base_url, station_code.as_ref());

let response = reqwest::Client::new()
.get(url)
Expand Down
4 changes: 2 additions & 2 deletions src/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub struct Train {

/// The (possible unique) number identifying the train.
#[serde(rename = "trainNum")]
pub train_num: u32,
pub train_num: String,

/// The concatenation of the [`train_num`] with another number (not sure
/// what exactly) in the format "{:train_num}-{:instance}".
Expand Down Expand Up @@ -511,7 +511,7 @@ pub struct Station {
/// # Examples:
/// * `19104`
/// * `10001`
pub zip: String,
pub zip: u32,

/// A list of current [`train_id`] that have departed from or are enroute to
/// this station.
Expand Down
4 changes: 2 additions & 2 deletions tests/station.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async fn test_single_station() -> Result<(), amtrak_api::errors::Error> {
"address2": " ",
"city": "Aberdeen",
"state": "MD",
"zip": "21001",
"zip": 21001,
"trains": []
}
}"#,
Expand All @@ -41,7 +41,7 @@ async fn test_single_station() -> Result<(), amtrak_api::errors::Error> {
assert_eq!(station.address2, " ");
assert_eq!(station.city, "Aberdeen");
assert_eq!(station.state, "MD");
assert_eq!(station.zip, "21001");
assert_eq!(station.zip, 21001);
assert_eq!(station.trains.len(), 0);

mock_server.assert_async().await;
Expand Down
4 changes: 2 additions & 2 deletions tests/train.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async fn test_single_train() -> Result<(), amtrak_api::errors::Error> {
"657": [
{
"routeName": "Keystone",
"trainNum": 657,
"trainNum": "657",
"trainID": "657-30",
"lat": 40.14815944794739,
"lon": -76.61796031144218,
Expand Down Expand Up @@ -274,7 +274,7 @@ async fn test_single_train() -> Result<(), amtrak_api::errors::Error> {
let train = trains.get(0).unwrap();

assert_eq!(train.route_name, "Keystone");
assert_eq!(train.train_num, 657);
assert_eq!(train.train_num, "657");
assert_eq!(train.lat, 40.14815944794739);
assert_eq!(train.lon, -76.61796031144218);
assert_eq!(train.train_timely, "NaN Minutes Early");
Expand Down

0 comments on commit ba716c8

Please sign in to comment.