Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Django Rest Framework - Building a Web API

With use of Django and Django REST framework you can create Web APIs practice and quickly avoiding the manual handling of JSON messages.

  • Loggen Sie sich ein, um Kommentare anzuzeigen.

Django Rest Framework - Building a Web API

  1. 1. Marcos PereiraBuilding a web API with Django
  2. 2. Review •Retrieves a Resource •CacheableGET •Creates a new ResourcePOST •Updates an existing ResourcePUT •Removes a ResourceDELETE 2
  3. 3. Review u Client-Server u Stateless u Json, XML, etc. u HATEOAS (Hipemedia As The Engine Of Application State) 3
  4. 4. Alternatives ? u SOAP u WSDLs u WebSockets 4
  5. 5. What’s DRF? Django REST framework is a powerful and flexible toolkit for building Web APIs. 5
  6. 6. 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 packages for OAuth1a and OAuth2. u Serialization that supports both ORM and non-ORM data sources. u Customizable all the way down - just use regular function-based views if you don't need the more powerful features. u Extensive documentation, and great community support. u Used and trusted by large companies such as Mozilla and Eventbrite. 6
  7. 7. 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
  8. 8. 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
  9. 9. Quickstart u Installation: 9
  10. 10. Quickstart u Settings: 10
  11. 11. CoffeeManger u Frontend https://github.com/marcospereirampj/frontend-coffee- manager u Backend https://github.com/marcospereirampj/backend-coffee- manager 11
  12. 12. DRF - Serialization u Creating a Serializer class u Working with Serializers u serializers.Serializer u Using ModelSerializers Serializers ~ Django’s Forms 12
  13. 13. DRF - Requests and Responses u Request objects: u REST framework introduces a Request object that extends the regular HttpRequest, and provides more flexible request parsing. The core functionality of the Request object is the 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 content and uses content negotiation to determine the correct content 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 you get an error code wrong. REST framework provides more explicit identifiers for each status code, such as HTTP_400_BAD_REQUEST in the status module. It's a good idea to use these throughout rather than using numeric identifiers. 13
  14. 14. DRF - Requests and Responses u Wrapping API views u REST framework provides two wrappers you can use to write API views: u The @api_view decorator for working with function based views. u The APIView class for working with class based views. 14
  15. 15. DRF - Class Based Views u We can also write our API views using class based views, rather than function based views. As we'll see this is a powerful pattern that allows us to reuse common functionality, and helps us keep our code DRY (don't repeat yourself). u Writing our API using class based views u Using mixins u Using generic class based views 15
  16. 16. DRF - Relationships & Hyperlinked APIs u At the moment relationships within our API are represented by using primary keys. In this part of the tutorial we'll improve the cohesion and discoverability of our 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
  17. 17. DRF - ViewSets & Routers u REST framework includes an abstraction for dealing with ViewSets, that allows the developer to concentrate on modeling the state and interactions of the API, and leave the URL construction to be handled automatically, based on common conventions. u ViewSet classes are almost the same thing as View classes, except that they provide operations such as read, or update, and not method handlers such as get or put. u Using Routers u Binding ViewSets 17
  18. 18. DRF - Authentication & Permissions u Authenticating with the API u Adding required permissions to views u Custom Permissions 18
  19. 19. Questions & Discussion Marcos Pereira marcos.pereira@itec.al.gov.br marcospereira.mpj@gmail.com 19