Slide 1

Slide 1 text

Creating a RESTful Web API in Django Ngazetungue Muheue [email protected] @muheuenga

Slide 2

Slide 2 text

ME Ø Python Software Foundation fellow Ø Python Society of Namibia co-founder Ø PyCon Namibia organiser Ø I work with Python and Django

Slide 3

Slide 3 text

RESTFUL WEB API WITH DJANGO

Slide 4

Slide 4 text

What does “API” mean? Ø Application Programming Interface Ø Code that allows two software programs to communicate with each other Ø It helps in connecting various software components Ø An API make it easier for developers to use certain technologies in building application using predefined operations.

Slide 5

Slide 5 text

What is a REST API? Ø REST is an acronym for Representational State Transfer Ø When Services: use REST architecture , they are called RESTful APIs Ø Set of web address respond with pure information, not formatted web page Ø It returns JSON Ø Surrounded by quotation marks, [],{} and descriptive title for each bit of information GET - Retrieve a resource based on given conditions. POST - create a resource. PUT - update a resource with the given updated attributes. DELETE - delete a resource. When?

Slide 6

Slide 6 text

How API work? PyCon Namibia Restaurant Client Chef and Kitchen Response Request Waiter API

Slide 7

Slide 7 text

GET request from GitHub's API curl https://api.github.com/users/Ngazetungue

Slide 8

Slide 8 text

GET request from GitHub's API Ø Many packages or many way Ø Django Rest framework Ø Sit on top of existing Django code

Slide 9

Slide 9 text

Building an “API” Ø Many packages or many way Ø Django Rest framework Ø Sit on top of existing Django code

Slide 10

Slide 10 text

DJANGO REST FRAMEWORK

Slide 11

Slide 11 text

Django Rest Framework Ø DRF is a Third Party Application that we will use in conjunction with our own application to create a REST API. Ø It provides us with many features that connect deeply with Django’s existing structures Ø Help us to create RESTful HTTP resources that correspond to the models we create Ø Sit on top of existing Django code

Slide 12

Slide 12 text

Anatomy of Django Rest framework?

Slide 13

Slide 13 text

FREE TICKETS API

Slide 14

Slide 14 text

Building the model

Slide 15

Slide 15 text

Building the Serializer Ø Serializers allow object state to be translated into a format that can be stored or transmitted and be reconstructed later on. Ø The serializers in REST framework work very similarly to Django’s Form and ModelForm classes. Ø Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes Ø Easily rendered into JSON, XML or other content types

Slide 16

Slide 16 text

Building the Viewset

Slide 17

Slide 17 text

Building URLS with the Router

Slide 18

Slide 18 text

Accessing the API Operation HTTP Method ØCreate POST ØRead GET ØUpdate PUT, PATCH ØDelete DELETE

Slide 19

Slide 19 text

Server / Admin

Slide 20

Slide 20 text

API - JSON Operation HTTP Method ØCreate POST ØRead GET ØUpdate PUT, PATCH ØDelete DELETE

Slide 21

Slide 21 text

Protecting our data Ø Read only Viewsets Ø Authentication Ø Restricting specific actions

Slide 22

Slide 22 text

Always visit Ø https://www.django-rest-framework.org/ Ø https://www.djangoproject.com/ Ø https://www.python.org/ RESOURCES Source code – Ticket Api Ø https://github.com/Ngazetungue/restticketapi My slide: https://speakerdeck.com/muheuenga20/djangowebapi

Slide 23

Slide 23 text

Thanks