SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
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 Application
State)
3
Alternatives ?
u SOAP
u WSDLs
u WebSockets
4
What’s DRF?
Django REST framework is a powerful and
flexible 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 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
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 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
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
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
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
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
DRF - Authentication & Permissions
u Authenticating with the API
u Adding required permissions to views
u Custom Permissions
18
Questions & Discussion
Marcos Pereira
marcos.pereira@itec.al.gov.br
marcospereira.mpj@gmail.com
19

Mais conteúdo relacionado

Mais procurados

Introduction to django
Introduction to djangoIntroduction to django
Introduction to django
Ilian Iliev
 

Mais procurados (20)

Django PPT.pptx
Django PPT.pptxDjango PPT.pptx
Django PPT.pptx
 
REST Easy with Django-Rest-Framework
REST Easy with Django-Rest-FrameworkREST Easy with Django-Rest-Framework
REST Easy with Django-Rest-Framework
 
Swagger
SwaggerSwagger
Swagger
 
Laravel introduction
Laravel introductionLaravel introduction
Laravel introduction
 
Angular 4 and TypeScript
Angular 4 and TypeScriptAngular 4 and TypeScript
Angular 4 and TypeScript
 
React workshop presentation
React workshop presentationReact workshop presentation
React workshop presentation
 
Getting Started with NgRx (Redux) Angular
Getting Started with NgRx (Redux) AngularGetting Started with NgRx (Redux) Angular
Getting Started with NgRx (Redux) Angular
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
Spring boot
Spring bootSpring boot
Spring boot
 
Introduction to django
Introduction to djangoIntroduction to django
Introduction to django
 
Expressjs
ExpressjsExpressjs
Expressjs
 
Introduction Node.js
Introduction Node.jsIntroduction Node.js
Introduction Node.js
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
 
Angular state Management-NgRx
Angular state Management-NgRxAngular state Management-NgRx
Angular state Management-NgRx
 
An Introduction To REST API
An Introduction To REST APIAn Introduction To REST API
An Introduction To REST API
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Web application development with Django framework
Web application development with Django frameworkWeb application development with Django framework
Web application development with Django framework
 
NextJS, A JavaScript Framework for building next generation SPA
NextJS, A JavaScript Framework for building next generation SPA  NextJS, A JavaScript Framework for building next generation SPA
NextJS, A JavaScript Framework for building next generation SPA
 
Express JS Rest API Tutorial
Express JS Rest API TutorialExpress JS Rest API Tutorial
Express JS Rest API Tutorial
 

Destaque (7)

Django admin
Django adminDjango admin
Django admin
 
Django rest framework tips and tricks
Django rest framework   tips and tricksDjango rest framework   tips and tricks
Django rest framework tips and tricks
 
Boost your django admin with Grappelli
Boost your django admin with GrappelliBoost your django admin with Grappelli
Boost your django admin with Grappelli
 
Django REST Framework
Django REST FrameworkDjango REST Framework
Django REST Framework
 
Customizing the Django Admin
Customizing the Django AdminCustomizing the Django Admin
Customizing the Django Admin
 
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
 
Create responsive websites with Django, REST and AngularJS
Create responsive websites with Django, REST and AngularJSCreate responsive websites with Django, REST and AngularJS
Create responsive websites with Django, REST and AngularJS
 

Semelhante a Django Rest Framework - Building a Web API

Creating Web Services with Zend Framework - Matthew Turland
Creating Web Services with Zend Framework - Matthew TurlandCreating Web Services with Zend Framework - Matthew Turland
Creating Web Services with Zend Framework - Matthew Turland
Matthew Turland
 

Semelhante a Django Rest Framework - Building a Web API (20)

Rest api best practices – comprehensive handbook
Rest api best practices – comprehensive handbookRest api best practices – comprehensive handbook
Rest api best practices – comprehensive handbook
 
REST full API Design
REST full API DesignREST full API Design
REST full API Design
 
Rest web service
Rest web serviceRest web service
Rest web service
 
Best practices and advantages of REST APIs
Best practices and advantages of REST APIsBest practices and advantages of REST APIs
Best practices and advantages of REST APIs
 
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Enterprise Level Application Architecture with Web APIs using Entity Framewor...Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
 
Introduction To Angular.js - SpringPeople
Introduction To Angular.js - SpringPeopleIntroduction To Angular.js - SpringPeople
Introduction To Angular.js - SpringPeople
 
Api design part 1
Api design part 1Api design part 1
Api design part 1
 
Top 10 Javascript Frameworks For Easy Web Development
Top 10 Javascript Frameworks For Easy Web DevelopmentTop 10 Javascript Frameworks For Easy Web Development
Top 10 Javascript Frameworks For Easy Web Development
 
What is the Role of Laravel in API Development?
What is the Role of Laravel in API Development?What is the Role of Laravel in API Development?
What is the Role of Laravel in API Development?
 
Mvc
MvcMvc
Mvc
 
IBM Integration Bus and REST APIs - Sanjay Nagchowdhury
IBM Integration Bus and REST APIs - Sanjay NagchowdhuryIBM Integration Bus and REST APIs - Sanjay Nagchowdhury
IBM Integration Bus and REST APIs - Sanjay Nagchowdhury
 
REST and RESTful Services
REST and RESTful ServicesREST and RESTful Services
REST and RESTful Services
 
Mule soft meetup_4_mty_online_oct_2020
Mule soft meetup_4_mty_online_oct_2020Mule soft meetup_4_mty_online_oct_2020
Mule soft meetup_4_mty_online_oct_2020
 
Laravel overview
Laravel overviewLaravel overview
Laravel overview
 
Code igniter - A brief introduction
Code igniter - A brief introductionCode igniter - A brief introduction
Code igniter - A brief introduction
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
 
Restful web services by Sreeni Inturi
Restful web services by Sreeni InturiRestful web services by Sreeni Inturi
Restful web services by Sreeni Inturi
 
Creating Web Services with Zend Framework - Matthew Turland
Creating Web Services with Zend Framework - Matthew TurlandCreating Web Services with Zend Framework - Matthew Turland
Creating Web Services with Zend Framework - Matthew Turland
 
Spring MVC framework features and concepts
Spring MVC framework features and conceptsSpring MVC framework features and concepts
Spring MVC framework features and concepts
 
Fundamental Essentials for API Design
Fundamental Essentials for API DesignFundamental Essentials for API Design
Fundamental Essentials for API Design
 

Último

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Último (20)

Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 

Django Rest Framework - Building a Web API

  • 1. Marcos PereiraBuilding a web API with Django
  • 2. Review •Retrieves a Resource •CacheableGET •Creates a new ResourcePOST •Updates an existing ResourcePUT •Removes a ResourceDELETE 2
  • 3. Review u Client-Server u Stateless u Json, XML, etc. u HATEOAS (Hipemedia As The Engine Of Application State) 3
  • 4. Alternatives ? u SOAP u WSDLs u WebSockets 4
  • 5. What’s DRF? Django REST framework is a powerful and flexible toolkit for building Web APIs. 5
  • 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. 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. 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
  • 12. DRF - Serialization u Creating a Serializer class u Working with Serializers u serializers.Serializer u Using ModelSerializers Serializers ~ Django’s Forms 12
  • 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. 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. 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. 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. 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. DRF - Authentication & Permissions u Authenticating with the API u Adding required permissions to views u Custom Permissions 18
  • 19. Questions & Discussion Marcos Pereira marcos.pereira@itec.al.gov.br marcospereira.mpj@gmail.com 19