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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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