Skip to content

swsphn/pmhclib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pmhclib

pmhclib is a Python wrapper for the unofficial PMHC MDS portal API. You can use it to automate uploads and downloads to the PMHC portal.

Install

pmhclib is a Python package. You should be able to install it directly with pip or poetry:

pip install pmhclib@git+https://github.com/swsphn/pmhclib.git

OR

poetry add pmhclib@git+https://github.com/swsphn/pmhclib.git

This will install pmhclib as an importable Python library.

NOTE: This package depends on Playwright to authenticate to the PMHC portal. Once the package is installed, you will need to install a browser for Playwright to use. Make sure you are in the same Python environment in which you installed pmhclib, then run:

playwright install chromium

Usage

pmhclib.PMHC is intended to be used with a context manager. This ensures that the web browser which performs the login process and the API requests is correctly shut down when the script exits. The standard use pattern is as follows:

from pmhclib import PMHC
with PMHC('PHN105') as pmhc:
    pmhc.login()
    ...
    # other pmhc methods.

pmhc.login() will read credentials from the following environment variables if they are set:

PMHC_USERNAME
PMHC_PASSWORD
PMHC_TOTP_SECRET

Otherwise, you will be prompted for credentials interactively.

In PowerShell, you can set the environment variables interactively as follows:

$env:PMHC_USERNAME='your_username_here'
$env:PMHC_PASSWORD=python -c 'import getpass; print(getpass.getpass())'
$env:PMHC_TOTP_SECRET=python -c 'import getpass; print(getpass.getpass("TOTP Secret: "))'

In a Unix shell (Mac, Linux), you can do:

export PMHC_USERNAME='your_username_here'
read -rs PMHC_PASSWORD && export PMHC_PASSWORD
read -rs PMHC_TOTP_SECRET && export PMHC_TOTP_SECRET

NOTE: PMHC_TOTP_SECRET is the unchanging base32-encoded TOTP secret, not the time-based six-digit code. You can likely find this secret in the 'advanced' section of your TOTP app. It will be a long string of upper-case letters and digits. See below for a list of TOTP apps which support viewing the TOTP secret. It is also possible to get the secret by scanning the setup QR code with a generic QR code reader, or by clicking the Trouble scanning button on the website to manually configure the TOTP app:

manually view totp secret

The six-digit code will be automatically calculated based on the current time as required if PMHC_TOTP_SECRET is specified. Otherwise, the user will be prompted to enter the current six-digit code.

Not all TOTP apps support viewing the secret. The following are known to support this:

For more details, see the list of recommended authenticator apps on our Data Wiki.

Documentation

See the online documentation.

Built-in docs

Review the built-in documentation from Python as follows:

>>> from pmhclib import PMHC
>>> help(PMHC)

Build docs

You can also generate html documentation locally using Sphinx if you have a local copy of the repository.

Linux:

cd docs
make html

PowerShell:

cd docs
./make.bat html

The generated documentation can be viewed at docs/_build/html/index.html.