TFL API Wrapper

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

Installation

Using cargo, add this to your project's Cargo.toml: toml [dependencies] tfl-api-wrapper = "0.1.3"

Usage

Get the Keys from TFL API Portal

  1. If you don't already have an account, register on TFL API Portal.
  2. Vist the Products link and create a new subscription for 500 Requests per min (allows 500 requests per min).
  3. Next, visit your profile, scroll down to the Primary key section of your subscription, click on Show and copy the value. You can use either of Primary key or Secondary key.

Use the crate

Set the APP_KEY environment variable.

Instantiate the Client using:

rust 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.

Example

Get the API version: rust 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 ```rust async fn listvalidmodes() { use tflapiwrapper::{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 ```rust async fn listseveritytypes() { use tflapiwrapper::{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 ```rust async fn listroutesbymode() { use tflapi_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 ```rust async fn getarrivalsbylines() { use tflapi_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 ```rust async fn getdisruptionsbylines() { use tflapi_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();

} ```

Tests

You can run the tests by running: sh APP_KEY=hjdhajsdas cargo test

Implemented APIs

Limitations

References/Credits

  1. Existing tfl wrappers
    1. tfl-api-wrapper - NodeJS wrapper for TFL API, made with TypeScript.
    2. tfl-api-wrapper-py - Python wrapper for TFL API
    3. go-tfl - Go client for TFL API
  2. Adzuna-rs - For existing wrapper implementation for Adzuna API.