Skip to content

Commit

Permalink
feat: (#41) new traffic flow api
Browse files Browse the repository at this point in the history
  • Loading branch information
zeon256 committed Mar 5, 2024
1 parent a2d41bf commit ee8cd1b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/async/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ pub trait ClientExt: Client {

async fn build_req_with_query<T, T2, F>(&self, url: &str, query: F) -> LTAResult<T2>
where
F: Send + FnOnce(Self::RB) -> Self::RB,
F: FnOnce(Self::RB) -> Self::RB,
for<'de> T: serde::Deserialize<'de> + Into<T2>;
}
35 changes: 27 additions & 8 deletions src/async/traffic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ pub trait TrafficRequests<C: Client + ClientExt> {
where
S: Into<Option<u32>>,
{
let url = concat_string!(client.base_url(), "/Traffic-Imagesv2");
client
.build_req_with_skip::<TrafficImageResp, _>(url.as_str(), skip.into())
.build_req_with_skip::<TrafficImageResp, _>(
&concat_string!(client.base_url(), "/Traffic-Imagesv2"),
skip.into(),
)
.await
}

Expand All @@ -131,9 +133,11 @@ pub trait TrafficRequests<C: Client + ClientExt> {
where
S: Into<Option<u32>> + Send,
{
let url = concat_string!(client.base_url(), "/TrafficIncidents");
client
.build_req_with_skip::<TrafficIncidentResp, _>(url.as_str(), skip.into())
.build_req_with_skip::<TrafficIncidentResp, _>(
&concat_string!(client.base_url(), "/TrafficIncidents"),
skip.into(),
)
.await
}

Expand All @@ -144,11 +148,13 @@ pub trait TrafficRequests<C: Client + ClientExt> {
/// **Update freq**: 2 minutes
async fn get_vms_emas<S>(client: &C, skip: S) -> LTAResult<Vec<Vms>>
where
S: Into<Option<u32>> + Send,
S: Into<Option<u32>>,
{
let url = concat_string!(client.base_url(), "/VMS");
client
.build_req_with_skip::<VMSResp, _>(url.as_str(), skip.into())
.build_req_with_skip::<VMSResp, _>(
&concat_string!(client.base_url(), "/VMS"),
skip.into(),
)
.await
}

Expand All @@ -164,5 +170,18 @@ pub trait TrafficRequests<C: Client + ClientExt> {
dist: D,
) -> LTAResult<Vec<BikeParking>>
where
D: Into<Option<f64>> + Send;
D: Into<Option<f64>>;

/// Returns hourly average traffic flow, taken from a representative month of
/// every quarter during 0700-0900 hours.
///
/// **Update freq**: Quaterly
async fn get_traffic_flow(client: &C) -> LTAResult<Vec<String>> {
client
.build_req_with_skip::<TrafficFlowRawResp, _>(
&concat_string!(client.base_url(), "/TrafficFlow"),
None,
)
.await
}
}
13 changes: 13 additions & 0 deletions src/blocking/traffic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,17 @@ pub trait TrafficRequests<C: Client + ClientExt> {
long: f64,
dist: impl Into<Option<f64>>,
) -> LTAResult<Vec<BikeParking>>;

/// Returns hourly average traffic flow, taken from a representative month of
/// every quarter during 0700-0900 hours.
///
/// **Update freq**: Quaterly
fn get_traffic_flow(client: &C) -> LTAResult<Vec<String>> {
client
.build_req_with_skip::<TrafficFlowRawResp, _>(
&concat_string!(client.base_url(), "/TrafficFlow"),
None,
)
.await

Check failure on line 150 in src/blocking/traffic.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

`await` is only allowed inside `async` functions and blocks
}
}
11 changes: 10 additions & 1 deletion src/reqwest_async/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl ClientExt for LTAClient<ReqwestAsync> {

async fn build_req_with_query<T, T2, F>(&self, url: &str, query: F) -> LTAResult<T2>
where
F: Send + FnOnce(Self::RB) -> Self::RB,
F: FnOnce(Self::RB) -> Self::RB,
for<'de> T: serde::Deserialize<'de> + Into<T2>,
{
let rb = self.req_builder(url);
Expand Down Expand Up @@ -198,6 +198,15 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn get_traffic_flow() -> LTAResult<()> {
let client = get_client();
let data = Traffic::get_traffic_flow(&client).await?;
println!("{:?}", &data);
assert_eq!(data.len(), 1);
Ok(())
}

#[ignore]
#[tokio::test]
async fn get_train_service_alerts() -> LTAResult<()> {
Expand Down
9 changes: 9 additions & 0 deletions src/reqwest_blocking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ mod tests {
Ok(())
}

#[test]
fn get_bike_parking() -> LTAResult<()> {

Check failure on line 218 in src/reqwest_blocking/mod.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

the name `get_bike_parking` is defined multiple times
let client = get_client();
let data = Traffic::get_traffic_flow(&client)?;
println!("{:?}", &data);
assert_eq!(data.len(), 1);
Ok(())
}

#[test]
fn get_crowd_density_rt() -> LTAResult<()> {
let client = get_client();
Expand Down

0 comments on commit ee8cd1b

Please sign in to comment.