Commit 6051d011 authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

fixed code to run again, with routing totally separated from core Resource

parent 65689030
......@@ -151,7 +151,6 @@ class RequestRouter:
'''Obsolete'''
raise RuntimeError("Do not @app.route() any more, as of 0.0.6 API has been re-designed around class methods, see docs and examples")
@defer.inlineCallbacks
def getResponse(self,request):
"""Finds the appropriate router and dispatches the request to the registered function. Returns the appropriate Response object"""
# see if already cached
......@@ -211,7 +210,7 @@ class RequestRouter:
if isinstance(val,defer.Deferred):
# add callback to finish the request
val.addCallback(self.__finishDeferred,request)
yield val.callback(request)
return val
else:
#special logic for POST to return 201 (created)
if request.method == Http.POST:
......@@ -221,26 +220,26 @@ class RequestRouter:
else:
request.setResponseCode(201)
defer.returnValue(self.__renderResponse(request, val))
return self.__generateResponse(request, val, request.code)
except exceptions.TypeError as ex:
defer.returnValue(self.__createErrorResponse(request,400,"%s" % ex))
return self.__createErrorResponse(request,400,"%s" % ex)
except Exception as ex:
defer.returnValue(self.__createErrorResponse(request,500,"Unexpected server error: %s\n%s" % (type(ex),ex)))
return self.__createErrorResponse(request,500,"Unexpected server error: %s\n%s" % (type(ex),ex))
else:
defer.returnValue(self.__createErrorResponse(request,404,"URL '%s' not found\n" % request.path))
return self.__createErrorResponse(request,404,"URL '%s' not found\n" % request.path)
except Exception as ex:
defer.returnValue(self.__createErrorResponse(request,500,"Internal server error: %s" % ex))
return self.__createErrorResponse(request,500,"Internal server error: %s" % ex)
def __renderResponse(self,request,response,code=200):
def __generateResponse(self,request,response,code=200):
"""
Takes care of automatically rendering the response and converting it to appropriate format (text,XML,JSON,YAML)
depending on what the caller can accept. Returns Response
"""
if isinstance(response, str):
return Response(code,response)
return Response(code,response,{HttpHeader.CONTENT_TYPE:MediaType.TEXT_PLAIN})
elif isinstance(response, Response):
return response
else:
......@@ -274,12 +273,12 @@ class RequestRouter:
"""Finishes any Defered/inlineCallback methods. Returns Response"""
if val != None:
try:
defer.returnValue(self.__renderResponse(request,val))
return self.__generateResponse(request,val)
except Exception as ex:
msg = "Unexpected server error: %s\n%s" % (type(ex),ex)
defer.returnValue(self.__createErrorResponse(request, 500, msg))
return self.__createErrorResponse(request, 500, msg)
else:
defer.returnValue(Response(209,None))
return Response(209,None)
def __createErrorResponse(self,request,code,message):
"""Common method for rendering errors"""
......
......@@ -51,13 +51,21 @@ class CorePost(Resource):
def __renderUrl(self,request):
try:
val = self.__router.getResponse(request)
# return can be Deferred or Response
if isinstance(val,Deferred):
val.addCallback(self.__finishRequest,request)
return NOT_DONE_YET
elif isinstance(val,Response):
self.__applyResponse(request, val.code, val.headers)
return val.entity
else:
raise RuntimeError("Unexpected return type from request router %s" % val)
except Exception as ex:
self.__applyResponse(request, 500, None)
return str(ex)
def __finishRequest(self,response,request):
if not request.finished:
self.__applyResponse(request, response.code,response.headers)
request.write(response.entity)
request.finish()
......
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