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.24.0.tar.gz (21.2 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.24.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for hopthru_api_python_client-0.24.0.tar.gz
Algorithm Hash digest
SHA256 fbd77dab2bcbbf54e6b2caa31a975ec0037b8f78536acc605ed0c26ff6fcba88
MD5 83d2aefdaf6c98a6ea39c60594581226
BLAKE2b-256 9b03ba77a6af2169eb790fdc5cec42716a1934a79f9169f1700f64b500c0d917

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hopthru_api_python_client-0.24.0-py3-none-any.whl
Algorithm Hash digest
SHA256 608884ef21d3b33b894775ee1a57e7e32379fbdc1372b7ad550ce59316a16547
MD5 f2745f02ef0321b0c4e2070f5b93854c
BLAKE2b-256 9c4a645197210d2b94f363c721e3bade47fda89c04566d190cb03cb10da49373

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