pub mod responses;
mod tests;
use crate::{
bus::{
traits::{NeedsRoute, NeedsStop},
urls::URLs,
},
error::Error,
requests::{Fetch, Request as WMATARequest},
Date, RadiusAtLatLong, Route, Stop,
};
use std::str::FromStr;
pub struct Client {
pub key: String,
}
impl Fetch for Client {}
impl Client {
pub fn new(api_key: &str) -> Self {
Client {
key: api_key.to_string(),
}
}
}
impl Client {
pub async fn routes(&self) -> Result<responses::Routes, Error> {
self.fetch::<responses::Routes>(WMATARequest::new(
&self.key,
&URLs::Routes.to_string(),
None,
))
.await
}
pub async fn stops(
&self,
radius_at_lat_long: Option<RadiusAtLatLong>,
) -> Result<responses::Stops, Error> {
if let Some(radius_at_lat_long) = radius_at_lat_long {
self.fetch(WMATARequest::new(
&self.key,
&URLs::Stops.to_string(),
Some(
radius_at_lat_long
.to_query()
.iter()
.map(|(key, value)| (key.as_str(), value.clone()))
.collect(),
),
))
.await
} else {
self.fetch::<responses::Stops>(WMATARequest::new(
&self.key,
&URLs::Stops.to_string(),
None,
))
.await
}
}
}
impl NeedsRoute for Client {}
impl Client {
pub async fn positions_along(
&self,
route: Option<Route>,
radius_at_lat_long: Option<RadiusAtLatLong>,
) -> Result<responses::BusPositions, Error> {
<Self as NeedsRoute>::positions_along(&self, route, radius_at_lat_long, &self.key).await
}
pub async fn incidents_along(
&self,
route: Option<Route>,
) -> Result<responses::Incidents, Error> {
<Self as NeedsRoute>::incidents_along(&self, route, &self.key).await
}
pub async fn path(
&self,
route: Route,
date: Option<Date>,
) -> Result<responses::PathDetails, Error> {
<Self as NeedsRoute>::path(&self, route, date, &self.key).await
}
pub async fn route_schedule(
&self,
route: Route,
date: Option<Date>,
including_variations: bool,
) -> Result<responses::RouteSchedule, Error> {
<Self as NeedsRoute>::route_schedule(&self, route, date, including_variations, &self.key)
.await
}
}
impl NeedsStop for Client {}
impl Client {
pub async fn next_buses(&self, stop: Stop) -> Result<responses::Predictions, Error> {
<Self as NeedsStop>::next_buses(&self, &stop, &self.key).await
}
pub async fn stop_schedule(
&self,
stop: Stop,
date: Option<Date>,
) -> Result<responses::StopSchedule, Error> {
<Self as NeedsStop>::stop_schedule(&self, &stop, date, &self.key).await
}
}
impl FromStr for Client {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Client { key: s.to_string() })
}
}