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_app(service_name): | |
app = KinjaApp(__name__) | |
app.config['KINJA_SERVICE_NAME'] = service_name | |
app.register_error_handler(Exception, general_error_handler) | |
app.register_error_handler(HTTPException, http_error_handler) | |
app.register_error_handler(Invalid, validation_error_handler) | |
init_ops(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
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
{ | |
"meta": { | |
"error": null, | |
"warnings": [], | |
}, | |
"data": { | |
"firstName": "Josh", | |
"lastName": "Holbrook", | |
"bio": "Born in Alaska, lives in New York, has a pet budgie! Fun stuff!" | |
} |
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
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 |
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 http_error_handler(exc): | |
""" | |
error handler intended for werkzeug HTTPException instances | |
""" | |
uid = log_and_generate_uid(exc) | |
code = kinja_error_code_from_exc(exc) | |
return error(exc, exc_code=code, uid=uid) |
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( |