Skip to main content

A Pydantic-based wrapper for the TfL Unified API https://api.tfl.gov.uk/. Not associated with or endorsed by TfL.

Project description

pydantic tfl api

I originally used TfL-python-api by @dhilmathy but that verision depends on the msrest package, which has been deprecated for 2+ years. I have created this package to replace it, using pydantic and requests.

This API returns data from the TfL API in a more pythonic way, using pydantic models. It's a thin wrapper around the TfL API, so you can use the TfL API documentation to see what data is available.

Installation

pip install pydantic-tfl-api

Usage

Uses Pydantic so you can use the model_dump_json() method to fully expand all the objects in the result. See Pydantic documentation for more help.

from pydantic_tfl_api.client import Client, ApiToken

app_id = 'APPLICATION ID'
app_key = 'APPLICATION KEY'

token = ApiToken(app_id, app_key)

client = Client(token)
print (client.get_line_meta_modes())
print (client.get_lines(mode="bus")[0].model_dump_json())
print (client.get_lines(line_id="victoria")[0].model_dump_json())
print (client.get_route_by_line_id_with_direction(line_id="northern", direction="all").model_dump_json())

Class structure

The Pydantic classes are in the tfl.models module. The tfl.client module contains the Client class, which is the main class you will use to interact with the API.

Pydantic models are used to represent the data returned by the TfL API. There is a circular reference in the TfL API, so I handled this in the StopPoint model to load the Line model only after StopPoint is fully loaded. Here's a visualisation of the Pydantic models:

classDiagram
    direction LR
    class AffectedRoute
    class RouteSectionNaptanEntrySequence
    AffectedRoute ..> RouteSectionNaptanEntrySequence

    class PredictionTiming
    class Prediction
    Prediction ..> PredictionTiming 

    class Crowding
    class PassengerFlow
    class TrainLoading
    Crowding ..> PassengerFlow
    Crowding ..> TrainLoading

    class Identifier
    Identifier ..> Crowding

    class OrderedRoute
    class RouteSequence
    class MatchedStop
    RouteSequence ..> MatchedStop
    RouteSequence ..> OrderedRoute

    class ApiError
    class Mode


    class Line
    Line ..> Disruption
    Line ..> LineStatus
    Line ..> RouteSection
    Line ..> ServiceType
    Line ..> Crowding

    class RouteSection
    class ServiceType
    class ValidityPeriod
    class LineStatus
    class Disruption

    class StopPoint
    class StopPointsResponse
    class AdditionalProperties
    class StopPointsResponse
    class LineModeGroup
    class LineGroup



    LineStatus ..> ValidityPeriod
    LineStatus ..> Disruption

    Disruption ..> AffectedRoute
    MatchedStop ..> Identifier 

    RouteSectionNaptanEntrySequence ..> StopPoint

    StopPoint ..> LineGroup
    StopPoint ..> LineModeGroup
    StopPoint ..> AdditionalProperties
    StopPoint ..> Line
    StopPointsResponse ..> StopPoint

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_tfl_api-0.25.2.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

pydantic_tfl_api-0.25.2-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_tfl_api-0.25.2.tar.gz.

File metadata

  • Download URL: pydantic_tfl_api-0.25.2.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.5.0-1022-azure

File hashes

Hashes for pydantic_tfl_api-0.25.2.tar.gz
Algorithm Hash digest
SHA256 f154438e3403ddb929a47ee1d0351cf73684764e0d336b3f8be332f0d6b23733
MD5 645cac3f94ba0c34cbe020af69492211
BLAKE2b-256 652ddb59d77bc89d32d79a11acfe7c9affe71538bd530c3dacfcd82682977a17

See more details on using hashes here.

Provenance

File details

Details for the file pydantic_tfl_api-0.25.2-py3-none-any.whl.

File metadata

  • Download URL: pydantic_tfl_api-0.25.2-py3-none-any.whl
  • Upload date:
  • Size: 22.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.5.0-1022-azure

File hashes

Hashes for pydantic_tfl_api-0.25.2-py3-none-any.whl
Algorithm Hash digest
SHA256 66dc57f3737e813f89f0a2fa4b75dfd1c5f065b7e71821f73d7cfc913951ea7f
MD5 656dcc63cb5ec2dc469616b403a5a2e4
BLAKE2b-256 f46c91cb460b44ac69c0cff80666438602c331b917810f3730efee5fdeeb83dd

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page