src.llm.adapters.watsonx.ibm_token_manager.IBMTokenManager
Manages IBM Cloud OAuth2 token lifecycle for WatsonX API access.
This class handles authentication token management for IBM Cloud services, including automatic token refresh and thread-safe token access. It implements a singleton pattern to maintain one token instance across the application.
Attributes:
Name | Type | Description |
---|---|---|
api_key |
str
|
IBM Cloud API key for authentication. |
token_url |
str
|
IBM IAM authentication endpoint URL. |
refresh_buffer |
int
|
Time buffer in seconds before token expiry to trigger refresh. |
access_token |
Optional[str]
|
Current valid access token. |
expiry_time |
float
|
Unix timestamp when the current token expires. |
lock |
Lock
|
Async lock for thread-safe token refresh operations. |
Source code in src/llm/adapters/watsonx/ibm_token_manager.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
__init__(api_key, refresh_buffer=60)
Initialize the IBM Token Manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
api_key
|
str
|
IBM WatsonX API key for authentication. |
required |
refresh_buffer
|
int
|
Buffer time in seconds before token expiry to trigger a refresh. Defaults to 60 seconds. |
60
|
Raises:
Type | Description |
---|---|
ValueError
|
If api_key is empty or None. |
EnvironmentError
|
If IBM_AUTH_URL environment variable is not set. |
Source code in src/llm/adapters/watsonx/ibm_token_manager.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
get_token()
async
Retrieve a valid access token, refreshing if necessary.
Returns:
Type | Description |
---|---|
Optional[str]
|
Optional[str]: Valid access token if successful, None if token refresh fails. |
Raises:
Type | Description |
---|---|
Exception
|
If token refresh fails when attempted. |
Source code in src/llm/adapters/watsonx/ibm_token_manager.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|