一、Restful API设计
API与用户的通讯协议总是使用HTTP协议。
域名
https://api.example.com 尽量将API部署在专用域名(会存在跨域问题) https://example.org/api/ API很简单
版本
URL,如:https://api.example.com/v1/ 请求头 跨域时,引发发送多次请求
路径
面向资源编程,使用名词表示 https://api.example.com/v1/zoos https://api.example.com/v1/animals https://api.example.com/v1/employees
method
GET :从服务器取出资源(一项或多项) POST :在服务器新建一个资源 PUT :在服务器更新资源(客户端提供改变后的完整资源) PATCH :在服务器更新资源(客户端提供改变的属性) DELETE :从服务器删除资源
过滤
通过在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:指定第几页
状态码
OK - [GET]:服务器成功返回用户请求的数据,该操作是幂等的(Idempotent)。 CREATED - [POST/PUT/PATCH]:用户新建或修改数据成功。 Accepted - [*]:表示一个请求已经进入后台排队(异步任务) NO CONTENT - [DELETE]:用户删除数据成功。 INVALID REQUEST - [POST/PUT/PATCH]:用户发出的请求有错误,服务器没有进行新建或修改数据的操作,该操作是幂等的。 Unauthorized - [*]:表示用户没有权限(令牌、用户名、密码错误)。 Forbidden - [*] 表示用户得到授权(与401错误相对),但是访问是被禁止的。 NOT FOUND - [*]:用户发出的请求针对的是不存在的记录,服务器没有进行操作,该操作是幂等的。 Not Acceptable - [GET]:用户请求的格式不可得(比如用户请求JSON格式,但是只有XML格式)。 Gone -[GET]:用户请求的资源被永久删除,且不会再得到的。 Unprocesable entity - [POST/PUT/PATCH] 当创建一个对象时,发生一个验证错误。 INTERNAL SERVER ERROR - [*]:服务器发生错误,用户将无法判断发出的请求是否成功。 更多看这里:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 常用状态码列表
错误处理
当状态码是4xx时,应该返回错误信息,把err当做key
返回结果(根据不同的操作,服务器向用户返回不同的结果应该符合以下规范)
GET /collection:返回资源对象的列表(数组) GET /collection/resource:返回单个资源对象 POST /collection:返回新生成的资源对象 PUT /collection/resource:返回完整的资源对象 PATCH /collection/resource:返回完整的资源对象 DELETE /collection/resource:返回一个空文档
二、基于Django实现
路由系统
urlpatterns = [ url(r'^users', Users.as_view()), ]
CBV视图
from django.views import View from django.http import JsonResponse class Users(View): def get(self, request, *args, **kwargs): result = { 'status': True, 'data': 'response data' } return JsonResponse(result, status=200) def post(self, request, *args, **kwargs): result = { 'status': True, 'data': 'response data' } return JsonResponse(result, status=200)
三. 基于Django Rest Framework框架实现
1.基本流程
url.py
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ # url(r'^admin/', admin.site.urls), # url(r'^users/', views.UsersView.as_view()), # url(r'^user/(d+)', views.UserView.as_view()), ]
view.py
from django.shortcuts import render,HttpResponse from django.views import View import json class UsersView(View): def get(self,request): response = {'code': 1000, 'data': None} response['data'] = [ {'name': '盛松', 'age': 19}, {'name': '鲁宁', 'age': 20}, {'name': '解析博', 'age': 5}, ] return HttpResponse(json.dumps(response), status=200) class UserView(View): def dispatch(self, request, *args, **kwargs): # 请求到来之后都要执行dispatch,dispatch方法根据请求方式不同触发 get/post/put等方法 # 注意:APIView中的dispatch方法有好多好多的功能 # method = request.method.lower() # func = getattr(self,method) # ret = func() # return ret ret = super(UserView,self).dispatch(request,*args, **kwargs) return ret def get(self,request,pk): print(request,type(request)) return HttpResponse(json.dumps({'name': '盛松', 'age': 19})) def post(self,request,pk): return HttpResponse(json.dumps({'name': '盛松', 'age': 19})) def put(self,request,pk): return HttpResponse(json.dumps({'name': '盛松', 'age': 19})) def delete(self,request,pk): return HttpResponse(json.dumps({'name': '盛松', 'age': 19}))
2.认证和授权
url.py
from app02 import views as app02_view urlpatterns=[ url(r'^auth/', app02_view.AuthView.as_view()), url(r'^hosts/', app02_view.HostView.as_view()), url(r'users',app02_view.Userview.as_view()), url(r'salary', app02_view.Salaryview.as_view()), # django rest framework ]
view.py
from django.views import View from rest_framework.views import APIView from rest_framework.authentication import SessionAuthentication from rest_framework.authentication import BasicAuthentication from rest_framework.authentication import BaseAuthentication from rest_framework.request import Request from rest_framework.exceptions import APIException from rest_framework.response import Response from rest_framework.permissions import AllowAny from rest_framework.exceptions import APIException,AuthenticationFailed from rest_framework.response import Response from rest_framework.renderers import JSONRenderer,BrowsableAPIRenderer from rest_framework import exceptions from app02 import models import hashlib import time # class MyBasicAuthentication(BasicAuthentication): # def authenticate_credentials(self, userid, password, request=None): # if userid == 'alex' and password == '123': # return ('alex','authaaaaaaaaaaaa') # raise APIException('认证失败') class AuthView(APIView): authentication_classes=[] def get(self,request): """ 接收用户名和密码 :param request: :return: """ ret = {'code':1000,'msg':None} user = request.query_params.get('user') pwd = request.query_params.get('pwd') obj = models.UserInfo.objects.filter(username=user,password=pwd).first() if not obj: ret['code'] = 1001 ret['msg'] = "用户名或密码错误" return Response(ret) # 创建随机字符串 ctime = time.time() print(ctime) key = "%s|%s" %(user,ctime) m = hashlib.md5() m.update(key.encode('utf-8')) token = m.hexdigest() # 保存到数据 obj.token = token obj.save() ret['token'] = token return Response(ret) class Myauthentication(BaseAuthentication): def authenticate(self, request): token=request.query_params.get('token') obj=models.UserInfo.objects.filter(token=token) if obj: return (obj.username,obj) return None def authenticate_header(self, request): pass class Mypermission(object): message='无权访问' def has_permission(self,request,view): return False class Adminermission(object): message='无权访问' def has_permission(self,request,view): if request.user=='root': return True return False class HostView(APIView): def get(self,request,*args,**kwargs): self.dispatch # print(request.user) # print(request.auth) return Response('主机列表') class Userview(APIView): authentication_classes = [Myauthentication,] permission_classes = [Mypermission] def get(self,request,*args,**kwargs): return Response('用户列表') class Salaryview(APIView): authentication_classes = [Myauthentication,] permission_classes = [Mypermission,Adminermission] def get(self,request,*args,**kwargs): return Response('薪资列表') def permission_denied(self, request, message=None): """ If request is not permitted, determine what kind of exception to raise. """ if request.authenticators and not request.successful_authenticator: raise exceptions.NotAuthenticated(detail='xx') raise exceptions.PermissionDenied(detail=message)