Commit 8443937b authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

updated the merge from kaosat-dev to remove jsonpickle (for now) and fix some...

updated the merge from kaosat-dev to remove jsonpickle (for now) and fix some subtle bugs that broke existing BDDs
parent 4ecf20e7
...@@ -12,13 +12,6 @@ import json ...@@ -12,13 +12,6 @@ import json
from UserDict import DictMixin from UserDict import DictMixin
from twisted.python import log from twisted.python import log
advanced_json = False
try:
import jsonpickle
advanced_json = True
except ImportError: pass
primitives = (int, long, float, bool, str,unicode) primitives = (int, long, float, bool, str,unicode)
def convertForSerialization(obj): def convertForSerialization(obj):
...@@ -66,11 +59,6 @@ def traverseDict(dictObject): ...@@ -66,11 +59,6 @@ def traverseDict(dictObject):
def convertToJson(obj): def convertToJson(obj):
"""Converts to JSON, including Python classes that are not JSON serializable by default""" """Converts to JSON, including Python classes that are not JSON serializable by default"""
if advanced_json:
try:
return jsonpickle.encode(obj, unpicklable=False)
except Exception as ex:
raise RuntimeError(str(ex))
try: try:
return json.dumps(obj) return json.dumps(obj)
except Exception as ex: except Exception as ex:
......
...@@ -19,12 +19,6 @@ import re, copy, exceptions, yaml,json, logging ...@@ -19,12 +19,6 @@ import re, copy, exceptions, yaml,json, logging
from xml.etree import ElementTree from xml.etree import ElementTree
import uuid import uuid
advanced_json = False
try:
import jsonpickle
advanced_json = True
except ImportError: pass
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 '''
...@@ -320,7 +314,9 @@ class RequestRouter: ...@@ -320,7 +314,9 @@ class RequestRouter:
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, Response): if isinstance(response, str):
return Response(code,response,{HttpHeader.CONTENT_TYPE: MediaType.TEXT_PLAIN})
elif isinstance(response, Response):
return response return response
else: else:
(content,contentType) = self.__convertObjectToContentType(request, response) (content,contentType) = self.__convertObjectToContentType(request, response)
...@@ -331,27 +327,20 @@ class RequestRouter: ...@@ -331,27 +327,20 @@ class RequestRouter:
Takes care of converting an object (non-String) response to the appropriate format, based on the what the caller can accept. Takes care of converting an object (non-String) response to the appropriate format, based on the what the caller can accept.
Returns a tuple of (content,contentType) Returns a tuple of (content,contentType)
""" """
obj = convertForSerialization(obj)
if HttpHeader.ACCEPT in request.received_headers: if HttpHeader.ACCEPT in request.received_headers:
accept = request.received_headers[HttpHeader.ACCEPT] accept = request.received_headers[HttpHeader.ACCEPT]
if MediaType.APPLICATION_JSON in accept: if MediaType.APPLICATION_JSON in accept:
if not advanced_json:
obj = convertForSerialization(obj)
return (convertToJson(obj),MediaType.APPLICATION_JSON) return (convertToJson(obj),MediaType.APPLICATION_JSON)
elif MediaType.TEXT_YAML in accept: elif MediaType.TEXT_YAML in accept:
obj = convertForSerialization(obj)
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:
obj = convertForSerialization(obj)
return (generateXml(obj),MediaType.APPLICATION_XML) return (generateXml(obj),MediaType.APPLICATION_XML)
else: else:
# no idea, let's do JSON # no idea, let's do JSON
if not advanced_json:
obj = convertForSerialization(obj)
return (convertToJson(obj),MediaType.APPLICATION_JSON) return (convertToJson(obj),MediaType.APPLICATION_JSON)
else: else:
if not advanced_json:
obj = convertForSerialization(obj)
# called has no accept header, let's default to JSON # called has no accept header, let's default to JSON
return (convertToJson(obj),MediaType.APPLICATION_JSON) return (convertToJson(obj),MediaType.APPLICATION_JSON)
...@@ -381,20 +370,21 @@ class RequestRouter: ...@@ -381,20 +370,21 @@ class RequestRouter:
'''Automatically parses JSON,XML,YAML if present''' '''Automatically parses JSON,XML,YAML if present'''
if request.method in (Http.POST,Http.PUT) and HttpHeader.CONTENT_TYPE in request.received_headers.keys(): if request.method in (Http.POST,Http.PUT) and HttpHeader.CONTENT_TYPE in request.received_headers.keys():
contentType = request.received_headers["content-type"] contentType = request.received_headers["content-type"]
data = request.content.read() request.data = request.content.read()
if contentType == MediaType.APPLICATION_JSON: if contentType == MediaType.APPLICATION_JSON:
try: try:
request.json = json.loads(data) if data else {} request.json = json.loads(request.data) if request.data else {}
except Exception as ex: except Exception as ex:
raise TypeError("Unable to parse JSON body: %s" % ex) raise TypeError("Unable to parse JSON body: %s" % ex)
elif contentType in (MediaType.APPLICATION_XML,MediaType.TEXT_XML): elif contentType in (MediaType.APPLICATION_XML,MediaType.TEXT_XML):
try: try:
request.xml = ElementTree.XML(data) request.xml = ElementTree.XML(request.data)
except Exception as ex: except Exception as ex:
raise TypeError("Unable to parse XML body: %s" % ex) raise TypeError("Unable to parse XML body: %s" % ex)
elif contentType == MediaType.TEXT_YAML: elif contentType == MediaType.TEXT_YAML:
try: try:
request.yaml = yaml.safe_load(data) request.yaml = yaml.safe_load(request.data)
except Exception as ex: except Exception as ex:
raise TypeError("Unable to parse YAML body: %s" % ex) raise TypeError("Unable to parse YAML body: %s" % ex)
...@@ -403,9 +393,11 @@ class RequestRouter: ...@@ -403,9 +393,11 @@ class RequestRouter:
# handler for weird Twisted logic where PUT does not get form params # handler for weird Twisted logic where PUT does not get form params
# see: http://twistedmatrix.com/pipermail/twisted-web/2007-March/003338.html # see: http://twistedmatrix.com/pipermail/twisted-web/2007-March/003338.html
requestargs = request.args requestargs = request.args
if request.method == Http.PUT and HttpHeader.CONTENT_TYPE in request.received_headers.keys() \ if request.method == Http.PUT and HttpHeader.CONTENT_TYPE in request.received_headers.keys() \
and request.received_headers[HttpHeader.CONTENT_TYPE] == MediaType.APPLICATION_FORM_URLENCODED: and request.received_headers[HttpHeader.CONTENT_TYPE] == MediaType.APPLICATION_FORM_URLENCODED:
requestargs = parse_qs(request.content.read(), 1) # request.data is populated in __parseRequestData
requestargs = parse_qs(request.data, 1)
#merge form args #merge form args
if len(requestargs.keys()) > 0: if len(requestargs.keys()) > 0:
......
...@@ -43,17 +43,17 @@ Feature: URL routing ...@@ -43,17 +43,17 @@ Feature: URL routing
When as user 'None:None' I DELETE 'http://127.0.0.1:8080/delete' When as user 'None:None' I DELETE 'http://127.0.0.1:8080/delete'
Then I expect HTTP code 200 Then I expect HTTP code 200
@single @single_post @single_put @single @single_post @single_put @1
Scenario: Single resource - multiple methods at same URL Scenario: Single resource - multiple methods at same URL
Given 'home_resource' is running Given 'home_resource' is running
When as user 'None:None' I POST 'http://127.0.0.1:8080/postput' with 'test=value&test2=value2' When as user 'None:None' I POST 'http://127.0.0.1:8080/postput' with 'test=value&test2=value2'
# POST return 201 by default # POST return 201 by default
Then I expect HTTP code 201 Then I expect HTTP code 201
And I expect content contains '{'test': 'value', 'test2': 'value2'}' And I expect content contains '{'test': 'value', 'test2': 'value2'}'
When as user 'None:None' I PUT 'http://127.0.0.1:8080/postput' with 'test=value&test2=value2' When as user 'None:None' I PUT 'http://127.0.0.1:8080/postput' with 'test=value&test3=value3'
# PUT return 201 by default # PUT return 201 by default
Then I expect HTTP code 200 Then I expect HTTP code 200
And I expect content contains '{'test': 'value', 'test2': 'value2'}' And I expect content contains '{'test': 'value', 'test3': 'value3'}'
@multi @multi
Scenario Outline: Multiple resources with submodules Scenario Outline: Multiple resources with submodules
......
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