Set up the Client
Get the latest client for the API from pypi.org: pinexq-client
To authenticate a user to the API, a valid API key is required to derive the user and permissions. It can be obtained from the corresponding portal or from an administrator. The API key is a secret, since it allows full access to your resources, and should be stored securely and not be shared.
To initialize the client the following parts must be supplied:
- The API host: a URL with port if required
- The API key (as header)
from httpx import Client
from pinexq.client.job_management.enterjma import enter_jma, create_pinexq_clientfrom pinexq.client.job_management.hcos.entrypoint_hco import EntryPointHco
# create a configured http clientclient = create_pinexq_client("https://myapihost.com", "my api key")
# the client is now ready to be passed to a function entering the API rootentrypoint: EntryPointHco = enter_jma(client)Note: when using enter_jma() to initially connect to the API, a version check of the client against the API is performed. There will be a warning if your current client is outdated.
Storing the API key
Section titled “Storing the API key”Option 1
Section titled “Option 1”Create a separate file (e.g. secret_key.py) that contains your secret (secret = '<api-key>') and import it from there to use it. This file is then excluded from source control (e.g. .gitignore).
from secret import secret_keyThis ensures the API key stays on your local machine (when not located in shared folder or checked in).
Option 2
Section titled “Option 2”Set a environment variable to the secret key and access it from here:
- Powershell:
$Env:SECRET_KEY = '<api-key>' - CMD:
set SECRET_KEY = '<api-key>' - Bash:
export SECRET_KEY = '<api-key>'
Use the environment variable:
import ossecret_key = os.environ['SECRET_KEY']The Python package python-dotenv makes managing environmental variables for multiple projects simple;
you can set multiple key-value pairs in a .env file, which are loaded as environmental variables (if not already set) with load_dotenv().
This file can be placed in your project directory: if you do, don’t forget to put .env in your .gitignore!
import dotenvload_dotenv('../.env') # default path is ./.env; $SECRET_KEY loaded if not already setsecret_key = os.environ['SECRET_KEY']SECRET_KEY=SecretMyApiKey.envOption 3
Section titled “Option 3”Use keyring to store your secrets
import keyringsecret_key = keyring.get_password('jobmanagement', 'api-key')