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 Clientimport os
from pinexq.client.job_management.enterjma import enter_jma, create_pinexq_clientfrom pinexq.client.job_management.hcos.entrypoint_hco import EntryPointHco
api_key = os.environ["PINEXQ_API_KEY"]# create a configured http client to your api endpointclient = create_pinexq_client("https://jobs.api.pinexq.net", 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”There are a few options for safely managing API keys, both for the JMA API and any others you may need.
Option 1
Section titled “Option 1”Create a separate file (e.g. secret_key.py) that contains your secret (api_key = '<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:PINEXQ_API_KEY = '<api-key>' - CMD:
set PINEXQ_API_KEY = '<api-key>' - Bash:
export PINEXQ_API_KEY = '<api-key>'
Use the environment variable:
import osapi_key = os.environ['PINEXQ_API_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; $PINEXQ_API_KEY loaded if not already setapi_key = os.environ['PINEXQ_API_KEY']PINEXQ_API_KEY=SecretMyApiKey.envOption 3
Section titled “Option 3”Use keyring to store your secrets
import keyringsecret_key = keyring.get_password('jobmanagement', 'api-key')