application.commands.handlers.create_session_handler¶
src.application.commands.handlers.create_session_handler
¶
Create session handler.
Flow: 1. Enrich device info from user agent 2. Enrich location from IP address 3. Check session limit for user tier 4. Evict oldest session if at limit 5. Create session in database 6. Cache session for fast lookups 7. Publish SessionCreated event 8. Return session ID
Architecture: - Application layer ONLY imports from domain layer (entities, protocols, events) - NO infrastructure imports (repositories are injected via protocols) - Handler orchestrates business logic without knowing persistence details
Classes¶
CreateSessionError
¶
Create session error reasons.
Source code in src/application/commands/handlers/create_session_handler.py
CreateSessionResponse
dataclass
¶
Response data for successful session creation.
Source code in src/application/commands/handlers/create_session_handler.py
CreateSessionHandler
¶
Handler for session creation command.
Orchestrates: - Device and location enrichment - Session limit enforcement (per user tier) - Session persistence (database + cache) - Domain event publishing
Source code in src/application/commands/handlers/create_session_handler.py
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 | |
Functions¶
__init__
¶
__init__(
session_repo: SessionRepository,
session_cache: SessionCache,
user_repo: UserRepository,
device_enricher: DeviceEnricher,
location_enricher: LocationEnricher,
event_bus: EventBusProtocol,
) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_repo
|
SessionRepository
|
Session repository for persistence. |
required |
session_cache
|
SessionCache
|
Session cache for fast lookups. |
required |
user_repo
|
UserRepository
|
User repository to check session tier. |
required |
device_enricher
|
DeviceEnricher
|
Device info enricher. |
required |
location_enricher
|
LocationEnricher
|
Location enricher. |
required |
event_bus
|
EventBusProtocol
|
Event bus for publishing domain events. |
required |
Source code in src/application/commands/handlers/create_session_handler.py
handle
async
¶
Handle create session command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
CreateSession
|
CreateSession command with user_id, ip_address, user_agent. |
required |
Returns:
| Type | Description |
|---|---|
Result[CreateSessionResponse, str]
|
Success(CreateSessionResponse) with session details. |
Result[CreateSessionResponse, str]
|
Failure(error_message) on failure. |
Side Effects
- May evict oldest session if at limit (publishes SessionEvictedEvent).
- Creates session in database.
- Caches session in Redis.
- Publishes SessionCreatedEvent.
Source code in src/application/commands/handlers/create_session_handler.py
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 | |