Skip to main content

Hopthru API Python Client

This is the Hopthru API Python Client for uploading APC data to Hopthru.

It provides the interface for determining which dates Hopthru expects to receive data for, and for uploading data to Hopthru.

Uploading Correlated APC Data

The following example shows how to upload correlated APC data to Hopthru.

from hopthru_api_client import initialize_hopthru_api_client
from hopthru_api_client.apc_data import upload_correlated_apc_data
from hopthru_api_client.client import HOPTHRU_API_URL

AGENCY_NAME = 'example'
API_KEY = '<obtained from hopthru>'

def get_ridership(start_date, end_date) -> list[str]:
    # Implement agency-specific logic here.
    # It should write a CSV file to the path specified in output_filename.
    return "file_path.csv"

if __name__ == "__main__":
    options, config = initialize_hopthru_api_client()

    upload_correlated_apc_data(
        api_key=config["api_key"],
        apc_data_func=get_ridership,
        start_date=options.start_date,
        end_date=options.end_date,
        hopthru_api_url=config.get("api_url", HOPTHRU_API_URL),
        dry_run=options.dry_run,
    )

The upload_correlated_apc_data function handles command line arguments, connects to the Hopthru API, and determines which date ranges need to be uploaded. It then calls the get_ridership() function provided by the script for each date range, and uploads the file it creates. And it logs the output so the script can be run as a scheduled task.

Uploading Raw APC Data

The following example shows how to upload raw APC data to Hopthru.

from hopthru_api_client import initialize_hopthru_api_client
from hopthru_api_client.apc_data import upload_raw_apc_data
from hopthru_api_client.client import HOPTHRU_API_URL

def get_raw_ridership(start_date, end_date) -> list[str]:
    # Implement agency-specific logic here.
    # It should write a CSV file to the path specified in output_filename.
    return "file_path.csv"

if __name__ == "__main__":
    options, config = initialize_hopthru_api_client()

    upload_raw_apc_data(
        api_key=config["api_key"],
        apc_data_func=get_raw_ridership,
        start_date=options.start_date,
        end_date=options.end_date,
        hopthru_api_url=config.get("api_url", HOPTHRU_API_URL),
        dry_run=options.dry_run,
    )

By default, the upload_raw_apc_data function will query the Hopthru API for the range of dates that need to be uploaded. It will then initiate the upload and lastly perform the upload.

Alternatively, you can specify the start and end dates manually.

Configuration

Configuration data should be stored in a file named hopthru.ini in the same directory.

[hopthru]
agency=<AGENCY_NAME>
api_key=<API_KEY_FROM_HOPTHRU>
log_file=log_file_name.txt
sentry_dsn=<SENTRY_DSN_FROM_HOPTHRU>
sentry_log_level=30
sentry_environment=prod
newrelic_api_key=<NEWRELIC_API_KEY_FROM_HOPTHRU>

Development

This project uses uv for dependency management and ruff for linting and formatting.

  • Install dependencies (creates .venv automatically):
    • uv sync --all-groups
  • Run the test suite:
    • uv run pytest
  • Lint and format:
    • uv run ruff check .
    • uv run ruff format .
  • Install the pre-commit hooks:
    • uv run pre-commit install

Building this package

  • Build the distribution:
    • uv build
  • The distribution will be in the dist folder.

Uploading the package to PyPI

uv run twine upload dist/*

Release files for hopthru-api-python-client 0.25.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hopthru-api-python-client 0.25.0
File Size Uploaded
hopthru_api_python_client-0.25.0.tar.gz 36.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hopthru-api-python-client 0.25.0
File Interpreter ABI Platform
hopthru_api_python_client-0.25.0-py3-none-any.whl Python 3 none any Details

Total release size: 58.4 kB

Release files / hopthru_api_python_client-0.25.0.tar.gz

Download URL hopthru_api_python_client-0.25.0.tar.gz
Size 36.8 kB
Tags Source
SHA-256 checksum
How to use checksums
96e5b1e4d7444bea2a12e33c02796f096cc155724c8f3cf03377b90363ac8c79
BLAKE2b-256 checksum
How to use checksums
b9cda43007189cc6684a896d0139f769e604c72399707a57cb32001c6d0527ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.10

Release files / hopthru_api_python_client-0.25.0-py3-none-any.whl

Download URL hopthru_api_python_client-0.25.0-py3-none-any.whl
Size 21.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
96f02d23eaf4ac57fe0c26c3cbe06711c4fae42245e88feba186ce76766c8601
BLAKE2b-256 checksum
How to use checksums
3ba373e820a3e182a2864c98314239865f1c8fba45a726f938e12392fb4a7dd9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.10

Release history Release notifications | RSS feed

This release

0.25.0 This release

2 release files

0.24.0

2 release files

0.22.0

2 release files

0.21.0

2 release files

0.20.0

2 release files

0.19.2

2 release files

0.19.1

2 release files

0.19.0

2 release files

0.18.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page