Commit a7e87051 authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

added parsing of form and query args into direct function parameters

parent 54518aef
......@@ -3,12 +3,13 @@ Main server classes
@author: jacekf
'''
import re
import re, copy
from twisted.internet import reactor
from twisted.web.resource import Resource
from twisted.web.server import Site
from collections import defaultdict
from enums import MediaType
from corepost.enums import Http
class RequestRouter:
""" Common class for containing info related to routing a request to a function """
......@@ -139,9 +140,10 @@ class CorePost(Resource):
def __renderUrl(self,request):
"""Finds the appropriate router and dispatches the request to the registered function"""
# see if already cached
urlrouter, pathargs = None, None
if request.path in self.__cachedUrls[request.method]:
cachedUrl = self.__cachedUrls[request.method][request.path]
return cachedUrl.router.call(request,**cachedUrl.args)
urlrouter,pathargs = cachedUrl.router, cachedUrl.args
else:
# first time this URL is called
for router in self.__urls[request.method].values():
......@@ -149,8 +151,22 @@ class CorePost(Resource):
if args != None:
if router.cache:
self.__cachedUrls[request.method][request.path] = CachedUrl(router, args)
return router.call(request,**args)
urlrouter,pathargs = router,args
#actual call
if urlrouter != None and pathargs != None:
allargs = copy.deepcopy(pathargs)
#merge form args
for arg in request.args.keys():
# maintain first instance of an argument always
if arg not in allargs:
allargs[arg] = request.args[arg][0]
# if POST/PUT, check if we need to automatically parse JSON
# TODO
return urlrouter.call(request,**allargs)
else:
return self.__renderError(request,404,"URL '%s' not found\n" % request.path)
def __renderError(self,request,code,message):
......
......@@ -9,16 +9,20 @@ from corepost.enums import Http
app = CorePost()
@app.route("/",Http.GET)
def root(request):
return request.path
def root(request,**kwargs):
return "%s" % kwargs
@app.route("/test",Http.GET)
def test(request):
return request.path
def test(request,**kwargs):
return "%s" % kwargs
@app.route("/test/<int:jacek>/yo/<someid>",Http.GET)
def test_get_resources(request,jacek,someid,**kwargs):
return "%s - %s" % (jacek,someid)
@app.route("/test",Http.POST)
def test_post(request,**kwargs):
return "%s" % kwargs
if __name__ == '__main__':
app.run()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment