Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from raygun import json
from raygun.params import parse_str
class Account(mongoengine.Document, raygun.params.CustomKind):
""" Account object for database storage AND request body schema
"""
name = StringField(required=True)
@classmethod
def get_kind(cls):
return "object"
@classmethod
def get_format(cls):
return ""
@classmethod
async def serialize(cls, instance: Account) -> StrOrBytes:
return json.dumps(instance.to_dict())
@classmethod
async def deserialize(cls, request: Request, name: str, default: Optional[Any]=EMPTY) -> Account:
data: StrOrBytes = await parse_str(request, name, default)
return cls(**json.loads(data))
def to_dict(self) -> dict:
return {
"name": self.name,
}
@raygun.route("POST", "/person")
@raygun.params.form(
"account",
Account,
description="Name of person to create",
)
@raygun.params.header(
"X-Request-Id",
str,
description="Request ID that triggered creation",
format="uuid",
hidden=True,
)
async def do_create(request: Request, data: params.RequestData):
""" Create a new person
Form parameters are documented with OpenAPI Request Body _OR_ Reference Objects:
https://swagger.io/specification/#requestBodyObject
https://swagger.io/specification/#referenceObject
---
requestBody:
description: "Person to create"
content:
multipart/form-data:
examples:
john:
summary: An example for "john"
value: {name: john}
responses:
200:
description: Person object
content:
text/plain:
schema: {type: string}
example: {person: {name: "john"}}
"""
account: Account = await data.form.get(request, "account")
request_id: str = await data.headers.get(request, "x-request-id")
metrics.incr("create")
account.save()
return response.json({"person": account.to_dict()})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.