Last active
April 9, 2019 18:50
-
-
Save pirogoeth/e1568b0e1ce9b17255afcdc45d407b63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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