Skip to main content

Crate publictransport_client

Crate publictransport_client 

Source
Expand description

HTTP client for public transport APIs: EFA, MOTIS, TRIAS, OJP, HAFAS and OpenTripPlanner.

The publictransport crate builds requests and parses responses without doing I/O. This crate sends them with reqwest. It re-exports publictransport and reqwest, so an application depends on this crate alone. The futures need a Tokio runtime, as all of reqwest does.

use publictransport_client::{
    Client, DeparturesQuery, LocationKind, LocationQuery, Network, Protocol, TripsQuery,
};

let client = Client::new("my-app/1.0 (+https://example.org)")?;
let vvo = Network::new("vvo", "VVO", "https://efa.vvo-online.de/std3", Protocol::Efa)
    .with_timezone("Europe/Berlin");

// The text alone searches for every kind of location.
let everything = client.locations(&vvo, "Hauptbahnhof").await?;
// A query type carries the options of a request.
let query = LocationQuery::new("Hauptbahnhof").with_kinds([LocationKind::Stop]);
let stops = client.locations(&vvo, query).await?;
println!("{} locations, {} of them stops", everything.len(), stops.len());
if let [from, to, ..] = stops.as_slice() {
    for departure in client.departures(&vvo, from).await? {
        // The time on the clocks at the stop, where its time zone is known.
        if let Some(time) = departure.stop.local_time(departure.scheduled_time) {
            println!("{time} {} to {:?}", departure.line.name, departure.direction);
        }
    }
    let board = client.departures(&vvo, DeparturesQuery::new(from).with_limit(5)).await?;
    let found = client.trips(&vvo, TripsQuery::new(from, to)).await?;
    println!("{} departures, {} trips", board.len(), found.trips.len());
}

The client does not retry, limit its request rate or cache. Those are decisions for the application.

§Features

The features choose what reqwest is built with. rustls, http2 and system-proxy are on by default.

  • rustls: TLS through rustls. Its aws-lc-rs provider needs a C toolchain to build.
  • native-tls: TLS through the platform’s library. Turn the default features off to use it in place of rustls.
  • http2: HTTP/2.
  • system-proxy: the proxy that the environment configures.

Without rustls and native-tls the client only reaches http:// endpoints.

Re-exports§

pub use reqwest;

Structs§

Area
A part of the world, described by region codes, by polygons or by both.
Attribution
Whom to name as the source of a Network’s data.
Client
Sends requests to public transport servers. One client serves every Network and shares its connections between them.
Coordinate
A WGS 84 position in degrees.
Coverage
Where a Network has data, in three tiers of quality.
CoveringNetwork
A network that covers a position, see Registry::covering.
Departure
One vehicle leaving a stop.
DeparturesQuery
A request for the next departures at a stop.
GlobalId
An identifier of a stop that is shared across networks.
HafasOptions
What a HAFAS mgate server needs to answer, as the operator’s own app sends it. The values come from that app; see Protocol::HafasMgate.
HafasProduct
One kind of vehicle of a HAFAS operator.
Header
One HTTP header. Its debug output leaves the value out, because that can be a key and debug output ends up in logs.
HttpRequest
An HTTP request for the caller to send.
HttpResponse
An HTTP response as the caller received it. The HTTP client must not follow redirects, so that a moved endpoint is reported as Error::Moved.
Leg
One part of a Trip: a ride in one vehicle, or a walk.
LegEnd
The start or the end of a Leg. The fields follow the same rules as those of a Departure.
Line
A line of a public transport network.
LocalTime
A time on the clocks at a place, from Location::local_time. It prints as the time of day, such as 14:08.
Location
A place a trip can start or end at.
LocationQuery
A search for locations by name.
Network
One server that answers public transport queries.
Polygon
A polygon as in GeoJSON: the first ring is the outline, further rings are holes in it.
Registry
A set of networks to choose from.
Stopover
A call at a stop: where it is and when the vehicle arrives and leaves. The fields follow the same rules as those of a Departure.
Timestamp
An instant in UTC. Location::local_time gives it as the clocks at a stop show it.
Trip
One way to get from the start to the destination.
Trips
The answer to a TripsQuery.
TripsQuery
A request for trips from one place to another.

Enums§

ApiError
Why a request could not be built or a response could not be used.
BundledNetwork
A network that ships with the library. Its name is its ID, vvo-efa, in the spelling the language has for such names. Each network was checked against its live server when it was added, and leaves the list when its server is gone for good.
CoverageTier
A tier of a Coverage, best first.
Error
Why a call to a Client failed.
IdScheme
A scheme of identifiers for stops that several networks use.
LegKind
How a Leg is traveled.
LocationKind
The kind of a Location.
Method
An HTTP request method.
Mode
The kind of vehicle that serves a Line.
Operation
One of the three things a Network can be asked.
Place
A place in a query: the stop of a DeparturesQuery or one end of a trip in a TripsQuery. An ID, a position and a location convert into it.
Protocol
The protocol a Network speaks.
TripTime
The time a TripsQuery is bound to.