#mbta #public-transit #massachusetts

mbta-rs

Simple Rust client for interacting with the MBTA V3 API

2 unstable releases

Uses new Rust 2021

0.1.0 May 12, 2022
0.0.1 May 10, 2022

#58 in HTTP client

Download history 12/week @ 2022-05-05 62/week @ 2022-05-12

74 downloads per month

MIT license

85KB
1.5K SLoC

MBTA Logo

MBTA-RS

A simple Rust client for interacting with the Massachusetts Bay Transport Authority's V3 API*

*This project is not affiliated with any official development work from the MBTA

About

The MBTA V3 API is described as:

A fast, flexible, standards-based API for schedules, arrival predictions, vehicle locations, and service alerts.

This project provides a simple synchronous client and data models to easily consume data from the API within your Rust code.

Built With

  • ureq as the underlying HTTP client
  • Serde and Serde JSON for data deserialization/serialization
  • Chrono for handling datetime data

Why provide a synchronous client rather than an asynchronous one?

  1. I didn't want this crate to be tied down to a specific async runtime
  2. I wanted to use the ureq crate for its simple API and small size, and it only provides a synchronous client

Why not auto-generate a client, given that the OpenAPI/Swagger client code-generators exists?

  1. I'm not very familiar with any of the code generation tools available
  2. I'd personally prefer to have a handcrafted client with some sharper data definitions than one that is auto-generated
  3. There aren't too many API endpoints as of now, so manual maintenance shouldn't be too much of an issue once kicked off
  4. I like subjecting myself to unnecessary and Sisyphean tasks

Usage

It is highly recommended to have the API Swagger docs handy, as it generally contains more detailed and thorough documentation for model field than what is provided here.

In your Cargo.toml file:

[dependencies]
mbta-rs = "*"

# if you need to manipulate/further inspect certain fields
chrono = "*"
serde_json = "*"

Simple example usage:

use std::{collections::HashMap, env};
use mbta_rs::Client;

let client = match env::var("MBTA_TOKEN") {
    Ok(token) => Client::with_key(token),
    Err(_) => Client::without_key()
};

let query_params = HashMap::from([
    ("page[limit]".to_string(), "3".to_string())
]);

let alerts_response = client.alerts(query_params);
if let Ok(response) = alerts_response {
    for alert in response.data {
        println!("MBTA alert: {}", alert.attributes.header);
    }
}

Contribute

See CONTRIBUTE.md to get started!

Other Acknowledgements

Dependencies

~3–4.5MB
~106K SLoC