Building JSON APIs with Django / Pinax

Building JSON APIs with Django / Pinax

1dff3a95bb7767916a64443fba9654dc?s=128

Brian Rosner

July 18, 2016
Tweet

Transcript

  1. 2.

    About Me • Brian Rosner • Denver, Colorado • Chief

    Architect at Eldarion, Inc. • Pinax (http://pinaxproject.com) • Kel (http://www.kelproject.com)
  2. 4.

    What is Pinax? • Standard project layout • Starter projects

    • Reusable apps • Default templates http://pinaxproject.com
  3. 5.

    Why Build Another API Framework? • Requirements from Eldarion development

    client project • JSON:API • django-rest-framework? • Must admit a little NIH
  4. 7.

    • 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
  5. 10.

    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", ]
  6. 11.

    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") }
  7. 12.

    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
  8. 13.

    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+" } ) ...
  9. 14.

    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()
  10. 15.

    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/)
  11. 16.

    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