domain.entities.provider_connection¶
src.domain.entities.provider_connection
¶
Provider connection domain entity.
Represents the connection between a user and a financial data provider. Authentication-agnostic - domain has no knowledge of OAuth, API keys, etc.
Architecture
- Pure domain entity (no infrastructure dependencies)
- Uses Result types (railway-oriented programming)
- NO event collection (handlers create events)
- State machine with validated transitions
Reference
- docs/architecture/provider-domain-model.md
Usage
from uuid_extensions import uuid7 from src.domain.entities import ProviderConnection from src.domain.enums import ConnectionStatus
connection = ProviderConnection( id=uuid7(), user_id=user.id, provider_id=provider_uuid, provider_slug="schwab", status=ConnectionStatus.PENDING, )
After successful authentication¶
result = connection.mark_connected(credentials) match result: case Success(_): assert connection.is_connected() case Failure(error): # Handle error
Classes¶
ProviderConnection
dataclass
¶
Connection between a user and a financial data provider.
Represents the user's connection to an external provider like Schwab, Chase, or Fidelity. The connection tracks status, credentials, and sync state.
Authentication Agnostic
Domain layer has no knowledge of authentication mechanisms. Credentials are stored as opaque encrypted blobs with a type hint for the infrastructure layer to handle.
State Machine
PENDING → ACTIVE ↔ EXPIRED/REVOKED → DISCONNECTED
Railway-Oriented Programming
All state transition methods return Result[None, str] instead of raising exceptions. Use pattern matching to handle success/failure.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
UUID
|
Unique connection identifier. |
user_id |
UUID
|
Owning user's ID. |
provider_id |
UUID
|
FK to Provider registry (infrastructure). |
provider_slug |
str
|
Denormalized provider identifier for logging. |
alias |
str | None
|
User-defined nickname (e.g., "My Schwab IRA"). |
status |
ConnectionStatus
|
Current connection status. |
credentials |
ProviderCredentials | None
|
Encrypted credentials (None when pending/disconnected). |
connected_at |
datetime | None
|
When connection was first established. |
last_sync_at |
datetime | None
|
Last successful data synchronization. |
created_at |
datetime
|
Record creation timestamp. |
updated_at |
datetime
|
Last modification timestamp. |
Example
conn = ProviderConnection( ... id=uuid7(), ... user_id=user_id, ... provider_id=provider_id, ... provider_slug="schwab", ... status=ConnectionStatus.PENDING, ... ) conn.is_connected() False result = conn.mark_connected(credentials) match result: ... case Success(_): print("Connected!") ... case Failure(e): print(f"Error: {e}")
Source code in src/domain/entities/provider_connection.py
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 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 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 | |
Functions¶
__post_init__
¶
Validate connection after initialization.
Raises:
| Type | Description |
|---|---|
ValueError
|
If required fields are invalid. |
Note
post_init still raises ValueError for construction errors. These are programming errors, not business logic failures.
Source code in src/domain/entities/provider_connection.py
is_connected
¶
Check if connection is active and usable.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if status is ACTIVE and credentials exist. |
Source code in src/domain/entities/provider_connection.py
needs_reauthentication
¶
Check if connection requires user to re-authenticate.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if status is EXPIRED, REVOKED, or FAILED. |
Source code in src/domain/entities/provider_connection.py
is_credentials_expired
¶
Check if credentials have passed expiration time.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if credentials exist and are expired. |
Source code in src/domain/entities/provider_connection.py
is_credentials_expiring_soon
¶
Check if credentials will expire within 5 minutes.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if credentials exist and expiring soon. |
Source code in src/domain/entities/provider_connection.py
can_sync
¶
Check if connection can perform data synchronization.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if connected and credentials not expired. |
Source code in src/domain/entities/provider_connection.py
mark_connected
¶
Transition to ACTIVE status with credentials.
Called after successful authentication. Updates status and records connection timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
credentials
|
ProviderCredentials
|
Encrypted credentials from provider. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Success |
None
|
Transition successful. |
Failure |
error
|
Credentials missing or invalid state transition. |
Side Effects (on success): - Sets status to ACTIVE - Stores credentials - Sets connected_at if first connection - Updates updated_at
Source code in src/domain/entities/provider_connection.py
mark_disconnected
¶
Transition to DISCONNECTED status.
Called when user explicitly removes the connection. Terminal state - credentials are cleared.
Returns:
| Name | Type | Description |
|---|---|---|
Success |
None
|
Transition successful (always succeeds). |
Side Effects
- Sets status to DISCONNECTED
- Clears credentials
- Updates updated_at
Source code in src/domain/entities/provider_connection.py
mark_expired
¶
Transition to EXPIRED status.
Called when credentials have expired and cannot be refreshed.
Returns:
| Name | Type | Description |
|---|---|---|
Success |
None
|
Transition successful. |
Failure |
error
|
Not currently ACTIVE. |
Side Effects (on success): - Sets status to EXPIRED - Updates updated_at - Does NOT clear credentials (may still contain refresh token)
Source code in src/domain/entities/provider_connection.py
mark_revoked
¶
Transition to REVOKED status.
Called when user or provider explicitly revokes access.
Returns:
| Name | Type | Description |
|---|---|---|
Success |
None
|
Transition successful. |
Failure |
error
|
Not currently ACTIVE. |
Side Effects (on success): - Sets status to REVOKED - Updates updated_at - Does NOT clear credentials (audit trail)
Source code in src/domain/entities/provider_connection.py
mark_failed
¶
Transition to FAILED status.
Called when authentication attempt fails.
Returns:
| Name | Type | Description |
|---|---|---|
Success |
None
|
Transition successful. |
Failure |
error
|
Not currently PENDING. |
Side Effects (on success): - Sets status to FAILED - Updates updated_at
Source code in src/domain/entities/provider_connection.py
update_credentials
¶
Update credentials after token refresh.
Called when credentials are refreshed without user interaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
credentials
|
ProviderCredentials
|
New encrypted credentials. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Success |
None
|
Update successful. |
Failure |
error
|
Credentials missing or not ACTIVE. |
Side Effects (on success): - Updates credentials - Updates updated_at
Source code in src/domain/entities/provider_connection.py
record_sync
¶
Record successful data synchronization.
Updates last_sync_at timestamp.
Returns:
| Name | Type | Description |
|---|---|---|
Success |
None
|
Sync recorded. |
Failure |
error
|
Not currently ACTIVE. |
Side Effects (on success): - Updates last_sync_at - Updates updated_at