Session and Token Storage¶
SDK API reference for tlm_cloud_sdk.CloudSession and the token storages.
Every client of the SDK is built from a CloudSession. The session reads no configuration file and no default path:
pass the instance, the credentials and a TokenStorage that keeps the token — FileTokenStorage at a path you
choose, MemoryTokenStorage, or your own subclass.
from tlm_cloud_sdk import CloudSession, MemoryTokenStorage
from tlm_cloud_sdk.manufacturing import PMSClient
session = CloudSession.login("tolomeo.io", username, password, token_storage=MemoryTokenStorage())
pms = PMSClient(session)
Runtime Behavior¶
| Behavior | Description |
|---|---|
| Request timeout | 10 seconds; override with the REQUEST_TIMEOUT environment variable, read when the SDK is imported |
| Session binding | PMSClient(session) binds the session to every PMS model in the process; FMClient and EMClient do the same for the /resources models, so a process talks to one instance at a time |
| Thread safety | A CloudSession can be shared between threads; concurrent requests trigger at most one login at a time |
| Token file | FileTokenStorage writes its file atomically with owner-only (0600) permissions |
| Concurrency | Synchronous; call it from a worker thread in an async application |
CloudSession¶
tlm_cloud_sdk.session.CloudSession
¶
Where to reach a ToloMEO instance, and how to get a valid token for it.
Every client of the SDK is built from a session: PMSClient(session), IoTClient(session) and so on.
The session reads no file and no environment of its own: the caller passes the instance, the credentials and
a :class:~tlm_cloud_sdk.auth.TokenStorage that keeps the token between calls.
It is safe to share one session between threads: a token is fetched at most once at a time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
InstanceConfig | str
|
The instance hostname (e.g. |
required |
token_provider
|
TokenProvider
|
Supplies a valid token for each request. |
required |
Examples:
>>> session = CloudSession.login(
... "tolomeo.io", "operator@example.com", "secret", token_storage=MemoryTokenStorage()
... )
>>> pms = PMSClient(session)
Source code in packages/tlm-cloud-sdk/tlm_cloud_sdk/session.py
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 130 131 132 133 134 135 136 | |
instance
property
¶
The target instance, with its derived API and auth URLs.
token_provider
property
¶
The provider every client of this session takes its tokens from.
from_config(config, *, token_storage, log=None)
classmethod
¶
Return a lazily authenticated session from an :class:AppConfig the caller loaded.
Source code in packages/tlm-cloud-sdk/tlm_cloud_sdk/session.py
from_credentials(instance, credentials, *, token_storage, log=None)
classmethod
¶
Return a session that logs in lazily, on the first request that needs a token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
InstanceConfig | str
|
The instance hostname or its :class: |
required |
credentials
|
AuthCredentials
|
Username and password. |
required |
token_storage
|
TokenStorage
|
Where the token is kept. Its key is set to |
required |
log
|
Logger | None
|
Optional logger for the authentication flow. |
None
|
Source code in packages/tlm-cloud-sdk/tlm_cloud_sdk/session.py
jsonapi(path)
¶
Return a jsonapi-requests ORM session for {api_base_url}/{path}.
Each request carries a token that is valid at the time it is sent.
Source code in packages/tlm-cloud-sdk/tlm_cloud_sdk/session.py
login(instance, username, password, *, token_storage, log=None)
classmethod
¶
Log in to the instance's Kratos with a username and password, and return the session.
A valid token already in token_storage for this instance and user is reused; otherwise the Kratos
login flow runs now and the new token is stored there, so a wrong password fails here and not on the
first API call. Later expiries are handled by logging in again on demand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
InstanceConfig | str
|
The instance hostname (e.g. |
required |
username
|
str
|
The Kratos identifier, an email address. |
required |
password
|
str
|
The password. |
required |
token_storage
|
TokenStorage
|
Where the token is kept, e.g. |
required |
log
|
Logger | None
|
Optional logger for the authentication flow. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
CloudSession |
CloudSession
|
An authenticated session. |
Raises:
| Type | Description |
|---|---|
HTTPStatusError
|
If Kratos rejects the login. |
ValueError
|
If Kratos answers in an unexpected format. |
Source code in packages/tlm-cloud-sdk/tlm_cloud_sdk/session.py
url(path)
¶
Return the API URL of path, e.g. https://apis.tolomeo.io/manufacturing.
Token Storage¶
tlm_cloud_sdk.auth.storage.TokenStorage
¶
Bases: ABC
Where a token provider keeps its token between calls (and between runs, for a persistent storage).
Implement this to keep tokens somewhere else (a database, a secret store). A storage holds the token of one
storage_key at a time, typically "<instance>:<username>"; :class:~tlm_cloud_sdk.CloudSession sets it
through :meth:bind_key when the caller did not.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
storage_key
|
str | None
|
Key of the token this storage reads and writes. |
None
|
Source code in packages/tlm-cloud-sdk/tlm_cloud_sdk/auth/storage.py
storage_key
property
¶
Key of the token this storage reads and writes.
bind_key(storage_key)
¶
Set the storage key, unless one was given at construction.
Raises:
| Type | Description |
|---|---|
ValueError
|
If an earlier session already bound another key: the two sessions would read each other's token. Give each session its own storage, or a key at construction. |
Source code in packages/tlm-cloud-sdk/tlm_cloud_sdk/auth/storage.py
get_token()
abstractmethod
¶
tlm_cloud_sdk.auth.storage.FileTokenStorage
¶
Bases: TokenStorage
Stores tokens in a JSON file, keyed by instance:username.
Supports multiple accounts in a single file and auto-cleans expired tokens. Nothing touches the filesystem until the first token is stored: the directory is created then, and the file is written atomically with owner-only permissions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the token storage JSON file, chosen by the caller. |
required |
storage_key
|
str | None
|
Unique key for this token, e.g. "instance:username". |
None
|
Source code in packages/tlm-cloud-sdk/tlm_cloud_sdk/auth/storage.py
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
file_path
property
¶
The token file.
tlm_cloud_sdk.auth.storage.MemoryTokenStorage
¶
Bases: TokenStorage
Keeps the token in memory only: every new process logs in again.