Skip to content

Commit

Permalink
Merge pull request #21 from cuenca-mx/exceptions
Browse files Browse the repository at this point in the history
Exceptions
  • Loading branch information
felipao-mx authored Jul 31, 2021
2 parents e6d9aab + 39dfd8c commit 1e5a20a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
4 changes: 2 additions & 2 deletions examples/resources/accounts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime as dt

from fastapi.responses import JSONResponse as Response

from fastapi import Request
from fast_agave.filters import generic_query
from ..models import Account as AccountModel
from ..validators import AccountQuery, AccountRequest, AccountUpdateRequest
Expand Down Expand Up @@ -33,7 +33,7 @@ async def update(
return Response(content=account.to_dict(), status_code=200)

@staticmethod
async def delete(account: AccountModel) -> Response:
async def delete(account: AccountModel, _: Request) -> Response:
account.deactivated_at = dt.datetime.utcnow().replace(microsecond=0)
await account.async_save()
return Response(content=account.to_dict(), status_code=200)
3 changes: 2 additions & 1 deletion fast_agave/blueprints/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def wrapper(func: Callable):
return func

for key, val in original_func.__dict__.items():
setattr(func, key, val)
if not key.startswith('_'):
setattr(func, key, val)

return func

Expand Down
15 changes: 10 additions & 5 deletions fast_agave/blueprints/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ def user_id_filter_required(self) -> bool:
async def retrieve_object(
self, resource_class: Any, resource_id: str
) -> Any:
resource_id = (
self.current_user_id if resource_id == 'me' else resource_id
)
query = Q(id=resource_id)
if self.user_id_filter_required():
if self.user_id_filter_required() and hasattr(
resource_class.model, 'user_id'
):
query = query & Q(user_id=self.current_user_id)
try:
data = await resource_class.model.objects.async_get(query)
except DoesNotExist:
raise NotFoundError
raise NotFoundError('Not valid id')
return data

def resource(self, path: str):
Expand Down Expand Up @@ -76,18 +81,18 @@ def wrapper_resource_class(cls):

@self.delete(path + '/{id}')
@copy_attributes(cls)
async def delete(id: str):
async def delete(id: str, request: Request):
obj = await self.retrieve_object(cls, id)
return await cls.delete(obj)
return await cls.delete(obj, request)

""" PATCH /resource/{id}
Enable PATCH method if Resource.update method exist. It validates
body data using `Resource.update_validator` but update logic is
completely your responsibility.
"""
if hasattr(cls, 'update'):
route = self.patch(path + '/{id}')

@self.patch(path + '/{id}')
@copy_attributes(cls)
async def update(id: str, request: Request):
params = await request.json()
Expand Down
22 changes: 19 additions & 3 deletions fast_agave/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@


@dataclass
class NotFoundError(HTTPException):
status_code: int = 404
detail: str = 'Not valid id'
class Error(HTTPException):
def __init__(self, detail=''):
super().__init__(status_code=self.status_code, detail=detail)


class BadRequestError(Error):
status_code = 400


class UnauthorizedError(Error):
status_code = 401


class ForbiddenError(Error):
status_code = 403


class NotFoundError(Error):
status_code = 404
2 changes: 1 addition & 1 deletion fast_agave/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.1'
__version__ = '0.0.2'

0 comments on commit 1e5a20a

Please sign in to comment.