App Module¶
The App module provides functions for initializing and securing apps that interact with the Unique platform.
Overview¶
The unique_toolkit.app module encompasses functions for:
- Initializing the SDK and logging
- Handling events from the platform
- Verifying webhook signatures
- Building FastAPI applications
- Running async tasks in parallel
Components¶
Settings¶
unique_toolkit.app.unique_settings.UniqueSettings
¶
Source code in unique_toolkit/unique_toolkit/app/unique_settings.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
_find_env_file(filename='unique.env')
classmethod
¶
Find environment file using cross-platform fallback locations.
Search order: 1. UNIQUE_ENV_FILE environment variable 2. Current working directory 3. User config directory (cross-platform via platformdirs)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the environment file (default: 'unique.env') |
'unique.env'
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the environment file. |
Raises:
| Type | Description |
|---|---|
EnvFileNotFoundError
|
If no environment file is found in any location. |
Source code in unique_toolkit/unique_toolkit/app/unique_settings.py
from_env(env_file=None)
classmethod
¶
Initialize settings from environment variables and/or env file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_file
|
Path | None
|
Optional path to environment file. If provided, will load variables from this file. |
None
|
Returns:
| Type | Description |
|---|---|
UniqueSettings
|
UniqueSettings instance with values loaded from environment/env file. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If env_file is provided but does not exist. |
ValidationError
|
If required environment variables are missing. |
Source code in unique_toolkit/unique_toolkit/app/unique_settings.py
from_env_auto(filename='unique.env')
classmethod
¶
Initialize settings by automatically finding environment file.
This method will automatically search for an environment file in standard locations and fall back to environment variables only if no file is found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the environment file to search for (default: '.env') |
'unique.env'
|
Returns:
| Type | Description |
|---|---|
UniqueSettings
|
UniqueSettings instance with values loaded from found env file or environment variables. |
Source code in unique_toolkit/unique_toolkit/app/unique_settings.py
from_env_auto_with_sdk_init(filename='unique.env')
classmethod
¶
Initialize settings and SDK in one convenient call.
This method combines from_env_auto() and init_sdk() for the most common use case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the environment file to search for (default: '.env') |
'unique.env'
|
Returns:
| Type | Description |
|---|---|
UniqueSettings
|
UniqueSettings instance with SDK already initialized. |
Source code in unique_toolkit/unique_toolkit/app/unique_settings.py
init_sdk()
¶
Initialize the unique_sdk global configuration with these settings.
This method configures the global unique_sdk module with the API key, app ID, and base URL from these settings.
Source code in unique_toolkit/unique_toolkit/app/unique_settings.py
Initialization¶
unique_toolkit.app.init_sdk.init_sdk(strict_all_vars=False)
¶
Initialize the SDK.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strict_all_vars
|
bool
|
This method raises a ValueError if strict and no value is found in the environment. Defaults to False. |
False
|
Source code in unique_toolkit/unique_toolkit/app/init_sdk.py
unique_toolkit.app.init_sdk.init_unique_sdk(*, unique_settings=None, env_file=None)
¶
Source code in unique_toolkit/unique_toolkit/app/init_sdk.py
unique_toolkit.app.init_logging.init_logging(config=unique_log_config)
¶
Event Schemas¶
unique_toolkit.app.schemas.ChatEvent
¶
Bases: BaseEvent
Source code in unique_toolkit/unique_toolkit/app/schemas.py
get_initial_debug_info()
¶
Get the debug information for the chat event
Source code in unique_toolkit/unique_toolkit/app/schemas.py
unique_toolkit.app.schemas.Event
¶
Bases: ChatEvent
Source code in unique_toolkit/unique_toolkit/app/schemas.py
unique_toolkit.app.schemas.BaseEvent
¶
Bases: BaseModel, Generic[FilterOptionsT]
Source code in unique_toolkit/unique_toolkit/app/schemas.py
unique_toolkit.app.schemas.EventName
¶
Bases: StrEnum
Source code in unique_toolkit/unique_toolkit/app/schemas.py
Verification¶
unique_toolkit.app.verification.verify_signature_and_construct_event(headers, payload, endpoint_secret, logger=logger, event_constructor=Event)
¶
Verify the signature of a webhook and construct an event object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
headers
|
Dict[str, str]
|
The headers of the webhook request. |
required |
payload
|
bytes
|
The raw payload of the webhook request. |
required |
endpoint_secret
|
str
|
The secret used to verify the webhook signature. |
required |
logger
|
Logger
|
A logger instance for logging messages. |
logger
|
event_constructor
|
Callable[..., T]
|
A callable that constructs an event object. |
Event
|
Raises:
| Type | Description |
|---|---|
WebhookVerificationError
|
If there's an error during verification or event construction. |
Source code in unique_toolkit/unique_toolkit/app/verification.py
unique_toolkit.app.webhook.is_webhook_signature_valid(headers, payload, endpoint_secret, tolerance=300)
¶
Verify webhook signature from Unique platform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
headers
|
dict[str, str]
|
Request headers with X-Unique-Signature and X-Unique-Created-At |
required |
payload
|
bytes
|
Raw request body bytes |
required |
endpoint_secret
|
str
|
App endpoint secret from Unique platform |
required |
tolerance
|
int
|
Max seconds between timestamp and now (default: 300) |
300
|
Returns:
| Type | Description |
|---|---|
bool
|
True if signature is valid, False otherwise |
Source code in unique_toolkit/unique_toolkit/app/webhook.py
FastAPI Factory¶
unique_toolkit.app.fast_api_factory.build_unique_custom_app(*, title='Unique Chat App', webhook_path='/webhook', settings, event_handler=default_event_handler, event_constructor=ChatEvent, subscribed_event_names=None)
¶
Factory class for creating FastAPI apps with Unique webhook handling.
Source code in unique_toolkit/unique_toolkit/app/fast_api_factory.py
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 | |