Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building JSON APIs with Django / Pinax

Building JSON APIs with Django / Pinax

Brian Rosner

July 18, 2016
Tweet

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

  3. pinax-api

    View Slide

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

    View Slide

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

    View Slide

  6. Increasingly Common Modern Application Architecture

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  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",
    ]

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  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()

    View Slide

  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/)

    View Slide

  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

    View Slide