Crates.io | tfl-api-wrapper |
lib.rs | tfl-api-wrapper |
version | 0.1.4 |
source | src |
created_at | 2023-06-22 14:11:18.216556 |
updated_at | 2024-04-09 11:10:51.586783 |
description | TFL API Wrapper |
homepage | https://github.com/ishantanu/tfl-rust |
repository | https://github.com/ishantanu/tfl-rust |
max_upload_size | |
id | 897438 |
size | 119,010 |
A rust crate for using the Transport for London (TFL) API.
Note: Only the Line API is currently supported. Other APIs are work in progress
Using cargo
, add this to your project's Cargo.toml
:
[dependencies]
tfl-api-wrapper = "0.1.3"
500 Requests per min
(allows 500 requests per min).Primary key
section of your subscription, click on Show
and copy the value. You can use either of Primary key
or Secondary key
.Set the APP_KEY
environment variable.
Instantiate the Client
using:
use tfl_api_wrapper::{Client, RequestBuilder};
let client = Client::new(std::env::var("APP_KEY").unwrap());
Here APP_KEY
could be either Primary key
or Secondary key
.
Get the API version:
async fn it_is_version_1() {
use tfl_api_wrapper::{Client, RequestBuilder};
let client = Client::new(std::env::var("APP_KEY").unwrap());
let ver = client.api_version().fetch().await.unwrap();
}
List valid modes
async fn list_valid_modes() {
use tfl_api_wrapper::{Client, RequestBuilder};
use std::env;
let client = Client::new(env::var("APP_KEY").unwrap().into());
let valid_modes = client.list_modes().fetch().await.unwrap();
}
List severity types
async fn list_severity_types() {
use tfl_api_wrapper::{Client, RequestBuilder};
use std::env;
let client = Client::new(env::var("APP_KEY").unwrap().into());
let severity_types = client.list_severity_types().fetch().await.unwrap();
}
List routes by mode
async fn list_routes_by_mode() {
use tfl_api_wrapper::{Client, RequestBuilder, linemodels, models};
use std::env;
let client = Client::new(env::var("APP_KEY").unwrap().into());
let modes: Vec<models::Mode> = vec![models::Mode::Bus, models::Mode::Tube];
let routes = client
.list_lines_routes_by_modes()
.mode(modes)
.fetch()
.await
.unwrap();
}
Get arrival predictions by lines
async fn get_arrivals_by_lines() {
use tfl_api_wrapper::{Client, RequestBuilder, linemodels};
use std::env;
let client = Client::new(env::var("APP_KEY").unwrap().into());
let lines: Vec<linemodels::LineID> = vec![linemodels::LineID::Bakerloo, linemodels::LineID::Jubilee];
let arrivals = client
.arrival_predictions_by_lines()
.line(lines)
.fetch()
.await
.unwrap();
}
Fetch disruptions by mode
async fn get_disruptions_by_lines() {
use tfl_api_wrapper::{Client, RequestBuilder, linemodels, models};
use std::env;
let client = Client::new(env::var("APP_KEY").unwrap().into());
let modes: Vec<models::Mode> = vec![models::Mode::Bus, models::Mode::Tube];
let disruptions = client
.disruptions_by_mode()
.mode(modes)
.fetch()
.await
.unwrap();
}
You can run the tests by running:
APP_KEY=hjdhajsdas cargo test
As this library maintains a LineID
enum, we have a tool to compare this with
the API's list of valid routes. It reports on the status of LineID
, listing
which lines are missing, which have a mismatched to_string()
mapping and
which are no longer in use.
Run:
APP_KEY=hjdhajsdas cargo run --bin line-id-check