Expand description
Client for public transport APIs: EFA, MOTIS, TRIAS, OJP, HAFAS and
OpenTripPlanner.
This crate does no I/O. It builds HTTP requests and parses HTTP responses,
so each platform can send them with its own HTTP stack. Every operation is
a pair of methods on Network: one returns the HttpRequest to send,
the other turns the HttpResponse into model types.
use publictransport::{HttpResponse, LocationKind, LocationQuery, Network, Protocol};
let network = Network::new(
"vvo",
"Verkehrsverbund Oberelbe",
"https://efa.vvo-online.de/std3",
Protocol::Efa,
);
let query = LocationQuery::new("Hauptbahnhof").with_kinds([LocationKind::Stop]);
let request = network.locations_request(&query)?;
// Send `request` with an HTTP client and copy its answer into a response.
let response = HttpResponse { status: 200, headers: Vec::new(), body };
for location in network.parse_locations(&response)? {
println!("{} ({})", location.name, location.id);
}Departures and trips work the same way, with
Network::departures_request and Network::parse_departures, and
with Network::trips_request and Network::parse_trips. An EFA
network needs Network::with_timezone for requests that carry a time,
a TRIAS or OJP network needs Network::with_requestor_ref, and a
HAFAS network needs Network::with_hafas and a time zone. Entur
asks every client for its name in the header ET-Client-Name
(company-application) and limits clients without it, so add it with
Network::with_header.
BundledNetwork names networks that were checked against their live
servers, so that they need not be described by hand:
Network::bundled(BundledNetwork::VvoEfa). Registry::bundled holds
them all, and Registry::covering finds the ones for a position.
The HTTP client has two duties:
- It sets a
User-Agentheader. The header identifies the application, so requests carry none. Some servers, such as Transitous, require one. - It does not follow redirects and passes on every response as it is, with
all headers, whatever the status. The parse methods turn failures into
an
Error.
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. - Coordinate
- A WGS 84 position in degrees.
- Coverage
- Where a
Networkhas data, in three tiers of quality. - Covering
Network - A network that covers a position, see
Registry::covering. - Departure
- One vehicle leaving a stop.
- Departures
Query - A request for the next departures at a stop.
- Global
Id - An identifier of a stop that is shared across networks.
- Hafas
Options - 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. - Hafas
Product - 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.
- Http
Request - An HTTP request for the caller to send.
- Http
Response - 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 aDeparture. - Line
- A line of a public transport network.
- Local
Time - A time on the clocks at a place, from
Location::local_time. It prints as the time of day, such as14:08. - Location
- A place a trip can start or end at.
- Location
Query - 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_timegives 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. - Trips
Query - A request for trips from one place to another.
Enums§
- Bundled
Network - 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. - Coverage
Tier - A tier of a
Coverage, best first. - Error
- Why a request could not be built or a response could not be used.
- IdScheme
- A scheme of identifiers for stops that several networks use.
- LegKind
- How a
Legis traveled. - Location
Kind - 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
Networkcan be asked. - Place
- A place in a query: the stop of a
DeparturesQueryor one end of a trip in aTripsQuery. An ID, a position and a location convert into it. - Protocol
- The protocol a
Networkspeaks. - Trip
Time - The time a
TripsQueryis bound to.