Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🚉 Public Transport SDK

A client library for public transport APIs, for Rust, Python, Swift, Kotlin and TypeScript. The libraries use a shared model for stops, departures and trips that covers several server types, so an application can query any supported network the same way.

The project is at an early stage. It can search for locations by name, list the departures at a stop and plan trips between two places on servers that speak one of six protocols:

  • EFA, the JSON interface of MDV’s journey planner, used by many German transport associations
  • MOTIS, the routing engine behind Transitous
  • TRIAS 1.2, the German XML standard VDV 431-2
  • OJP 1.0, its European successor
  • HAFAS mgate, the JSON interface behind the apps of many operators
  • OpenTripPlanner 2 with its Transmodel API

The library ships with networks that were checked against their live servers, named by BundledNetwork and listed by Registry::bundled(): VVO, VRR, Transitous, Entur and the HAFAS servers of about 35 operators in Europe and the USA. Further networks can be described in code or read from entries in the format of transport-apis. Each network says where it has coverage, so an application can pick the networks for a position.

Design

The core crate does no I/O. It builds HTTP requests and parses HTTP responses, and the caller sends them. This keeps the core usable from other languages and from WebAssembly, where each platform brings its own HTTP stack. The crate documentation of publictransport shows the request and parse steps and what the HTTP client has to do.

Rust applications that do not want to bring their own HTTP client use publictransport-client, which sends the requests with reqwest. Packages for Python, Swift, Kotlin and TypeScript wrap the same core, each with a client on its platform’s HTTP stack: httpx, URLSession, OkHttp and fetch.

The documentation has a guide for each language, and examples has a short program for each kind of request.

Install

LanguagePackage
Rustcargo add publictransport-client, or publictransport alone to bring your own HTTP clientcrates/client, crates/core
Pythonuv add publictransport or pip install publictransportpackages/python
Swift.package(url: "https://github.com/kiliankoe/publictransport", from: "0.1.2"), product PublicTransportpackages/swift
Kotlinimplementation("io.github.kiliankoe:publictransport:0.1.2")packages/kotlin
TypeScriptpnpm add @kiliankoe/publictransport or npm install @kiliankoe/publictransportpackages/typescript

Shared Core

Every language runs the same Rust core, so a request is built and an answer is read the same way everywhere.

  • The model is defined once, in Rust. Each language gets the types that UniFFI generates from it. To make things more idiomatic, the generator’s settings and a small script adapt details, such as scripts/swift-idioms.py and scripts/kotlin-idioms.py. TypeScript runs the core as WebAssembly and gets its types from wasm-bindgen and tsify, steered by attributes on the Rust types.
  • Written by hand in each language are the client on the platform’s HTTP stack, and the bridges to the platform’s own types, such as TimeZone in Swift or the error classes in TypeScript.
  • A convenience that every language can use goes into the Rust code, not into one package.