Transcript

Marcos PereiraBuilding a web API with Django

Review

•Retrieves a Resource•CacheableGET•Creates a new ResourcePOST•Updates an existing ResourcePUT•Removes a ResourceDELETE

2

Review

u Client-Server

u Stateless

u Json, XML, etc.

u HATEOAS (Hipemedia As The Engine Of ApplicationState)

3

Alternatives ?

u SOAP

u WSDLs

u WebSockets

4

What’s DRF?

Django REST framework is a powerful andflexible toolkit for building Web APIs.

5

What’s DRF?

u Some reasons you might want to use REST framework:

u The Web browsable API is a huge usability win for your developers.

u Authentication policies including packagesfor OAuth1a and OAuth2.

u Serialization that supports both ORM and non-ORM data sources.

u Customizable all the way down - just use regular function-basedviews if you don't need the more powerful features.

u Extensive documentation, and great community support.

u Used and trusted by large companies suchas Mozilla and Eventbrite.

6

What’s DRF?

traditional

1. Models/Querysets

2. Class-Based Views/Mixins

3. Generic Views

4. URLs

5. HTTP Requests

6. Rendered Responses

Django Rest Framework

1. Serializers

2. APIViews/Mixins

3. ViewSets

4. Routers

5. HTTP Requests

6. HTTP Responses

7

Quickstart

u Requirements:u Python (2.7, 3.2, 3.3, 3.4, 3.5)

u Django (1.7+, 1.8, 1.9)

u Installation:

8

Quickstart

u Installation:

9

Quickstart

u Settings:

10

CoffeeManger

u Frontend

https://github.com/marcospereirampj/frontend-coffee-manager

u Backend

https://github.com/marcospereirampj/backend-coffee-manager

11

DRF - Serialization

u Creating a Serializer class

u Working with Serializers

u serializers.Serializer

u Using ModelSerializers

Serializers ~ Django’s Forms

12

DRF - Requests and Responsesu Request objects:

u REST framework introduces a Request object that extendsthe regular HttpRequest, and provides more flexible requestparsing. The core functionality of the Request object isthe request.data attribute, which is similar to request.POST, but more useful for working with Web APIs.

u Response objects:u REST framework also introduces a Response object, which is

a type of TemplateResponse that takes unrendered contentand uses content negotiation to determine the correctcontent type to return to the client.

u Status codes:u Using numeric HTTP status codes in your views doesn't always

make for obvious reading, and it's easy to not notice if youget an error code wrong. REST framework provides more explicit identifiers for each status code, suchas HTTP_400_BAD_REQUEST in the status module. It's a goodidea to use these throughout rather than using numericidentifiers.

13

DRF - Requests and Responses

u Wrapping API views

u REST framework provides two wrappers you can use towrite API views:

u The @api_view decorator for working with function basedviews.

u The APIView class for working with class based views.

14

DRF - Class Based Views

u We can also write our API views using class basedviews, rather than function based views. As we'll seethis is a powerful pattern that allows us to reuse common functionality, and helps us keep ourcode DRY (don't repeat yourself).

u Writing our API using class based views

u Using mixins

u Using generic class based views

15

DRF - Relationships & Hyperlinked APIs

u At the moment relationships within our API are represented by using primary keys. In this part of thetutorial we'll improve the cohesion and discoverability ofour API, by instead using hyperlinking for relationships.

u Using primary keys.

u Using hyperlinking between entities.

u Using a unique identifying slug field on the related entity.

u Using the default string representation of the related entity.

u Nesting the related entity inside the parent representation.

u Some other custom representation.

16

DRF - ViewSets & Routersu REST framework includes an abstraction for dealing

with ViewSets, that allows the developer toconcentrate on modeling the state and interactionsof the API, and leave the URL construction to behandled automatically, based on common conventions.

u ViewSet classes are almost the same thingas View classes, except that they provide operationssuch as read, or update, and not method handlerssuch as get or put.

u Using Routersu Binding ViewSets

17

DRF - Authentication & Permissions

u Authenticating with the API

u Adding required permissions to views

u Custom Permissions

18

Questions & Discussion

Marcos Pereira

[email protected]

[email protected]

19


Top Related