Skip to content

Commit

Permalink
Add disruption by line support and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ishantanu committed Jul 17, 2023
1 parent 45a48a4 commit 9327967
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 17 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# TFL API Wrapper

A rust crate for using the [Transport for London (TFL) API](https://api.tfl.gov.uk).
[Cargo.toml](Cargo.toml)

*Note: This is currently WIP and not ready for use.*

## Installation
Expand All @@ -12,9 +12,6 @@ Using `cargo`, add this to your project's `Cargo.toml`:
tfl-api-wrapper = "0.1.2"
```

## Implemented APIs
Currently only the version for the API is implemented.

## Usage

### Get the Keys from TFL API Portal
Expand Down Expand Up @@ -50,6 +47,16 @@ You can run the tests by running:
```sh
APP_KEY=hjdhajsdas cargo test
```
## Implemented APIs
- Version
- Line
- Get all valid routes for all lines, including the name and id of the originating and terminating stops for each route.
- Get all valid routes for given line ids, including the name and id of the originating and terminating stops for each route.
- Get disruptions for all lines of the given modes.
- Get disruptions for the given line ids.

## Limitations
- Currently fetching of disruptions and routes is supported for single line only. Support for specifying multiple lines will be added in the future.

## References/Credits
1. Existing tfl wrappers
Expand Down
6 changes: 5 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
line::{DisruptionByMode, RouteRequest, RouteRequestById},
line::{DisruptionByLine, DisruptionByMode, RouteRequest, RouteRequestById},
request::*,
};

Expand Down Expand Up @@ -39,4 +39,8 @@ impl Client {
pub fn disruptions_by_mode(&self) -> DisruptionByMode<'_> {
DisruptionByMode::new(self)
}

pub fn disruptions_by_line(&self) -> DisruptionByLine<'_> {
DisruptionByLine::new(self)
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ pub use self::client::Client;
pub use self::request::RequestBuilder;

pub mod client;
pub mod line;
pub mod models;
pub mod request;
pub mod line;
36 changes: 33 additions & 3 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl RequestBuilder for RouteRequestById<'_> {
fn get_request_url(&self) -> String {
format!(
"/Line/{}/Route?{}",
self.get_parameters().line_id,
self.get_parameters().line,
self.get_parameters().service_type
)
}
Expand All @@ -50,7 +50,7 @@ impl RequestBuilder for RouteRequestById<'_> {
impl RouteRequestById<'_> {
// filter by line id
pub fn line(mut self, line: models::LineID) -> Self {
self.parameters.line_id = line.line().to_string();
self.parameters.line = line.line().to_string();
self
}

Expand All @@ -74,7 +74,7 @@ impl RequestBuilder for DisruptionByMode<'_> {
}

fn get_request_url(&self) -> String {
format!("/Line/Mode/{}/Disruption", self.get_parameters().mode,)
format!("/Line/Mode/{}/Disruption", self.get_parameters().mode)
}
}

Expand All @@ -85,3 +85,33 @@ impl DisruptionByMode<'_> {
self
}
}

create_endpoint!(DisruptionByLine);

impl RequestBuilder for DisruptionByLine<'_> {
type Response = models::Disruption;

fn get_client(&self) -> &Client {
self.client
}

fn get_parameters(&self) -> &models::Parameters {
&self.parameters
}

fn get_request_url(&self) -> String {
println!(
"{}",
format!("Line/{}/Disruption", self.get_parameters().line)
);
format!("/Line/{}/Disruption", self.get_parameters().line)
}
}

impl DisruptionByLine<'_> {
// filter by line
pub fn line(mut self, line: models::LineID) -> Self {
self.parameters.line = line.line().to_string();
self
}
}
2 changes: 1 addition & 1 deletion src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl LineID {
#[derive(Debug, Default, Serialize)]
pub struct Parameters {
#[serde(flatten)]
pub line_id: String,
pub line: String,
pub service_type: String,
pub mode: String,
}
26 changes: 19 additions & 7 deletions tests/tfl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod tests {
}

#[tokio::test]
async fn it_is_version_1() {
async fn it_is_expected_version() {
let client = get_client();
let ver = client.api_version().fetch().await.unwrap();
println!("aaa {:?}", ver);
Expand All @@ -26,16 +26,11 @@ mod tests {
.fetch()
.await
.unwrap();
for trains in routes.clone() {
if trains.mode_name == "tube" {
println!("{}", trains.name)
}
}
assert!(!routes.is_empty());
}

#[tokio::test]
async fn it_fetches_routes_by_id() {
async fn it_fetches_routes_by_line() {
let client = get_client();

let route = client
Expand Down Expand Up @@ -63,4 +58,21 @@ mod tests {
assert!(!disruptions.is_empty());
}
}

#[tokio::test]
async fn it_fetches_disruptions_by_line() {
let client = get_client();

let disruptions = client
.disruptions_by_line()
.line(models::LineID::Bakerloo)
.fetch()
.await
.unwrap();
if disruptions.len() == 0 {
assert!(disruptions.is_empty());
} else {
assert!(!disruptions.is_empty());
}
}
}

0 comments on commit 9327967

Please sign in to comment.