Skip to content

Commit

Permalink
exclude query if is not required
Browse files Browse the repository at this point in the history
  • Loading branch information
rogelioLpz committed May 30, 2022
1 parent 97b9898 commit 1b6a9c3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
7 changes: 7 additions & 0 deletions examples/resources/transactions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from fastapi import Request
from fastapi.responses import JSONResponse as Response

from ..models.transactions import Transaction as TransactionModel
from .base import app


@app.resource('/transactions')
class Transaction:
model = TransactionModel

@staticmethod
async def create(request: Request) -> Response:
return Response(content={}, status_code=201)
1 change: 0 additions & 1 deletion examples/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from pydantic import BaseModel
import datetime as dt

from pydantic.fields import Field
from pydantic.main import BaseConfig


Expand Down
38 changes: 17 additions & 21 deletions fast_agave/blueprints/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@
from urllib.parse import urlencode

from cuenca_validations.types import QueryParams
from fastapi import (
APIRouter,
BackgroundTasks,
Depends,
HTTPException,
Request,
status,
)
from fastapi import APIRouter, BackgroundTasks, Depends, Request, status
from fastapi.responses import JSONResponse as Response
from fastapi.responses import StreamingResponse
from mongoengine import DoesNotExist, Q
Expand Down Expand Up @@ -97,6 +90,7 @@ def resource(self, path: str):
@app.resource('/my_resource')
class Items(Resource):
model = MyMongoModel
response_model = MyPydanticModel (Resource Interface)
query_validator = MyPydanticModel
def create(): ...
Expand Down Expand Up @@ -311,14 +305,6 @@ class QueryResponse(BaseModel):
},
]

@self.get(
path,
summary=f'Query {cls.__name__}',
response_model=QueryResponse,
description=query_description,
responses=get_response(200, 'Successful Response', examples),
include_in_schema=include_in_schema,
)
@copy_attributes(cls)
async def query(query_params: query_validator = Depends()):
"""GET /resource
Expand All @@ -340,11 +326,6 @@ async def query(query_params: query_validator = Depends()):
next_page = <url_for_next_items>
}
"""
if not hasattr(cls, 'query_validator') or not hasattr(
cls, 'get_query_filter'
):
raise HTTPException(405)

if self.platform_id_filter_required() and hasattr(
cls.model, 'platform_id'
):
Expand Down Expand Up @@ -404,6 +385,21 @@ async def _all(query: QueryParams, filters: Q, resource_path: str):
next_page_uri = f'{resource_path}?{urlencode(params)}'
return dict(items=item_dicts, next_page_uri=next_page_uri)

if hasattr(cls, 'query_validator') and hasattr(
cls, 'get_query_filter'
):
route = self.get(
path,
summary=f'Query {cls.__name__}',
response_model=QueryResponse,
description=query_description,
responses=get_response(
200, 'Successful Response', examples
),
include_in_schema=include_in_schema,
)
route(query)

return cls

return wrapper_resource_class
2 changes: 1 addition & 1 deletion fast_agave/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.6.0.dev2'
__version__ = '0.6.0.dev3'
4 changes: 2 additions & 2 deletions tests/blueprint/test_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,13 @@ def test_cannot_query_resource(client: TestClient) -> None:


def test_cannot_create_resource(client: TestClient) -> None:
response = client.post('/transactions', json=dict())
response = client.post('/billers', json=dict())
assert response.status_code == 405
assert response.json() == dict(error='Method Not Allowed')


def test_cannot_update_resource(client: TestClient) -> None:
response = client.patch('/transactions', json=dict())
response = client.patch('/transactions/123', json=dict())
assert response.status_code == 405
assert response.json() == dict(error='Method Not Allowed')

Expand Down

0 comments on commit 1b6a9c3

Please sign in to comment.