|
22 | 22 | maxsize=1000, ttl=60 * 60 |
23 | 23 | ) |
24 | 24 |
|
| 25 | +# Shared HTTP client for TDEI backend calls. Initialized by main.py lifespan. |
| 26 | +_tdei_client: httpx.AsyncClient | None = None |
| 27 | + |
| 28 | + |
| 29 | +def init_tdei_client() -> None: |
| 30 | + global _tdei_client |
| 31 | + _tdei_client = httpx.AsyncClient( |
| 32 | + base_url=settings.TDEI_BACKEND_URL, |
| 33 | + timeout=httpx.Timeout(connect=10, read=30, write=30, pool=10), |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +async def close_tdei_client() -> None: |
| 38 | + global _tdei_client |
| 39 | + if _tdei_client is not None: |
| 40 | + await _tdei_client.aclose() |
| 41 | + _tdei_client = None |
| 42 | + |
| 43 | + |
25 | 44 | security = HTTPBearer() |
26 | 45 |
|
27 | 46 |
|
@@ -119,6 +138,7 @@ def get_task_db_session( |
119 | 138 | ) -> AsyncSession: |
120 | 139 | return session |
121 | 140 |
|
| 141 | + |
122 | 142 | async def validate_token( |
123 | 143 | credentials: HTTPAuthorizationCredentials = Depends(security), |
124 | 144 | osm_db_session: AsyncSession = Depends(get_osm_db_session), |
@@ -185,32 +205,30 @@ async def _validate_token_uncached( |
185 | 205 | r.user_name = payload.get("preferred_username", "unknown") |
186 | 206 |
|
187 | 207 | # get user's project groups and roles from TDEI |
188 | | - pg_base_url = f"{settings.TDEI_BACKEND_URL}/project-group-roles/{user_id}" |
189 | 208 | pgs = [] |
190 | | - async with httpx.AsyncClient() as http_client: |
191 | | - response = await http_client.get( |
192 | | - pg_base_url, |
193 | | - headers=headers, |
194 | | - params={"page_no": 1, "page_size": 1000}, |
195 | | - ) |
| 209 | + response = await _tdei_client.get( |
| 210 | + f"project-group-roles/{user_id}", |
| 211 | + headers=headers, |
| 212 | + params={"page_no": 1, "page_size": 1000}, |
| 213 | + ) |
| 214 | + |
| 215 | + # token is not valid or server unavailable |
| 216 | + if response.status_code != 200: |
| 217 | + raise credentials_exception |
| 218 | + |
| 219 | + try: |
| 220 | + pg_data = response.json() |
| 221 | + except Exception: |
| 222 | + raise credentials_exception |
196 | 223 |
|
197 | | - # token is not valid or server unavailable |
198 | | - if response.status_code != 200: |
199 | | - raise credentials_exception |
200 | | - |
201 | | - try: |
202 | | - pg_data = response.json() |
203 | | - except Exception: |
204 | | - raise credentials_exception |
205 | | - |
206 | | - for i in pg_data: |
207 | | - pgs.append( |
208 | | - UserInfoPGMembership( |
209 | | - project_group_id=i["tdei_project_group_id"], |
210 | | - project_group_name=i["project_group_name"], |
211 | | - tdeiRoles=i["roles"], |
212 | | - ) |
| 224 | + for i in pg_data: |
| 225 | + pgs.append( |
| 226 | + UserInfoPGMembership( |
| 227 | + project_group_id=i["tdei_project_group_id"], |
| 228 | + project_group_name=i["project_group_name"], |
| 229 | + tdeiRoles=i["roles"], |
213 | 230 | ) |
| 231 | + ) |
214 | 232 |
|
215 | 233 | r.projectGroups = pgs |
216 | 234 |
|
|
0 commit comments