Restful 接口規範

[TOC]django

Restful 接口規範

與django相比的話,不會出現csrf問題api

接口規範:安全

https/api/v1/books/get、post/?limit=3/http_status/{status,msg,results}/data:https://delete
成功不作數據返回

簡介

2000年Roy Fielding博士在其博士論文中提出REST(Representational State Transfer)風格的軟件架構模式後,REST就基本上迅速取代了複雜而笨重的SOAP,成爲Web API的標準了。服務器

RESTful做爲目前最流行的 API 設計規範,必定有着它獨有的魅力:強大、簡易、易上手。架構

URL設計

django內數據分析

views.pyapp

from . import models

class Book(View):
    def get(self, request, *args, **kwargs):
        pk = kwargs.get('pk', None)
        if pk:  # 單查
            book_dic = models.Book.objects.filter(pk=pk).values('name', 'price').first()
            results = book_dic
        else:  # 羣查
            book_query = models.Book.objects.values('name', 'price')
            results = list(book_query)

        if not results:
            return JsonResponse({
                'status': 1,
                'msg': 'data error',
            })
        return JsonResponse({
            'status': 0,
            'msg': 'ok',
            'results': results
        })

    # 單增:{}
    # 羣增:[{},{},{}]
    def post(self, request, *args, **kwargs):
        return JsonResponse({'res': 'post ok'})

    # 單改:pk,{}
    # 羣改:[{pk:1,...},{},{}]
    def put(self, request, *args, **kwargs):

        return JsonResponse({'res': 'put ok'})

    # 單改:pk,{}
    # 羣改:[{pk:1,...},{},{}]
    def patch(self, request, *args, **kwargs):
        return JsonResponse({'res': 'patch ok'})

    # 單刪:pk
    # 羣刪:pks
    def delete(self, request, *args, **kwargs):
        return JsonResponse({'res': 'delete ok'})

urls.pypost

from django.conf.urls import url, include
from django.contrib import admin

from api import views
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^test/', views.Test.as_view()),

    # 路由分發
    url(r'^api/', include('api.urls')),
]

app內的urls.pyui

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^books/$', views.Book.as_view()),
    url(r'^books/(?P<pk>\d+)/$', views.Book.as_view()),
]

數據的安全保障

  • url連接通常都採用https協議進行傳輸url

    注:採用https協議,能夠提升數據交互過程當中的安全性spa

接口特徵表現

多數據版本共存

數據便是資源

資源操做由請求方式決定

響應狀態碼

正常響應

  • 響應狀態碼2xx
    • 200:常規請求
    • 201:建立成功

重定向響應

  • 響應狀態碼3xx
    • 301:永久重定向
    • 302:暫時重定向

客戶端異常

  • 響應狀態碼4xx
    • 403:請求無權限
    • 404:請求路徑不存在
    • 405:請求方法不存在

服務器異常

  • 響應狀態碼5xx
    • 500:服務器異常

響應結果

響應數據要有狀態碼、狀態信息以及數據自己

{
    "status": 0,
    "msg": "ok",
    "results":[
        {
            "name":"肯德基(羅餐廳)",
            "location":{
                "lat":31.415354,
                "lng":121.357339
            },
            "address":"月羅路2380號",
            "province":"上海市",
            "city":"上海市",
            "area":"寶山區",
            "street_id":"339ed41ae1d6dc320a5cb37c",
            "telephone":"(021)56761006",
            "detail":1,
            "uid":"339ed41ae1d6dc320a5cb37c"
        }
        ...
        ]
}

須要url請求的資源須要訪問資源的請求連接

{
    "status": 0,
    "msg": "ok",
    "results":[
        {
            "name":"肯德基(羅餐廳)",
            "img": "https://image.baidu.com/kfc/001.png"
        }
        ...
        ]
}

相關文章
相關標籤/搜索