Update table utility to request full 60 minutes of data main
authorJakob Cornell <jakob+gpg@jcornell.net>
Sat, 21 Sep 2024 05:20:38 +0000 (00:20 -0500)
committerJakob Cornell <jakob+gpg@jcornell.net>
Sat, 21 Sep 2024 05:20:38 +0000 (00:20 -0500)
Cargo.lock
dump_stop/Cargo.toml
dump_stop/src/main.rs
mta_live_client/Cargo.toml
mta_live_client/src/lib.rs

index 24f7797101046d61a309b891bde0f89da0d32d7a..bb8e94ae0b99c5b8cd126791a43677e76f593d9e 100644 (file)
@@ -234,6 +234,7 @@ name = "dump_stop"
 version = "0.1.0"
 dependencies = [
  "ascii_table",
+ "chrono",
  "clap",
  "mta_live_client",
 ]
index 4eb2150ed52bb9fb40cd089d5babd77033198e77..a21d98c6186d6ae8b9ed2bea1f274d51c29c8576 100644 (file)
@@ -4,6 +4,7 @@ version = "0.1.0"
 edition = "2021"
 
 [dependencies]
-mta_live_client = { path = "../mta_live_client" }
-clap = { version = "4", features = ["derive"] }
 ascii_table = { version = "4" }
+chrono = { version = "0" }
+clap = { version = "4", features = ["derive"] }
+mta_live_client = { path = "../mta_live_client" }
index c7fcdf5f6b899995a5e28d3ba35eaddd2ec62d44..316f436d1d14ca2b2dad319115df31aef34b5f43 100644 (file)
@@ -2,6 +2,7 @@ use std::collections::HashSet;
 use std::fmt::Display;
 
 use ascii_table::AsciiTable;
+use chrono::TimeDelta;
 use clap::Parser;
 
 /// Dump upcoming train ETAs at a given stop
@@ -19,7 +20,11 @@ struct Args {
 fn main() {
        let args = Args::parse();
 
-       let stop_responses = mta_live_client::get_nearby(&HashSet::from([&args.stop_id])).unwrap();
+       let stop_responses = mta_live_client::get_nearby(
+               &HashSet::from([&args.stop_id]),
+               TimeDelta::minutes(60), // maximum window size supported by API
+       )
+       .unwrap();
        let stop_response = match stop_responses.len() {
                1 => stop_responses.into_iter().next().unwrap(),
                length => panic!("expected 1 stop response from API, got {length}"),
index cc43fb429d8f928607798d5e8142a7703a28390e..3e5576e5747af920e290676f4f67cf758619cf57 100644 (file)
@@ -4,8 +4,8 @@ version = "0.1.0"
 edition = "2021"
 
 [dependencies]
-reqwest = { version = "0.12", features = ["blocking", "json"] }
-serde_json = { version = "1" }
-serde = { version = "1", features = ["derive"] }
 chrono = { version = "0" }
 clap = { version = "4", features = ["derive"] }
+reqwest = { version = "0.12", features = ["blocking", "json"] }
+serde = { version = "1", features = ["derive"] }
+serde_json = { version = "1" }
index 651e3e85d18662e005edd800b9a48577ce7efa85..e012a55509e3780d4008cbce269a4c5c75a9013d 100644 (file)
@@ -1,7 +1,7 @@
 use std::collections::HashSet;
 
 use chrono::offset::FixedOffset;
-use chrono::DateTime;
+use chrono::{DateTime, TimeDelta};
 use reqwest::Url;
 use serde::Deserialize;
 
@@ -58,17 +58,25 @@ pub struct StopResponse {
 
 pub fn get_nearby(
        stop_ids: &HashSet<impl AsRef<str>>,
+       window_size: TimeDelta,
 ) -> Result<Vec<StopResponse>, reqwest::Error> {
+       assert!(
+               window_size >= TimeDelta::zero(),
+               "expected non-negative window size"
+       );
        let url = Url::parse_with_params(
                "https://otp-mta-prod.camsys-apps.com/otp/routers/default/nearby",
-               &[(
-                       "stops",
-                       stop_ids
-                               .iter()
-                               .map(AsRef::as_ref)
-                               .collect::<Vec<_>>()
-                               .join(","),
-               )],
+               &[
+                       (
+                               "stops",
+                               stop_ids
+                                       .iter()
+                                       .map(AsRef::as_ref)
+                                       .collect::<Vec<_>>()
+                                       .join(","),
+                       ),
+                       ("timeRange", window_size.num_seconds().to_string()),
+               ],
        )
        .unwrap();
        reqwest::blocking::get(url)?.json()