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 ...@@ -3,12 +3,13 @@ Main server classes
@author: jacekf @author: jacekf
''' '''
import re import re, copy
from twisted.internet import reactor from twisted.internet import reactor
from twisted.web.resource import Resource from twisted.web.resource import Resource
from twisted.web.server import Site from twisted.web.server import Site
from collections import defaultdict from collections import defaultdict
from enums import MediaType from enums import MediaType
from corepost.enums import Http
class RequestRouter: class RequestRouter:
""" Common class for containing info related to routing a request to a function """ """ Common class for containing info related to routing a request to a function """
...@@ -139,9 +140,10 @@ class CorePost(Resource): ...@@ -139,9 +140,10 @@ class CorePost(Resource):
def __renderUrl(self,request): def __renderUrl(self,request):
"""Finds the appropriate router and dispatches the request to the registered function""" """Finds the appropriate router and dispatches the request to the registered function"""
# see if already cached # see if already cached
urlrouter, pathargs = None, None
if request.path in self.__cachedUrls[request.method]: if request.path in self.__cachedUrls[request.method]:
cachedUrl = self.__cachedUrls[request.method][request.path] cachedUrl = self.__cachedUrls[request.method][request.path]
return cachedUrl.router.call(request,**cachedUrl.args) urlrouter,pathargs = cachedUrl.router, cachedUrl.args
else: else:
# first time this URL is called # first time this URL is called
for router in self.__urls[request.method].values(): for router in self.__urls[request.method].values():
...@@ -149,9 +151,23 @@ class CorePost(Resource): ...@@ -149,9 +151,23 @@ class CorePost(Resource):
if args != None: if args != None:
if router.cache: if router.cache:
self.__cachedUrls[request.method][request.path] = CachedUrl(router, args) self.__cachedUrls[request.method][request.path] = CachedUrl(router, args)
return router.call(request,**args) urlrouter,pathargs = router,args
return self.__renderError(request,404,"URL '%s' not found\n" % request.path) #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): def __renderError(self,request,code,message):
"""Common method for rendering errors""" """Common method for rendering errors"""
......
...@@ -9,16 +9,20 @@ from corepost.enums import Http ...@@ -9,16 +9,20 @@ from corepost.enums import Http
app = CorePost() app = CorePost()
@app.route("/",Http.GET) @app.route("/",Http.GET)
def root(request): def root(request,**kwargs):
return request.path return "%s" % kwargs
@app.route("/test",Http.GET) @app.route("/test",Http.GET)
def test(request): def test(request,**kwargs):
return request.path return "%s" % kwargs
@app.route("/test/<int:jacek>/yo/<someid>",Http.GET) @app.route("/test/<int:jacek>/yo/<someid>",Http.GET)
def test_get_resources(request,jacek,someid,**kwargs): def test_get_resources(request,jacek,someid,**kwargs):
return "%s - %s" % (jacek,someid) return "%s - %s" % (jacek,someid)
@app.route("/test",Http.POST)
def test_post(request,**kwargs):
return "%s" % kwargs
if __name__ == '__main__': if __name__ == '__main__':
app.run() 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