44
55from . import apiList as apiList
66
7- def empty (request ):
8- return HttpResponse ("You need to give me an api name" )
9-
10- def index (request ):
7+ def indexGET (request ):
118# resStr = """
129# request.path: {}
1310# request.body: {}
@@ -35,23 +32,52 @@ def index(request):
3532 # #-------------------------------------
3633 # # deal with GET
3734
38- # preRe= re.compile('^/api/')
39- # applicationPath = preRe.sub('', request.path)
40- # print(applicationPath)
41-
4235 # if request.method == 'GET':
36+ # # 刪掉網址中的 /api/
37+ # preRe= re.compile('^/api/')
38+ # apiName = preRe.sub('', request.path)
39+
4340 # parameterGET = dict(request.GET)
4441 # # http://localhost:8000/api/index?aaa=b;hhh=t
4542 # # {'aaa': ['b'], 'hhh': ['t']}
43+ # for eachP in parameterGET:
44+ # if len(parameterGET[eachP])==1:
45+ # parameterGET[eachP] = parameterGET[eachP][0]
46+
4647 # data = ''
4748 # try:
48- # data = getattr(apiList, applicationPath )(parameterGET)
49+ # data = getattr(apiList, apiName )(parameterGET)
4950 # if data['status']:
5051 # data = json.dumps(data['data'])
5152 # else:
5253 # data = data['err']
5354 # except:
5455 # data = '沒有這支 api'
56+ # # http://localhost:8000/api/playData?name=Charmander
57+ rtnRes .write (data )
58+ return rtnRes
5559
56- # rtnRes.write(data)
60+ def indexPOST (request ):
61+ rtnRes = HttpResponse ()
62+ # #-------------------------------------
63+ # # deal with POST NEED TO DISABLE CSRF
64+ if request .method == 'POST' :
65+ if request .content_type == 'application/json' :
66+ parameterPOST = json .loads ( str (request .body , encoding = 'utf8' ) )
67+ apiName = parameterPOST ['api' ]
68+ try :
69+ apiName = parameterPOST ['api' ]
70+ data = getattr (apiList , apiName )(parameterPOST )
71+ except :
72+ data = "api 為必要欄位"
73+
74+ else :
75+ data = "如用 GET 請加上 api 名稱"
76+
77+ rtnRes .write (data )
5778 return rtnRes
79+
80+ # {
81+ # "api": "playData",
82+ # "name": "Charmander"
83+ # }
0 commit comments