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: ...@@ -151,7 +151,6 @@ class RequestRouter:
'''Obsolete''' '''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") 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): def getResponse(self,request):
"""Finds the appropriate router and dispatches the request to the registered function. Returns the appropriate Response object""" """Finds the appropriate router and dispatches the request to the registered function. Returns the appropriate Response object"""
# see if already cached # see if already cached
...@@ -211,7 +210,7 @@ class RequestRouter: ...@@ -211,7 +210,7 @@ class RequestRouter:
if isinstance(val,defer.Deferred): if isinstance(val,defer.Deferred):
# add callback to finish the request # add callback to finish the request
val.addCallback(self.__finishDeferred,request) val.addCallback(self.__finishDeferred,request)
yield val.callback(request) return val
else: else:
#special logic for POST to return 201 (created) #special logic for POST to return 201 (created)
if request.method == Http.POST: if request.method == Http.POST:
...@@ -221,26 +220,26 @@ class RequestRouter: ...@@ -221,26 +220,26 @@ class RequestRouter:
else: else:
request.setResponseCode(201) request.setResponseCode(201)
defer.returnValue(self.__renderResponse(request, val)) return self.__generateResponse(request, val, request.code)
except exceptions.TypeError as ex: except exceptions.TypeError as ex:
defer.returnValue(self.__createErrorResponse(request,400,"%s" % ex)) return self.__createErrorResponse(request,400,"%s" % ex)
except Exception as 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: 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: 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) 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 depending on what the caller can accept. Returns Response
""" """
if isinstance(response, str): if isinstance(response, str):
return Response(code,response) return Response(code,response,{HttpHeader.CONTENT_TYPE:MediaType.TEXT_PLAIN})
elif isinstance(response, Response): elif isinstance(response, Response):
return response return response
else: else:
...@@ -274,12 +273,12 @@ class RequestRouter: ...@@ -274,12 +273,12 @@ class RequestRouter:
"""Finishes any Defered/inlineCallback methods. Returns Response""" """Finishes any Defered/inlineCallback methods. Returns Response"""
if val != None: if val != None:
try: try:
defer.returnValue(self.__renderResponse(request,val)) return self.__generateResponse(request,val)
except Exception as ex: except Exception as ex:
msg = "Unexpected server error: %s\n%s" % (type(ex),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: else:
defer.returnValue(Response(209,None)) return Response(209,None)
def __createErrorResponse(self,request,code,message): def __createErrorResponse(self,request,code,message):
"""Common method for rendering errors""" """Common method for rendering errors"""
......
...@@ -51,16 +51,24 @@ class CorePost(Resource): ...@@ -51,16 +51,24 @@ class CorePost(Resource):
def __renderUrl(self,request): def __renderUrl(self,request):
try: try:
val = self.__router.getResponse(request) val = self.__router.getResponse(request)
val.addCallback(self.__finishRequest,request) # return can be Deferred or Response
return NOT_DONE_YET 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: except Exception as ex:
self.__applyResponse(request, 500, None) self.__applyResponse(request, 500, None)
return str(ex) return str(ex)
def __finishRequest(self,response,request): def __finishRequest(self,response,request):
self.__applyResponse(request, response.code,response.headers) if not request.finished:
request.write(response.entity) self.__applyResponse(request, response.code,response.headers)
request.finish() request.write(response.entity)
request.finish()
def __applyResponse(self,request,code,headers={"content-type":MediaType.TEXT_PLAIN}): def __applyResponse(self,request,code,headers={"content-type":MediaType.TEXT_PLAIN}):
request.setResponseCode(code) request.setResponseCode(code)
......
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