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>

Building this package

  • Create a virtual environment
    • python -m venv venv
  • Install the Python dependencies:
    • python -m pip install --upgrade pip
    • python -m pip install -r test-requirements.txt
  • Build the distribution:
    • python -m build
  • The distribution will be in the dist folder.

Uploading the package to Pypi

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.22.0.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

hopthru_api_python_client-0.22.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for hopthru_api_python_client-0.22.0.tar.gz
Algorithm Hash digest
SHA256 d9f8a7c36d1a3ce1584cc76179da9fd0e6091d7bef43e3e277a05f9a67fcebf1
MD5 0f6482635eb4352a7899690c90b79931
BLAKE2b-256 23aed8241d9e49eed9a9abccaa071f4ae386c6f09961e370024a791c7ecf0383

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hopthru_api_python_client-0.22.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5c48c9d53de65e3917ff6d98dc38ac12fc49961d25150e2479d84e39db7df45e
MD5 bc676774d2ab160f292ade6058909c6b
BLAKE2b-256 b493b857a509fa31c26c41261e2338ceba763164b064c31d10a2cf7a3a2d5315

See more details on using hashes here.

Supported by

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