Skip to content

Instantly share code, notes, and snippets.

View jfhbrook's full-sized avatar
💭
KNEE DEEP IN THE HOOPLA

Josh Holbrook jfhbrook

💭
KNEE DEEP IN THE HOOPLA
View GitHub Profile
@jfhbrook
jfhbrook / app-factory.py
Created April 26, 2019 16:16
Our kinja-service Flask app factory
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)
@jfhbrook
jfhbrook / kinja-api-model-example.scala
Last active April 26, 2019 16:02
A completely fake example of what a model might look like in one of our Scala API apps
case class Person(
id: Option[Id[Person]],
firstName: String,
lastName: String,
bio: String)
@jfhbrook
jfhbrook / kinja-api-response-example.json
Last active April 25, 2019 21:31
An example of what a Kinja API response might look like
{
"meta": {
"error": null,
"warnings": [],
},
"data": {
"firstName": "Josh",
"lastName": "Holbrook",
"bio": "Born in Alaska, lives in New York, has a pet budgie! Fun stuff!"
}
@jfhbrook
jfhbrook / kinja-api-model-example-attrs.py
Last active April 19, 2019 21:10
A completely fake example of what an attrs-based model might look like in a hypothetical python API app
@attr.s
class Person:
first_name = attr.ib()
last_name = attr.ib()
bio = attr.ib()
user_id = attr.ib()
@jfhbrook
jfhbrook / kinja-api-controller-async-example.py
Last active April 19, 2019 21:09
A completely fake example of what a controller might look like in a world where our python API apps used asyncio
@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,
@jfhbrook
jfhbrook / kinja-api-controller-example.scala
Last active April 19, 2019 21:09
A completely fake example of what a controller might look like in one of our Scala API apps
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
@jfhbrook
jfhbrook / kinja-service-http-error-handler.py
Created April 19, 2019 21:07
A snippet from the data team's API framework used to handle exceptions
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)
@jfhbrook
jfhbrook / kinja-service-to-json-helper.py
Created April 19, 2019 21:03
A snippet from the data team's API framework used to JSON-serialize results
# 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 = (', ', ': ')
@jfhbrook
jfhbrook / kinja-service-create-response-helper.py
Created April 19, 2019 21:02
A snippet from the data team's API framework used to generate responses
def create_response(
*,
error,
warnings,
data,
status,
headers
):
return current_app.response_class(
response=_to_json(
@jfhbrook
jfhbrook / kinja-service-success-helper.py
Created April 19, 2019 21:01
A snippet from the data team's API framework used to generate success responses
def success(
response,
*,
warnings=None,
status=200,
headers=None
):
warnings = warnings if warnings else get_warnings()
return create_response(