utils module
This software is released under the AGPL-3.0 license Copyright (c) 2023-2024 Braedon Hendy
Further updates and packaging added in 2024 through the ClinicianFOCUS initiative, a collaboration with Dr. Braedon Hendy and Conestoga College Institute of Applied Learning and Technology as part of the CNERG+ applied research project, Unburdening Primary Healthcare: An Open-Source AI Clinician Partner Platform”. Prof. Michael Yingbull (PI), Dr. Braedon Hendy (Partner), and Research Students - Software Developer Alex Simko, Pemba Sherpa (F24), and Naitik Patel.
- utils.check_api_key()[source]
Checks and retrieves the API key from the environment variables.
This function checks if the
SESSION_API_KEYenvironment variable is set. If not, it generates a new API key and sets it in the environment variables.- Returns:
The API key stored in the environment variables.
- Return type:
str
Example:
>>> api_key = check_api_key() >>> print(api_key) 'aBcDeFgHiJkLmNoPqRsTuVwXyZ1234567890'
- utils.generate_api_key()[source]
Generates a secure API key.
This function uses the secrets module to generate a secure, URL-safe API key. The key is 32 characters long.
- Returns:
A securely generated API key.
- Return type:
str
Example:
>>> api_key = generate_api_key() >>> print(api_key) 'aBcDeFgHiJkLmNoPqRsTuVwXyZ1234567890'
- utils.get_api_key(credentials: HTTPAuthorizationCredentials = Depends(HTTPBearer)) str[source]
Retrieves and validates the API key from the request headers.
This function retrieves the API key from the request headers using the HTTPBearer scheme. It checks if the provided token matches the one stored in the environment variable
SESSION_API_KEY. If the token is invalid or missing, it raises an HTTPException with a 401 status code.- Parameters:
credentials (HTTPAuthorizationCredentials) – The credentials extracted from the request headers.
- Returns:
The validated API key.
- Return type:
str
- Raises:
HTTPException –
401: If the provided token is invalid or missing.
Example:
>>> api_key = get_api_key(credentials) >>> print(api_key) 'aBcDeFgHiJkLmNoPqRsTuVwXyZ1234567890'
- utils.get_ip_from_headers(request: Request)[source]
Extracts the client’s IP address from the request headers.
If the
X-Forwarded-Forheader is not present, it falls back to using the direct client IP address from the request.- Parameters:
request (Request) – The request object containing the headers and client info.
- Returns:
The client’s IP address, either from the
X-Forwarded-Forheader or fromrequest.client.host.- Return type:
str
Example:
>>> ip = get_ip_from_headers(request) >>> print(ip) '192.168.1.1'
- utils.parse_arguments()[source]
Parses and returns configuration arguments for the application from environment variables.
- Returns:
A dictionary containing the parsed configuration values with keys: -
host: The hostname or IP address. -port: The port number. -whispermodel: The name of the Whisper model.- Return type:
dict
Environment Variables:
WHISPER_HOST: The hostname or IP address on which the application should listen. Default:"0.0.0.0".WHISPER_PORT: The port number on which the application should listen. Default:2224.WHISPER_MODEL: The name of the Whisper model to be used. Default:"medium".USE_GPU: Whether to use the GPU for inference. Default:"True".