Router Class

mail@pastecode.io avatar
unknown
python
2 years ago
1.9 kB
2
Indexable
Never
class Router(InferringRouter):
    """A FastAPI Inferring Router tailored to Galaxy."""

    def get(self, *args, **kwd):
        """Extend FastAPI.get to accept a require_admin Galaxy flag."""
        
        return super().get(*self._handle_galaxy_args(args, kwd), 
            **self._handle_galaxy_kwd(kwd))

    def patch(self, *args, **kwd):
        """Extend FastAPI.patch to accept a require_admin Galaxy flag."""
        return super().patch(*args, **self._handle_galaxy_kwd(kwd))

    def put(self, *args, **kwd):
        """Extend FastAPI.put to accept a require_admin Galaxy flag."""
        return super().put(*self._handle_galaxy_args(args, kwd), 
            **self._handle_galaxy_kwd(kwd))

    def post(self, *args, **kwd):
        """Extend FastAPI.post to accept a require_admin Galaxy flag."""
        return super().post(*self._handle_galaxy_args(args, kwd), 
            **self._handle_galaxy_kwd(kwd))

    def delete(self, *args, **kwd):
        """Extend FastAPI.delete to accept a require_admin Galaxy flag."""
        return super().delete(*self._handle_galaxy_args(args, kwd), 
            **self._handle_galaxy_kwd(kwd))

    def _handle_galaxy_kwd(self, kwd):
        require_admin = kwd.pop("require_admin", False)
        if require_admin:
            if "dependencies" in kwd:
                kwd["dependencies"].append(AdminUserRequired)
            else:
                kwd["dependencies"] = [AdminUserRequired]

        return kwd

    def _handle_galaxy_args(self, args, kwd):
        alias = kwd.pop("alias", False)
        if alias:
            return (alias,)
        return args

    @property
    def cbv(self):
        """Short-hand for frequently used Galaxy-pattern of FastAPI class based views.

        Creates a class-based view for for this router, for more information see:
        https://fastapi-utils.davidmontague.xyz/user-guide/class-based-views/
        """
        return cbv(self)