Commit f9d52ba7 authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

bug fixes: handle conversions of numerics in JSON responses, better parsing of...

bug fixes: handle conversions of numerics in JSON responses, better parsing of arguments with unexpected characters
parent 1259ce7c
...@@ -13,9 +13,14 @@ from UserDict import DictMixin ...@@ -13,9 +13,14 @@ from UserDict import DictMixin
xmlListTemplate = Template("""<list>{% for item in items %}<item>{% for prop,val in item.iteritems() %}<{{prop}}>{{val}}</{{prop}}>{% endfor %}</item>{% endfor %}</list>""") xmlListTemplate = Template("""<list>{% for item in items %}<item>{% for prop,val in item.iteritems() %}<{{prop}}>{{val}}</{{prop}}>{% endfor %}</item>{% endfor %}</list>""")
xmlTemplate = Template("""<item>{% for prop,val in item.iteritems() %}<{{prop}}>{{val}}</{{prop}}>{% endfor %}</item>""") xmlTemplate = Template("""<item>{% for prop,val in item.iteritems() %}<{{prop}}>{{val}}</{{prop}}>{% endfor %}</item>""")
primitives = (int, long, float, bool, str,unicode)
def convertForSerialization(obj): def convertForSerialization(obj):
"""Converts anything (clas,tuples,list) to the safe serializable equivalent""" """Converts anything (clas,tuples,list) to the safe serializable equivalent"""
if isinstance(obj, dict) or isinstance(obj,DictMixin): if type(obj) in primitives:
# no conversion
return obj
elif isinstance(obj, dict) or isinstance(obj,DictMixin):
return traverseDict(obj) return traverseDict(obj)
elif isClassInstance(obj): elif isClassInstance(obj):
return convertClassToDict(obj) return convertClassToDict(obj)
......
...@@ -21,8 +21,8 @@ from xml.etree import ElementTree ...@@ -21,8 +21,8 @@ from xml.etree import ElementTree
class UrlRouter: class UrlRouter:
''' 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 '''
__urlMatcher = re.compile(r"<(int|float|):?([a-zA-Z0-9]+)>") __urlMatcher = re.compile(r"<(int|float|):?([^/]+)>")
__urlRegexReplace = {"":r"(?P<arg>([a-zA-Z0-9]+))","int":r"(?P<arg>\d+)","float":r"(?P<arg>\d+.?\d*)"} __urlRegexReplace = {"":r"(?P<arg>([^/]+))","int":r"(?P<arg>\d+)","float":r"(?P<arg>\d+.?\d*)"}
__typeConverters = {"int":int,"float":float} __typeConverters = {"int":int,"float":float}
def __init__(self,f,url,methods,accepts,produces,cache): def __init__(self,f,url,methods,accepts,produces,cache):
...@@ -257,6 +257,7 @@ class RequestRouter: ...@@ -257,6 +257,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)
val.addErrback(self.__finishDeferredError,request)
return val return val
else: else:
#special logic for POST to return 201 (created) #special logic for POST to return 201 (created)
...@@ -305,7 +306,7 @@ class RequestRouter: ...@@ -305,7 +306,7 @@ class RequestRouter:
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,{HttpHeader.CONTENT_TYPE:MediaType.TEXT_PLAIN}) return Response(code,response,{HttpHeader.CONTENT_TYPE: MediaType.TEXT_PLAIN})
elif isinstance(response, Response): elif isinstance(response, Response):
return response return response
else: else:
...@@ -326,7 +327,7 @@ class RequestRouter: ...@@ -326,7 +327,7 @@ class RequestRouter:
elif MediaType.TEXT_YAML in accept: elif MediaType.TEXT_YAML in accept:
return (yaml.dump(obj),MediaType.TEXT_YAML) return (yaml.dump(obj),MediaType.TEXT_YAML)
elif MediaType.APPLICATION_XML in accept or MediaType.TEXT_XML in accept: elif MediaType.APPLICATION_XML in accept or MediaType.TEXT_XML in accept:
return (generateXml(obj),MediaType.APPLICATION_XML+";charset=utf-8") return (generateXml(obj),MediaType.APPLICATION_XML)
else: else:
# no idea, let's do JSON # no idea, let's do JSON
return (convertToJson(obj),MediaType.APPLICATION_JSON) return (convertToJson(obj),MediaType.APPLICATION_JSON)
...@@ -347,6 +348,11 @@ class RequestRouter: ...@@ -347,6 +348,11 @@ class RequestRouter:
else: else:
return Response(209,None) return Response(209,None)
def __finishDeferredError(self,error,request):
"""Finishes any Defered/inlineCallback methods that raised an error. Returns Response"""
log.err(error, "Deferred failed")
return self.__createErrorResponse(request, 500,"Internal server error")
def __createErrorResponse(self,request,code,message): def __createErrorResponse(self,request,code,message):
"""Common method for rendering errors""" """Common method for rendering errors"""
return Response(code=code, entity=message, headers={"content-type": MediaType.TEXT_PLAIN}) return Response(code=code, entity=message, headers={"content-type": MediaType.TEXT_PLAIN})
......
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