Skip to main content

The Hopthru API Python Client

Project description

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/*

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

hopthru_api_python_client-0.25.0.tar.gz (36.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hopthru_api_python_client-0.25.0-py3-none-any.whl (21.6 kB view details)

Uploaded Python 3

File details

Details for the file hopthru_api_python_client-0.25.0.tar.gz.

File metadata

File hashes

Hashes for hopthru_api_python_client-0.25.0.tar.gz
Algorithm Hash digest
SHA256 96e5b1e4d7444bea2a12e33c02796f096cc155724c8f3cf03377b90363ac8c79
MD5 1790a93c48052159c4b4d1f50f0b4969
BLAKE2b-256 b9cda43007189cc6684a896d0139f769e604c72399707a57cb32001c6d0527ca

See more details on using hashes here.

File details

Details for the file hopthru_api_python_client-0.25.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hopthru_api_python_client-0.25.0-py3-none-any.whl
Algorithm Hash digest
SHA256 96f02d23eaf4ac57fe0c26c3cbe06711c4fae42245e88feba186ce76766c8601
MD5 54a4af4d0ec056e0ef6c50ad35f2f85b
BLAKE2b-256 3ba373e820a3e182a2864c98314239865f1c8fba45a726f938e12392fb4a7dd9

See more details on using hashes here.

Supported by

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