use crate::api_url;
use crate::models::traffic::prelude::*;
use crate::r#async::{build_req_with_query, build_req_with_skip, LTAClient};
use crate::{Client, LTAError, LTAResult, Traffic};
use async_trait::async_trait;
#[async_trait]
pub trait TrafficRequests<C: Client> {
async fn get_erp_rates<S>(client: &C, skip: S) -> LTAResult<Vec<ErpRate>>
where
S: Into<Option<u32>> + Send;
async fn get_carpark_avail<S>(client: &C, skip: S) -> LTAResult<Vec<CarPark>>
where
S: Into<Option<u32>> + Send;
async fn get_est_travel_time<S>(client: &C, skip: S) -> LTAResult<Vec<EstTravelTime>>
where
S: Into<Option<u32>> + Send;
async fn get_faulty_traffic_lights<S>(
client: &C,
skip: S,
) -> LTAResult<Vec<FaultyTrafficLight>>
where
S: Into<Option<u32>> + Send;
async fn get_road_details<S>(
client: &C,
road_details_type: RoadDetailsType,
skip: S,
) -> LTAResult<Vec<RoadDetails>>
where
S: Into<Option<u32>> + Send;
async fn get_traffic_speed_band<S>(client: &C, skip: S) -> LTAResult<Vec<TrafficSpeedBand>>
where
S: Into<Option<u32>> + Send;
async fn get_traffic_images<S>(client: &C, skip: S) -> LTAResult<Vec<TrafficImage>>
where
S: Into<Option<u32>> + Send;
async fn get_traffic_incidents<S>(client: &C, skip: S) -> LTAResult<Vec<TrafficIncident>>
where
S: Into<Option<u32>> + Send;
async fn get_vms_emas<S>(client: &C, skip: S) -> LTAResult<Vec<Vms>>
where
S: Into<Option<u32>> + Send;
async fn get_bike_parking<D>(
client: &C,
lat: f64,
long: f64,
dist: D,
) -> LTAResult<Vec<BikeParking>>
where
D: Into<Option<f64>> + Send;
}
#[async_trait]
impl TrafficRequests<LTAClient> for Traffic {
async fn get_erp_rates<S>(client: <AClient, skip: S) -> LTAResult<Vec<ErpRate>>
where
S: Into<Option<u32>> + Send,
{
build_req_with_skip::<ErpRatesResp, _, _>(client, api_url!("/ERPRates"), skip.into()).await
}
async fn get_carpark_avail<S>(client: <AClient, skip: S) -> LTAResult<Vec<CarPark>>
where
S: Into<Option<u32>> + Send,
{
build_req_with_skip::<CarparkAvailResp, _, _>(
client,
api_url!("/CarParkAvailabilityv2"),
skip.into(),
)
.await
}
async fn get_est_travel_time<S>(client: <AClient, skip: S) -> LTAResult<Vec<EstTravelTime>>
where
S: Into<Option<u32>> + Send,
{
build_req_with_skip::<EstTravelTimeResp, _, _>(
client,
api_url!("/EstTravelTimes"),
skip.into(),
)
.await
}
async fn get_faulty_traffic_lights<S>(
client: <AClient,
skip: S,
) -> LTAResult<Vec<FaultyTrafficLight>>
where
S: Into<Option<u32>> + Send,
{
build_req_with_skip::<FaultyTrafficLightResp, _, _>(
client,
api_url!("/FaultyTrafficLights"),
skip.into(),
)
.await
}
async fn get_road_details<S>(
client: <AClient,
road_details_type: RoadDetailsType,
skip: S,
) -> LTAResult<Vec<RoadDetails>>
where
S: Into<Option<u32>> + Send,
{
let url = match road_details_type {
RoadDetailsType::RoadOpening => api_url!("/RoadOpenings"),
RoadDetailsType::RoadWorks => api_url!("/RoadWorks"),
_ => return Err(LTAError::UnknownEnumVariant),
};
build_req_with_skip::<RoadDetailsResp, _, _>(client, url, skip.into()).await
}
async fn get_traffic_speed_band<S>(
client: <AClient,
skip: S,
) -> LTAResult<Vec<TrafficSpeedBand>>
where
S: Into<Option<u32>> + Send,
{
build_req_with_skip::<TrafficSpeedBandResp, _, _>(
client,
api_url!("/TrafficSpeedBandsv2"),
skip.into(),
)
.await
}
async fn get_traffic_images<S>(client: <AClient, skip: S) -> LTAResult<Vec<TrafficImage>>
where
S: Into<Option<u32>> + Send,
{
build_req_with_skip::<TrafficImageResp, _, _>(
client,
api_url!("/Traffic-Imagesv2"),
skip.into(),
)
.await
}
async fn get_traffic_incidents<S>(
client: <AClient,
skip: S,
) -> LTAResult<Vec<TrafficIncident>>
where
S: Into<Option<u32>> + Send,
{
build_req_with_skip::<TrafficIncidentResp, _, _>(
client,
api_url!("/TrafficIncidents"),
skip.into(),
)
.await
}
async fn get_vms_emas<S>(client: <AClient, skip: S) -> LTAResult<Vec<Vms>>
where
S: Into<Option<u32>> + Send,
{
build_req_with_skip::<VMSResp, _, _>(client, api_url!("/VMS"), skip.into()).await
}
async fn get_bike_parking<D>(
client: <AClient,
lat: f64,
long: f64,
dist: D,
) -> LTAResult<Vec<BikeParking>>
where
D: Into<Option<f64>> + Send,
{
let unwrapped_dist = dist.into().unwrap_or(0.5);
build_req_with_query::<BikeParkingResp, _, _, _>(
client,
api_url!("/BicycleParkingv2"),
|rb| rb.query(&[("Lat", lat), ("Long", long), ("Dist", unwrapped_dist)]),
)
.await
}
}