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
# Used to implement the same pretty-printing behavior as flask:jsonify | |
# Also calls cattr.unstructure on data | |
def _to_json(error, warnings, data): | |
indent = None | |
separators = (',', ':') | |
if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] or current_app.debug: | |
indent = 2 | |
separators = (', ', ': ') |
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
def create_response( | |
*, | |
error, | |
warnings, | |
data, | |
status, | |
headers | |
): | |
return current_app.response_class( | |
response=_to_json( |
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
def success( | |
response, | |
*, | |
warnings=None, | |
status=200, | |
headers=None | |
): | |
warnings = warnings if warnings else get_warnings() | |
return create_response( |
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
# In the default handler for flask, they don't allow None. We however | |
# allow anything that's json serializable. :) | |
if not isinstance(rv, self.response_class): | |
if isinstance(rv, (bytes, bytearray)): | |
# For now, treat bytes as raw data and don't attempt to | |
# JSON-serialize | |
rv = self.response_class(rv, status=status, headers=headers) | |
status = headers = None | |
# Try to coerce things that are Responses or wsgi callables already |
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
""" | |
:attr:`response_class`. | |
:param rv: the return value from the view function. The view function | |
must return a response. Returning ``None``, or the view ending | |
without returning, is not allowed. The following types are allowed | |
for ``view_rv``: | |
``str`` (``unicode`` in Python 2) | |
A response object is created with the string encoded to UTF-8 | |
as the body. | |
``bytes`` (``str`` in Python 2) |
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
import cattrs | |
from flask import Blueprint, request | |
from flask_sqlalchemy import SQLAlchemy | |
from kinja_service import create_app | |
# Our framework's app factory | |
app = create_app('my_app') | |
# This is what setting up sqlalchemy models looks like with Flask | |
db = SQLAlchemy(app) |
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
@attr.s | |
class Person: | |
first_name = attr.ib() | |
last_name = attr.ib() | |
bio = attr.ib() | |
user_id = attr.ib() |
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
@authenticated | |
async def create(request): | |
body = await request.json() | |
first_name = body['firstName'] | |
last_name = body['lastName'] | |
bio = body.get('bio', '') | |
saved = await person_logic.create( | |
first_name, |
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
case class Person( | |
id: Option[Id[Person]], | |
firstName: String, | |
lastName: String, | |
bio: String) |
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
def create: Action[JsValue] = Authenticated.json { request => | |
for { | |
firstName <- request.body.required[String]("firstName").toAcc | |
lastName <- request.body.required[String]("lastName").toAcc | |
bio <- request.body.optional[String]("bio").map(_.getOrElse("")).toAcc | |
saved <- personLogic.create(firstName, lastName, bio, request.userId) | |
} yield saved | |
}.withNoCaching |