Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import falcon
from awokado.consts import CREATE, READ
from awokado.meta import ResourceMeta
from awokado.resource import BaseResource
class HealthCheckResource(BaseResource):
Meta = ResourceMeta(name="healthcheck", methods=(READ, CREATE))
def on_get(
self,
req: falcon.request.Request,
resp: falcon.response.Response,
resource_id: int = None,
):
resp.body = "OK"
def on_post(
self, req: falcon.request.Request, resp: falcon.response.Response
):
raise Exception("Something Goes Wrong")
import tests.test_app.models as m
from awokado import custom_fields
from awokado.consts import CREATE, READ, BULK_CREATE
from awokado.meta import ResourceMeta
from awokado.request import ReadContext
from awokado.resource import BaseResource
STORE_OPEN = "open"
STORE_CLOSED = "closed"
class StoreResource(BaseResource):
Meta = ResourceMeta(
model=m.Store,
name="store",
methods=(CREATE, BULK_CREATE, READ),
select_from=sa.outerjoin(
m.Store, m.Book, m.Store.id == m.Book.store_id
),
)
id = fields.Int(model_field=m.Store.id)
book_ids = custom_fields.ToMany(
fields.Int(), resource="book", model_field=m.Book.id
)
name = fields.String(model_field=m.Store.name, required=True)
status = custom_fields.Choice(
model_field=m.Store.status,
allowed_values=[STORE_OPEN, STORE_CLOSED],
allow_none=True,
)
def validate_create_request(self, req: falcon.Request, is_bulk=False):
methods = self.Meta.methods
payload = json.load(req.bounded_stream)
if isinstance(payload.get(self.Meta.name), list):
request_method = BULK_CREATE
is_bulk = True
else:
request_method = CREATE
if request_method not in methods:
raise MethodNotAllowed()
data = payload.get(self.Meta.name)
if not data:
raise BadRequest(
f"Invalid schema, resource name is missing at the top level. "
f"Your POST request has to look like: "
f'{{"{self.Meta.name}": [{{"field_name": "field_value"}}] '
f'or {{"field_name": "field_value"}} }}'
)
try:
deserialized = self.load(data, many=is_bulk)