-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsession.py
More file actions
143 lines (111 loc) · 4.24 KB
/
session.py
File metadata and controls
143 lines (111 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""Session dependencies."""
from typing import Annotated
from fastapi import Cookie, Depends, HTTPException
from fmu_settings_api.config import HttpHeader
from fmu_settings_api.services.session import SessionService
from fmu_settings_api.session import (
ProjectSession,
Session,
SessionNotFoundError,
destroy_fmu_session_if_expired,
get_fmu_session,
)
async def destroy_session_if_expired(
fmu_settings_session: Annotated[str | None, Cookie()] = None,
) -> None:
"""Destroys a session from the session manager if it has expired."""
return (
await destroy_fmu_session_if_expired(fmu_settings_session)
if fmu_settings_session
else None
)
DestroySessionIfExpiredDep = Annotated[None, Depends(destroy_session_if_expired)]
async def get_session(
expired_session_dep: DestroySessionIfExpiredDep,
fmu_settings_session: Annotated[str | None, Cookie()] = None,
) -> Session:
"""Gets an active session from the session manager."""
if not fmu_settings_session:
raise HTTPException(
status_code=401,
detail="No active session found",
headers={
HttpHeader.WWW_AUTHENTICATE_KEY: HttpHeader.WWW_AUTHENTICATE_COOKIE
},
)
try:
return await get_fmu_session(fmu_settings_session)
except SessionNotFoundError as e:
raise HTTPException(
status_code=401,
detail="No active session found",
headers={
HttpHeader.WWW_AUTHENTICATE_KEY: HttpHeader.WWW_AUTHENTICATE_COOKIE
},
) from e
except Exception as e:
raise HTTPException(status_code=500, detail=f"Session error: {e}") from e
SessionDep = Annotated[Session, Depends(get_session)]
async def get_project_session(
session: SessionDep,
fmu_settings_session: str | None = Cookie(None),
) -> ProjectSession:
"""Gets a session with an FMU Project opened from the session manager."""
if not isinstance(session, ProjectSession):
raise HTTPException(
status_code=401,
detail="No FMU project directory open",
)
if not session.project_fmu_directory.path.exists():
raise HTTPException(
status_code=404,
detail="Project .fmu directory not found. It may have been deleted.",
)
return session
ProjectSessionDep = Annotated[ProjectSession, Depends(get_project_session)]
async def ensure_smda_session(session: Session) -> None:
"""Raises exceptions if a session is not SMDA-query capable."""
if (
session.user_fmu_directory.get_config_value("user_api_keys.smda_subscription")
is None
):
raise HTTPException(
status_code=401,
detail="User SMDA API key is not configured",
headers={HttpHeader.UPSTREAM_SOURCE_KEY: HttpHeader.UPSTREAM_SOURCE_SMDA},
)
if session.access_tokens.smda_api is None:
raise HTTPException(
status_code=401,
detail="SMDA access token is not set",
headers={HttpHeader.UPSTREAM_SOURCE_KEY: HttpHeader.UPSTREAM_SOURCE_SMDA},
)
async def get_smda_session(
session: SessionDep,
fmu_settings_session: str | None = Cookie(None),
) -> Session:
"""Gets a session capable of querying SMDA from the session manager."""
await ensure_smda_session(session)
return session
async def get_project_smda_session(
session: ProjectSessionDep,
fmu_settings_session: str | None = Cookie(None),
) -> ProjectSession:
"""Returns a project .fmu session that is SMDA-querying capable."""
await ensure_smda_session(session)
return session
ProjectSmdaSessionDep = Annotated[ProjectSession, Depends(get_project_smda_session)]
async def get_session_service(
session: SessionDep,
) -> SessionService:
"""Returns a SessionService instance for the session."""
return SessionService(session)
SessionServiceDep = Annotated[SessionService, Depends(get_session_service)]
async def get_project_session_service(
session: ProjectSessionDep,
) -> SessionService:
"""Returns a SessionService instance for a project session."""
return SessionService(session)
ProjectSessionServiceDep = Annotated[
SessionService, Depends(get_project_session_service)
]