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

REST API's with Django and Django Rest Framework

REST API's with Django and Django Rest Framework

This is the first part of my presentations about RESTfull API with Django and Django Rest Framework. In this presentation, I talk about the REST API architectural model and its Constraints to compose a RESTFull API.

Avatar for Vicente Marçal

Vicente Marçal

April 11, 2018
Tweet

Other Decks in Programming

Transcript

  1. REST API'S WITH REST API'S WITH REST API'S WITH REST

    API'S WITH REST API'S WITH REST API'S WITH DJANGO AND DJANGO DJANGO AND DJANGO DJANGO AND DJANGO DJANGO AND DJANGO DJANGO AND DJANGO DJANGO AND DJANGO REST FRAMEWORK REST FRAMEWORK REST FRAMEWORK REST FRAMEWORK REST FRAMEWORK REST FRAMEWORK A POWERFUL WAY TO DO REST API'S Navigate : Space / Arrow Keys | - Menu | - Fullscreen | - Overview | - Blackout | - Speaker | - Help M F O B S ?  1 / 27
  2. INTRODUCTION What's a Rest? The Constraints The foundations of REST

    REST vs. SOAP The best practices of HTTP that guide REST Some practices to use our resources References RESTFull API's with Django and Django Rest Framework  2 / 27
  3. WHAT'S A REST? It had its origin in Roy Fielding'

    PhD Thesis Roy Fielding was one of the authors of HTTP protocol REST means REpresentational State Transfer REST or RESTFul?? Roy proposed some constraints to de ne the REST architecture RESTFull API's with Django and Django Rest Framework  3 / 27
  4. THE CONSTRAINTS Client-Server The main purpose of this constraint is

    to separate the responsibilities of each part of the application Servers are not concerned with the user interface or user state so that servers can be simpler and more scalable. Servers and clients may also be replaced and developed independently, as long as the interface is not altered. RESTFull API's with Django and Django Rest Framework  4 / 27
  5. THE CONSTRAINTS Stateless Essentially, what this means is that the

    necessary state to handle the request is contained within the request itself, whether as part of the URI, query- string parameters, body, or headers. In REST, the client must include all information for the server to ful ll the request, resending state as necessary if that state must span multiple requests. RESTFull API's with Django and Django Rest Framework  5 / 27
  6. THE CONSTRAINTS Cacheable As on the World Wide Web, clients

    can cache responses. Responses must therefore, implicitly or explicitly, de ne themselves as cacheable, or not, to prevent clients reusing stale or inappropriate data in response to further requests. Well- managed caching partially or completely eliminates some client–server interactions, further improving scalability and performance. RESTFull API's with Django and Django Rest Framework  6 / 27
  7. THE CONSTRAINTS Uniform Interface The uniform interface constraint de nes

    the interface between clients and servers. It simpli es and decouples the architecture, which enables each part to evolve independently. The four guiding principles of the uniform interface are: Resource-Based: Individual resources are identi ed in requests using URIs as resource identi ers. RESTFull API's with Django and Django Rest Framework  7 / 27
  8. THE CONSTRAINTS Uniform Interface four guiding principles (continuation) Manipulation of

    Resources Through Representations: When a client holds a representation of a resource it has enough information to modify or delete the resource on the server, provided it has permission to do so. Self-descriptive Messages: Each message includes enough information to describe how to process the message. RESTFull API's with Django and Django Rest Framework  8 / 27
  9. THE CONSTRAINTS Uniform Interface four guiding principles (continuation) Hypermedia as

    the Engine of Application State (HATEOAS): Clients deliver state via body contents, query-string parameters, request headers and the requested URI (the resource name). Services deliver state to clients via body content, response codes, and response headers. This is technically referred-to as hypermedia. RESTFull API's with Django and Django Rest Framework  9 / 27
  10. THE CONSTRAINTS Layered System A client cannot ordinarily tell whether

    it is connected directly to the end server, or to an intermediary along the way. Intermediary servers may improve system scalability by enabling load- balancing and by providing shared caches. Layers may also enforce security policies. RESTFull API's with Django and Django Rest Framework  10 / 27
  11. THE CONSTRAINTS Code on Demand (optional) Servers are able to

    temporarily extend or customize the functionality of a client by transferring logic to it that it can execute. Examples of this may include compiled components such as Java applets and client-side scripts such as JavaScript. RESTFull API's with Django and Django Rest Framework  11 / 27
  12. THE FOUNDATIONS OF REST The rst concept fundamental in REST

    is the resource Basically, the resource is the most important concept that we need to understand; The resource is the set of data that traf cs by the protocol; The resources de ne how our URLs will be described. RESTFull API's with Django and Django Rest Framework  12 / 27
  13. THE FOUNDATIONS OF REST The second concept fundamental in REST

    is the representation The representation is the form that the information is exchanged between client and server; The representation can be a JSON, XML, HTML, JPG, PNG, MP3, MP4 etc; RESTFull API's with Django and Django Rest Framework  13 / 27
  14. REST VS. SOAP REST is an architecture model. SOAP is

    a protocol. REST SOAP architecture model Protocol Simple HTTP Request Invokes services by calling RPC method Support many types like XML, JSON, and YAML Support only XML RESTFull API's with Django and Django Rest Framework  14 / 27
  15. THE BEST PRACTICES OF HTTP THAT GUIDE REST Appropriate use

    of HTTP methods; GET, POST, PUT, PUSH and DELETE (but not only!!) Appropriate use of URLs; Appropriate use of HTTP status code to represent success or fails; The families: 100, 200, 300, 400 and 500 Appropriate use of HTTP headers; The possibility to link various different resources. RESTFull API's with Django and Django Rest Framework  15 / 27
  16. SOME PRACTICES TO USE OUR RESOURCES - 1: 1 Use

    nouns, not verbs The resources are a set of data about somewhere or something, then use nouns not verbs, e.g. as follow Resource GET read POST create PUT update DELETE /cars Returns a list of cars Create a new car Bulk update of cars Delete all cars /cars/711 Returns a speci c car Method not allowed (405) Updates a speci c car Deletes a speci c car RESTFull API's with Django and Django Rest Framework  16 / 27
  17. SOME PRACTICES TO USE OUR RESOURCES - 2A: 2 The

    HTTP methods The best practice in REST is the appropriate use of HTTP methods: GET, POST, PUT, PUSH and DELETE GET: This method is used to return a list of all objects of our resource or all data about a speci c object of our resource, to return a speci c object we use the ID of it. POST: This method is used to create a new instance of an object into our resource. RESTFull API's with Django and Django Rest Framework  17 / 27
  18. SOME PRACTICES TO USE OUR RESOURCES - 2B: 2 The

    HTTP methods PUT: This method is used to update all data of a speci c object of our resource. PUSH: This method is similar to PUT, but it can update a partial data of a speci c object of our resource. DELETE: Well, with this method we can delete all objects of our resource, if we call it into a root of our resource, or we can delete a speci c object of our resource with its ID. RESTFull API's with Django and Django Rest Framework  18 / 27
  19. SOME PRACTICES TO USE OUR RESOURCES - 3: 3 GET

    method and query parameters should not alter the state of our resources To alter the state of our resources we shold use the POST, PUT, PUSH or DELETE methods; The common error is use something like: GET /cars/711?activate GET /cars/711/activate That is not a good practice, the GET method is used to return a list of all objects or a speci c object with its ID RESTFull API's with Django and Django Rest Framework  19 / 27
  20. SOME PRACTICES TO USE OUR RESOURCES - 4A: 4 Use

    HTTP status code The HTTP standard provides over 70 status codes to describe the return values. We don’t need them all, but there should be used at least a mount of 10: Family 200: 200 – OK – Eyerything is working 201 – OK – New resource has been created 204 – OK – The resource was successfully deleted RESTFull API's with Django and Django Rest Framework  20 / 27
  21. SOME PRACTICES TO USE OUR RESOURCES - 4B: 4 Use

    HTTP status code Family 300: 304 – Not Modi ed – The client can use cached data Family 400: 400 – Bad Request – The request was invalid or cannot be served. The exact error should be explained in the error payload. E.g. "The JSON is not valid" RESTFull API's with Django and Django Rest Framework  21 / 27
  22. SOME PRACTICES TO USE OUR RESOURCES - 4C: 4 Use

    HTTP status code Family 400: 401 – Unauthorized – The request requires an user authentication 403 – Forbidden – The server understood the request, but is refusing it or the access is not allowed. 404 – Not found – There is no resource behind the URI. RESTFull API's with Django and Django Rest Framework  22 / 27
  23. SOME PRACTICES TO USE OUR RESOURCES - 4D: 4 Use

    HTTP status code Family 400: 405 – Method not allowed – Indicates that the request method is known by the server but has been disabled and cannot be used. 422 – Unprocessable Entity – Should be used if the server cannot process the enitity, e.g. if an image cannot be formatted or mandatory elds are missing in the payload. RESTFull API's with Django and Django Rest Framework  23 / 27
  24. SOME PRACTICES TO USE OUR RESOURCES - 4E: 4 Use

    HTTP status code Family 500: 500 – Internal Server Error – API developers should avoid this error. If an error occurs in the global catch blog, the stracktrace should be logged and not returned as response. RESTFull API's with Django and Django Rest Framework  24 / 27
  25. REFERENCES Fielding, Roy Thomas. . In: Architectural Styles and the

    Design of Network-based Software Architectures. Doctoral dissertation, University of California, Irvine, 2000. Mozilla Developer Network - Representational State Transfer (REST) HTTP response status codes REST: Construa API's inteligentes de maneira simples RESTful Web APIs: Services for a Changing World RESTFull API's with Django and Django Rest Framework  25 / 27
  26. REFERENCES Django RESTful Web Services: The easiest way to build

    Python RESTful APIs and web services with Django http://www.restapitutorial.com Entendendo e documentando REST / RESTful APIs RESTFull API's with Django and Django Rest Framework  26 / 27
  27. THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU!

    THANK YOU! RESTFull API's with Django and Django Rest Framework  27 / 27