What ' s the RESTful
Restframework is a framework that can quickly provide us with API interfaces to facilitate our programming. The API is written by the backend programmer, in order for the front end to take the data of an interface, usually in the form of a URL.
Every project has the first person to do the infrastructure, this time is not just to implement an API is OK, need to consider more things, including
-
Unified exception Handling
API permissions
Uniform parameter Checking
How caching can be done more simply and uniformly
Certification
Unified Query Filtering
Code layering
Restframework can help us do these things very well.
Before we get to know restframework, we first need to know what rest is:
- Rest is a standard, restful is a specification, according to the requirements of the product needs to set a convenient front-end specification, unlike the Protocol (such as the HTTP protocol), not all standard requirements must be followed.
- Rest is not technology-related, it represents a software architecture style, rest is the abbreviation for representational state transfer, and Chinese translates as "representational states transfer"
- Rest looks at the entire network from the resource's perspective, which identifies the resources distributed across a node in the network through URLs, and the client application uses URLs to obtain representations of the resources, resulting in these applications transforming state
- All the data, but the data obtained through the network or operation (increase and deletion), are resources, all the data as a resource is the most essential attribute of rest difference and other architectural style
- For the resource-oriented architectural style of rest, a new architectural concept is proposed, namely: resource-oriented architecture (Roa:resource oriented Architecture)
API Design Specifications
First of all we need to know that the API is not necessary, if the entire project only you or a few people to complete, can be directly with the Django template engine rendering sent to the front-end directly after the operation. The use of API is mainly to solve the situation of multi-person development, especially the separation of front and back. Because the front-end personnel in the production page will inevitably need to back-end data, but if the front and back end is separate, you can not use Django render, most of the situation front end will use Ajax to send the request, the back-end people send a JSON string to the front end, the front-end and then use after deserialization. In fact, we design a set of APIs that will make our task a lot easier. As a result, Restframework was born.
The communication protocol between API and user is essentially the HTTPS protocol.
With the Restframework design API there is a set of specifications, that is, restful, and in order not to add to the annoyance of the people you develop with, we still have to follow these specifications. So what are the rules?
- Domain name : The domain name to show that you use the API, we have two ways
- Https://api.example.com: Deploy the API on a dedicated domain name (the recommended way to do this, but there are cross-domain issues)
- https://example.org/api/Way Two: Write on the path, the API is very simple
- version : Our project will be added and optimized in the development process, this time we usually set a version number for each version, the version number is displayed in two ways
- Https://api.example.com/v1/Way One: Written on the path, the API is simple
- https://v1.example.com mode Two: Deploy the version number on a dedicated domain name (there will also be cross-domain issues, which will cause multiple requests to be sent across domains)
- path : Depending on the network, everything is a resource, so paths are represented by nouns (plural)
- Https://api.example.com/v1/zoos
- Https://api.example.com/v1/animals
- Request Method :
- GET: Fetching resources from the server (one or more items)
- POST: Create a new resource on the server
- PUT: Update the resources on the server (the client provides the changed full resources-all modified)
- PATCH: Update resources on the server (client provides changed properties--partial modification)
- Delete: Remove resources from the server
- filtering : A GET request gets the corresponding data by uploading the parameter in the URL
- HTTPS://API.EXAMPLE.COM/V1/ZOOS?LIMIT=10: Specifies the number of returned data
- HTTPS://API.EXAMPLE.COM/V1/ZOOS?OFFSET=10: Specifies the starting position of the returned data
- HTTPS://API.EXAMPLE.COM/V1/ZOOS?PAGE=2&PER_PAGE=100: Specify the number of pages and the amount of data per page
- HTTPS://API.EXAMPLE.COM/V1/ZOOS?SORTBY=NAME&ORDER=ASC: Specifies which attribute the returned result is sorted by, and the sort order
- Https://api.example.com/v1/zoos?animal_type_id=1: Specifying filter criteria
- Status Code: We can use the status code to determine the status of the request in order to process the corresponding request. When the status code is 4, you should catch the corresponding error and return the error message
$ OK-[GET]: The server successfully returned the data requested by the user, the operation is idempotent (idempotent). 201 CREATED-[post/put/PATCH]: User new or modified data succeeded. 202 Accepted-[*]: Indicates that a request has entered the background queue (asynchronous task)204 NO CONTENT-[Delete]: The user deleted the data successfully. INVALID REQUEST-[post/put/PATCH]: The user made a request error, the server did not create new or modify the data operation, the operation is idempotent. 401 Unauthorized-[*]: Indicates that the user does not have permissions (token, user name, password error). 403 Forbidden-[*] Indicates that the user is authorized (relative to the 401 error), but access is forbidden. 404 Not FOUND-[*]: The user made a request for a nonexistent record, the server did not operate, the operation is idempotent. 406 Not acceptable-[GET]: User requested format is not available (such as user request JSON format, but only XML format). 410 Gone-[get]: The resource requested by the user is permanently deleted and will no longer be available. 422 unprocesable Entity-[post/put/PATCH] A validation error occurs when an object is created. INTERNAL SERVER ERROR-[*]: The server has an error and the user will not be able to determine if the request was successful. See more here: http:Www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Common status Code" Invalid API Key "} # error message because of using error as key
- return results : The results that the server returns to the user for different operations should conform to the following specifications.
- Get/collection: Returns a list of resource objects
- Get/collection/resource: Returns a single Resource object
- Post/collection: Returns the newly generated resource object
- Put/collection/resource: Returns the full resource object
- Patch/collection/resource: Returns the full resource object
- Delete/collection/resource: Returns an empty document
- hypermedia API: The RESTful API is best done by hypermedia, which provides links to the returned results, and even to other API methods, so that users do not look up documents and know what to do next.
{"Link": { "rel":"Collection Https://www.example.com/zoos", "href":"Https://api.example.com/zoos", "title":"List of Zoos", "type":"Application/vnd.yourformat+json"}}
RESTFRAMEWORK--API Design Code