Slide 1

Slide 1 text

Building JSON APIs with Django / Pinax Building Django Backends for the Modern World

Slide 2

Slide 2 text

About Me ● Brian Rosner ● Denver, Colorado ● Chief Architect at Eldarion, Inc. ● Pinax (http://pinaxproject.com) ● Kel (http://www.kelproject.com)

Slide 3

Slide 3 text

pinax-api

Slide 4

Slide 4 text

What is Pinax? ● Standard project layout ● Starter projects ● Reusable apps ● Default templates http://pinaxproject.com

Slide 5

Slide 5 text

Why Build Another API Framework? ● Requirements from Eldarion development client project ● JSON:API ● django-rest-framework? ● Must admit a little NIH

Slide 6

Slide 6 text

Increasingly Common Modern Application Architecture

Slide 7

Slide 7 text

● http://jsonapi.org/ ● Clean object representation ● All objects are uniquely identified for easy linkage ● Supports relationships and compound documents for excellent object graph serialization ● Registered with IANA as application/vnd.api+json JSON:API

Slide 8

Slide 8 text

Design Influences ● Kubernetes ● Django ● django-rest-framework ● Pinax

Slide 9

Slide 9 text

API Primitives (pinax.api) ● api.Resource ● api.Relationship ● api.EndpointSet

Slide 10

Slide 10 text

api.Resource ● Think a Django form without validation ● Completely agnostic to where the data is sourced ● Helpers used in endpoints to link to data sets from pinax import api class AuthorResource(api.Resource): api_type = "author" model = Author attributes = [ "name", ]

Slide 11

Slide 11 text

api.Relationship ● Define how resources link to each other ● Either a non-collection (many to one) or collection (many to many) from pinax import api @api.register class AuthorResource(api.Resource): api_type = "author" model = Author attributes = [ "name", ] relationships = { "posts": api.Relationship("post", collection=True), "publisher": api.Relationship("publisher") }

Slide 12

Slide 12 text

api.EndpointSet ● Binds resources to HTTP methods ● api.ResourceEndpointSet and api.RelationshipEndpointSet ● Automatically generates URL patterns ● Provides simple validation primitive to connect data to a Resource

Slide 13

Slide 13 text

from pinax import api from .resources import AuthorResource @api.bind(resource=AuthorResource) class AuthorEndpointSet(api.ResourceEndpointSet): url = api.url( base_name="author", base_regex=r"authors", lookup={ "field": "pk", "regex": r"\d+" } ) ...

Slide 14

Slide 14 text

def list(self, request): return self.render(self.resource_class.from_queryset(self.get_queryset())) def retrieve(self, request, pk): resource = self.resource_class(self.author) return self.render(resource) def create(self, request): with self.validate(self.resource_class) as resource: resource.save() return self.render_create(resource) def update(self, request, pk): with self.validate(self.resource_class, obj=self.author) as resource: resource.save() return self.render(resource) def destroy(self, request, pk): self.author.delete() return self.render_delete()

Slide 15

Slide 15 text

Other pinax-api Features ● Authentication (are you who you say you are?) ● Permissions (can you access a given resource?) ● API Blueprint Documentation Generator (https://apiblueprint.org/)

Slide 16

Slide 16 text

Thank You! ● https://github.com/pinax/pinax-api ● Brian Rosner ● https://brosner.com ● Eldarion, Inc. ● Pinax (Github, Youtube, Slack and Twitter) ● https://twitter.com/brosner