Django's RESTful specification

RESTful specification

First, what is RESTful

  • REST has nothing to do with technology, represent a style of software architecture, REST is Representational State Transfer short, the Chinese translation of "transfer of state characterized" as
  • REST look like from the perspective of resources throughout the network, which will be identified in the distribution of resources in the network of a node by its URL, the client application to obtain the resources characterized by URL, access to these applications characterize the resulting change the status of these
  • REST has nothing to do with technology, represent a style of software architecture, REST is Representational State Transfer short, the Chinese translation of "transfer of state characterized" as
  • All of the data, but was acquired by the network or the operation (CRUD) data, all resources, all data will be treated as REST resources are the most essential attribute different from other architectural styles
  • For this resource-oriented REST architectural style, it was suggested that a new structure concept, namely: resource-oriented architecture (ROA: Resource Oriented Architecture)

Two, RESTful API design

  • API and user protocol, always use

  • Domain Name: API will try to deploy a dedicated domain name (there will be cross-domain issue) ( https://api.example.com ) or ( https://example.org/api/ )

  • Path, everything is on the CTV network resources, are using the noun representation (may be complex)

    https://api.example.com/v1/zoos
    https://api.example.com/v1/animals
    https://api.example.com/v1/employees
  • method

    GET      :从服务器取出资源(一项或多项)
    POST     :在服务器新建一个资源
    PUT      :在服务器更新资源(客户端提供改变后的完整资源)
    PATCH    :在服务器更新资源(客户端提供改变的属性)
    DELETE   :从服务器删除资源
  • Filtered, passed through a search condition on the uploaded parameters form url

    https://api.example.com/v1/zoos?limit=10:指定返回记录的数量
    https://api.example.com/v1/zoos?offset=10:指定返回记录的开始位置
    https://api.example.com/v1/zoos?page=2&per_page=100:指定第几页,以及每页的记录数
    https://api.example.com/v1/zoos?sortby=name&order=asc:指定返回结果按照哪个属性排序,以及排序顺序
    https://api.example.com/v1/zoos?animal_type_id=1:指定筛选条件
  • status code

    200 OK - [GET]:服务器成功返回用户请求的数据,该操作是幂等的(Idempotent)。
    201 CREATED - [POST/PUT/PATCH]:用户新建或修改数据成功。
    202 Accepted - [*]:表示一个请求已经进入后台排队(异步任务)
    204 NO CONTENT - [DELETE]:用户删除数据成功。
    400 INVALID REQUEST - [POST/PUT/PATCH]:用户发出的请求有错误,服务器没有进行新建或修改数据的操作,该操作是幂等的。
    401 Unauthorized - [*]:表示用户没有权限(令牌、用户名、密码错误)。
    403 Forbidden - [*] 表示用户得到授权(与401错误相对),但是访问是被禁止的。
    404 NOT FOUND - [*]:用户发出的请求针对的是不存在的记录,服务器没有进行操作,该操作是幂等的。
    406 Not Acceptable - [GET]:用户请求的格式不可得(比如用户请求JSON格式,但是只有XML格式)。
    410 Gone -[GET]:用户请求的资源被永久删除,且不会再得到的。
    422 Unprocesable entity - [POST/PUT/PATCH] 当创建一个对象时,发生一个验证错误。
    500 INTERNAL SERVER ERROR - [*]:服务器发生错误,用户将无法判断发出的请求是否成功。
    
    更多看这里:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
    
  • Error handling, should return an error message, error as key

    {
        error: "Invalid API key"
    }
  • Return results to the user returns for different operations, the server gave should meet the following specifications

    GET /collection:返回资源对象的列表(数组)
    GET /collection/resource:返回单个资源对象
    POST /collection:返回新生成的资源对象
    PUT /collection/resource:返回完整的资源对象
    PATCH /collection/resource:返回完整的资源对象
    DELETE /collection/resource:返回一个空文档
  • Hypermedia API, RESTful API is best to do Hypermedia, ie, the returned results provide links, even to other API methods, so that the user does not check the documents, but also know how 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"
    }}

 Excerpt: http://www.ruanyifeng.com/blog/2014/05/restful_api.html

Third, based on django achieve

# 路由系统
urlpatterns = [
    url(r'^users/$', views.Users.as_view()),
    url(r'^users2/$', views.user2),
]
# 视图函数
import json

def  user2(request):
    if request.method=='GET':
        dic = {'status':200,'name': 'lqz2', 'age': 18}
        return HttpResponse(json.dumps(dic))
    elif request.method=='POST':
        dic = {'status': 200, 'msg': '修改成功'}
        return JsonResponse(dic)

class Users(View):
    def get(self, request):
        dic = {'status':200,'name': 'lqz', 'age': 18}
        return HttpResponse(json.dumps(dic))

    def post(self, request):
        dic = {'status': 200, 'msg': '修改成功'}
        return JsonResponse(dic)

Guess you like

Origin www.cnblogs.com/qianzhengkai/p/11115955.html