-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For now dump them into a single module, but that could become a package of course.
- Loading branch information
Showing
4 changed files
with
134 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from sqlalchemy import ( | ||
and_, | ||
true, | ||
) | ||
from sqlalchemy.orm import joinedload | ||
|
||
from galaxy import model | ||
|
||
|
||
class GalaxySessionManager: | ||
"""Manages GalaxySession.""" | ||
|
||
def __init__(self, sa_session): | ||
self.session = sa_session | ||
|
||
def get_session_from_session_key(self, session_key: str): | ||
"""Returns GalaxySession if session_key is valid.""" | ||
galaxy_session = self.session.query(model.GalaxySession).filter( | ||
and_( | ||
model.GalaxySession.table.c.session_key == session_key, | ||
model.GalaxySession.table.c.is_valid == true()) | ||
).options(joinedload("user")).first() | ||
return galaxy_session |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
""" | ||
This module *does not* contain API routes. It exclusively contains dependencies to be used in FastAPI routes | ||
""" | ||
from functools import lru_cache | ||
from typing import ( | ||
Optional, | ||
) | ||
|
||
from fastapi import ( | ||
Cookie, | ||
Depends, | ||
Header, | ||
HTTPException, | ||
Query, | ||
) | ||
from sqlalchemy.orm import Session | ||
|
||
from galaxy import ( | ||
app as galaxy_app, | ||
exceptions, | ||
model, | ||
) | ||
from galaxy.app import UniverseApplication | ||
from galaxy.managers.session import GalaxySessionManager | ||
from galaxy.managers.users import UserManager | ||
from galaxy.model import User | ||
from galaxy.work.context import SessionRequestContext | ||
|
||
|
||
@lru_cache() | ||
def get_app() -> UniverseApplication: | ||
return galaxy_app.app | ||
|
||
|
||
@lru_cache() | ||
def get_db(app: UniverseApplication = Depends(get_app)) -> Session: | ||
# TODO: return sqlachemy 2.0 style session without autocommit and expire_on_commit! | ||
return app.model.session | ||
|
||
|
||
@lru_cache() | ||
def get_user_manager(app: UniverseApplication = Depends(get_app)): | ||
return UserManager(app) | ||
|
||
|
||
@lru_cache() | ||
def get_session_manager(db=Depends(get_db)) -> GalaxySessionManager: | ||
return GalaxySessionManager(db) | ||
|
||
|
||
@lru_cache() | ||
def get_session(session_manager: GalaxySessionManager = Depends(get_session_manager), | ||
app: UniverseApplication = Depends(get_app), | ||
galaxysession: Optional[str] = Cookie(None)) -> Optional[model.GalaxySession]: | ||
session_key = app.security.decode_guid(galaxysession) | ||
if session_key: | ||
return session_manager.get_session_from_session_key(session_key) | ||
# TODO: What should we do if there is no session? Since this is the API, maybe nothing is the right choice? | ||
|
||
|
||
@lru_cache() | ||
def get_api_user(user_manager: UserManager = Depends(get_user_manager), key: Optional[str] = Query(None), x_api_key: Optional[str] = Header(None)) -> Optional[User]: | ||
api_key = key or x_api_key | ||
if not api_key: | ||
return None | ||
try: | ||
return user_manager.by_api_key(api_key=api_key) | ||
except exceptions.AuthenticationFailed as e: | ||
raise HTTPException(status_code=e.status_code, detail=str(e)) | ||
|
||
|
||
@lru_cache() | ||
def get_user(galaxy_session: Optional[model.GalaxySession] = Depends(get_session), api_user: Optional[User] = Depends(get_api_user)) -> Optional[User]: | ||
if galaxy_session: | ||
return galaxy_session.user | ||
return api_user | ||
|
||
|
||
@lru_cache() | ||
def get_trans(app=Depends(get_app), user: Optional[User] = Depends(get_user), | ||
galaxy_session: Optional[model.GalaxySession] = Depends(get_session), | ||
) -> SessionRequestContext: | ||
return SessionRequestContext(app=app, user=user, galaxy_session=galaxy_session) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters