CLI¶
Constants¶
consts ¶
CLI constants and environment variable keys for t3api_utils configuration.
Defines default values and the :class:EnvKeys enum whose members map to
environment variables (and .t3.env file keys) that control authentication,
HTTP behaviour, performance tuning, and output settings.
DEFAULT_OTP_WHITELIST
module-attribute
¶
Metrc hostnames that require a one-time password for authentication.
DEFAULT_CREDENTIAL_EMAIL_WHITELIST
module-attribute
¶
Metrc hostnames that require an email address for authentication.
DEFAULT_T3_API_HOST
module-attribute
¶
Default base URL for the T3 API when no override is configured.
DEFAULT_ENV_PATH
module-attribute
¶
Default path to the dotenv file where credentials and settings are persisted.
EnvKeys ¶
Bases: str, Enum
Environment variable keys recognised by :class:t3api_utils.cli.utils.ConfigManager.
Each member's value is the literal env-var / dotenv key string. Members are grouped by concern: authentication, connection, performance, hostname behaviour, development, and output.
METRC_HOSTNAME
class-attribute
instance-attribute
¶
Metrc state hostname (e.g. ca.metrc.com).
METRC_USERNAME
class-attribute
instance-attribute
¶
Metrc account username.
METRC_PASSWORD
class-attribute
instance-attribute
¶
Metrc account password.
METRC_EMAIL
class-attribute
instance-attribute
¶
Email address for hostnames that require it (see :data:DEFAULT_CREDENTIAL_EMAIL_WHITELIST).
JWT_TOKEN
class-attribute
instance-attribute
¶
Pre-issued JWT bearer token for direct authentication.
API_KEY
class-attribute
instance-attribute
¶
T3 API key for API-key-based authentication.
API_STATE_CODE
class-attribute
instance-attribute
¶
Two-letter US state code associated with the API key.
T3_API_HOST
class-attribute
instance-attribute
¶
Base URL of the T3 API (overrides :data:DEFAULT_T3_API_HOST).
HTTP_TIMEOUT
class-attribute
instance-attribute
¶
Overall HTTP request timeout in seconds.
HTTP_CONNECT_TIMEOUT
class-attribute
instance-attribute
¶
TCP connection timeout in seconds.
HTTP_READ_TIMEOUT
class-attribute
instance-attribute
¶
HTTP response read timeout in seconds.
VERIFY_SSL
class-attribute
instance-attribute
¶
Whether to verify TLS certificates ("true" / "false").
MAX_WORKERS
class-attribute
instance-attribute
¶
Maximum number of parallel workers for concurrent data fetching.
BATCH_SIZE
class-attribute
instance-attribute
¶
Number of pages to fetch per batch in async parallel loading.
RATE_LIMIT_RPS
class-attribute
instance-attribute
¶
Maximum requests per second for the rate limiter.
RATE_LIMIT_BURST
class-attribute
instance-attribute
¶
Maximum burst size allowed by the rate limiter.
RETRY_MAX_ATTEMPTS
class-attribute
instance-attribute
¶
Maximum number of retry attempts for failed HTTP requests.
RETRY_BACKOFF_FACTOR
class-attribute
instance-attribute
¶
Exponential backoff multiplier between retries.
RETRY_MIN_WAIT
class-attribute
instance-attribute
¶
Minimum wait time in seconds between retries.
OTP_WHITELIST
class-attribute
instance-attribute
¶
Comma-separated list of hostnames requiring OTP authentication.
EMAIL_WHITELIST
class-attribute
instance-attribute
¶
Comma-separated list of hostnames requiring an email address.
OTP_SEED
class-attribute
instance-attribute
¶
TOTP seed for automatic one-time password generation.
LOG_LEVEL
class-attribute
instance-attribute
¶
Logging level (e.g. DEBUG, INFO, WARNING).
DEBUG_MODE
class-attribute
instance-attribute
¶
Enable debug mode ("true" / "false").
CACHE_RESPONSES
class-attribute
instance-attribute
¶
Cache API responses locally ("true" / "false").
T3_LOG_HTTP
class-attribute
instance-attribute
¶
Enable verbose HTTP request/response logging ("true" / "false").
T3_LOG_HEADERS
class-attribute
instance-attribute
¶
Log request headers when HTTP logging is enabled ("true" / "false", default "true").
T3_LOG_BODY
class-attribute
instance-attribute
¶
Log request body/payload when HTTP logging is enabled ("true" / "false", default "true").
T3_LOG_FILE
class-attribute
instance-attribute
¶
File path for HTTP debug logs. File is truncated on each run. Empty to disable file logging.
OUTPUT_DIR
class-attribute
instance-attribute
¶
Directory for CSV/JSON output files.
AUTO_OPEN_FILES
class-attribute
instance-attribute
¶
Automatically open exported files after saving ("true" / "false").
STRIP_EMPTY_COLUMNS
class-attribute
instance-attribute
¶
Remove columns that are entirely empty from CSV output ("true" / "false").
DEFAULT_FILE_FORMAT
class-attribute
instance-attribute
¶
Default export format ("csv" or "json").
Utilities¶
utils ¶
CLI utility functions for configuration management and authentication.
Provides the ConfigManager class for reading and auto-generating the
.t3.env configuration file, as well as helper functions for loading,
prompting, and persisting Metrc credentials.
DEFAULT_ENV_PATH
module-attribute
¶
Default path to the dotenv file where credentials and settings are persisted.
ConfigManager ¶
Centralized configuration manager for .t3.env file.
Handles auto-generation, validation, and reading of the comprehensive .t3.env configuration file with all t3api-utils settings.
Initialize the configuration manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str
|
Filesystem path to the |
DEFAULT_ENV_PATH
|
Source code in t3api_utils/cli/utils.py
ensure_config_exists ¶
Ensure .t3.env exists and is complete, auto-generating if needed.
Creates the configuration file when it is missing or regenerates it when required keys are absent. Prints an info message when the file is generated or updated.
Source code in t3api_utils/cli/utils.py
get_config_value ¶
Get a configuration value with automatic type conversion.
Loads the configuration file, reads the requested key, and converts the raw string to the appropriate Python type (bool, float, int, or set) based on the key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
EnvKeys
|
The |
required |
default
|
Any
|
Fallback value returned when the key is missing or type conversion fails. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The converted configuration value, or |
Any
|
absent or conversion fails. |
Source code in t3api_utils/cli/utils.py
get_otp_whitelist ¶
Get the set of hostnames that require OTP authentication.
Returns:
| Type | Description |
|---|---|
Set[str]
|
Set of Metrc hostnames requiring OTP, falling back to |
Set[str]
|
|
Source code in t3api_utils/cli/utils.py
get_email_whitelist ¶
Get the set of hostnames that require an email during authentication.
Returns:
| Type | Description |
|---|---|
Set[str]
|
Set of Metrc hostnames requiring email, falling back to |
Set[str]
|
|
Source code in t3api_utils/cli/utils.py
get_api_host ¶
Get the T3 API base URL.
Returns:
| Type | Description |
|---|---|
str
|
The configured API host string, falling back to |
str
|
|
Source code in t3api_utils/cli/utils.py
get_otp_seed ¶
Get the Base32-encoded OTP seed for TOTP generation.
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The trimmed seed string, or |
Optional[str]
|
configured or is empty. |
Source code in t3api_utils/cli/utils.py
generate_otp_from_seed ¶
Generate a 6-digit TOTP code from the OTP_SEED environment variable.
Returns:
| Type | Description |
|---|---|
Optional[str]
|
6-digit OTP string if OTP_SEED is configured, None otherwise. |
Raises:
| Type | Description |
|---|---|
AuthenticationError
|
If OTP_SEED is malformed or TOTP generation fails. |
Source code in t3api_utils/cli/utils.py
load_credentials_from_env ¶
Load credential values from the .t3.env environment file.
Reads all available authentication values including Metrc credentials, JWT tokens, and API key credentials. Only non-empty values are included in the returned dictionary.
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
Dictionary mapping credential names (e.g. |
Dict[str, str]
|
|
Dict[str, str]
|
Keys with empty or missing values are omitted. |
Source code in t3api_utils/cli/utils.py
offer_to_save_credentials ¶
Offer to save credentials to the .t3.env file.
If the environment file does not exist, the user is prompted to create it. If it exists but any credential values differ from those provided, the user is prompted to update the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
credentials
|
T3Credentials
|
The |
required |
Source code in t3api_utils/cli/utils.py
offer_to_save_jwt_token ¶
Offer to save a JWT token to the .t3.env file.
If the environment file does not exist, the user is prompted to create it. If it exists but the stored token differs, the user is prompted to update the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
jwt_token
|
str
|
The JWT token string to compare and potentially save. |
required |
Source code in t3api_utils/cli/utils.py
offer_to_save_api_key ¶
Offer to save an API key and state code to the .t3.env file.
If the environment file does not exist, the user is prompted to create it. If it exists but either the API key or state code differs, the user is prompted to update the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
The API key string to compare and potentially save. |
required |
state_code
|
str
|
The two-letter state code to compare and potentially save. |
required |
Source code in t3api_utils/cli/utils.py
prompt_for_credentials_or_error ¶
Prompt for any missing credentials, using provided values if available.
Pre-populated values (typically loaded from the environment) are used without prompting. Missing values are requested interactively. OTP and email prompts are shown only when the hostname requires them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
object
|
Optional pre-populated credential values. Recognized keys
are |
{}
|
Returns:
| Type | Description |
|---|---|
T3Credentials
|
A fully-populated |
Raises:
| Type | Description |
|---|---|
AuthenticationError
|
If a required credential is missing or empty, the OTP code is invalid, or the email address is malformed. |
Source code in t3api_utils/cli/utils.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 | |
resolve_auth_inputs_or_error ¶
Resolve authentication credentials from env and/or interactive prompt.
Loads any stored credentials from the environment file, prompts the
user for missing values, and offers to persist the final set back to
the .t3.env file.
Returns:
| Type | Description |
|---|---|
T3Credentials
|
A fully-populated |
T3Credentials
|
the authentication client. |
Raises:
| Type | Description |
|---|---|
AuthenticationError
|
If required credentials cannot be resolved
(propagated from |