Commit a7a26a6a authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

support for automatic parsing of JSON, YAML, XML, Form arguments, needs docs before final release

parent f7cdbf9a
......@@ -7,7 +7,7 @@ Common routing classes, regardless of whether used in HTTP or multiprocess conte
from collections import defaultdict
from corepost import Response, RESTException
from corepost.enums import Http, HttpHeader
from corepost.utils import getMandatoryArgumentNames, convertToJson
from corepost.utils import getMandatoryArgumentNames, convertToJson, safeDictUpdate
from corepost.convert import convertForSerialization, generateXml
from corepost.filters import IRequestFilter, IResponseFilter
......@@ -385,16 +385,25 @@ class RequestRouter:
requestargs = parse_qs(request.content.read(), 1)
#merge form args
if len(requestargs.keys()) > 0:
for arg in requestargs.keys():
# maintain first instance of an argument always
if arg not in allargs:
allargs[arg] = requestargs[arg][0]
# if JSON parse root elements instead of form elements
if hasattr(request,'json'):
safeDictUpdate(allargs,arg,requestargs[arg][0])
elif hasattr(request,'json'):
# if YAML parse root elements instead of form elements
for key in request.json.keys():
if key not in allargs:
allargs[key] = request.json[key]
safeDictUpdate(allargs, key, request.json[key])
elif hasattr(request,'yaml'):
# if YAML parse root elements instead of form elements
for key in request.yaml.keys():
safeDictUpdate(allargs, key, request.yaml[key])
elif hasattr(request,'xml'):
# if XML, parse attributes first, then root nodes
for key in request.xml.attrib:
safeDictUpdate(allargs, key, request.xml.attrib[key])
for el in request.xml.findall("*"):
safeDictUpdate(allargs, el.tag,el.text)
def __filterRequests(self,request):
"""Filters incoming requests"""
......
......@@ -33,11 +33,13 @@ Feature: Arguments
@arguments_by_type
Scenario Outline: Parse form arguments OR from JSON documents for POST / PUT
Given 'arguments' is running
# pass in as form arguments
When as user 'None:None' I <method> 'http://127.0.0.1:8082/formOrJson' with 'first=John&last=Doe'
Then I expect HTTP code <code>
And I expect content contains 'John Doe'
# pass in as JSON document
# pass in as *** JSON *** document
When as user 'None:None' I <method> 'http://127.0.0.1:8082/formOrJson' with JSON
"""
{"first":"Jane","last":"Doeovskaya"}
......@@ -52,6 +54,52 @@ Feature: Arguments
Then I expect HTTP code <code>
And I expect content contains 'Jane Doeovskaya'
# pass in as *** YAML *** document
When as user 'None:None' I <method> 'http://127.0.0.1:8082/formOrJson' with YAML
"""
first: Oksana
last: Dolovskaya
"""
Then I expect HTTP code <code>
And I expect content contains 'Oksana Dolovskaya'
# additional arguments should be OK
When as user 'None:None' I <method> 'http://127.0.0.1:8082/formOrJson' with YAML
"""
first: Svetlana
middle: Jane
last: Gingrychnoya
"""
Then I expect HTTP code <code>
And I expect content contains 'Svetlana Gingrychnoya'
# pass in as *** XML *** document wit both attributes and child nodes
When as user 'None:None' I <method> 'http://127.0.0.1:8082/formOrJson' with XML
"""
<root first="John" last="Doe" middle="Jim"/>
"""
Then I expect HTTP code <code>
And I expect content contains 'John Doe'
When as user 'None:None' I <method> 'http://127.0.0.1:8082/formOrJson' with XML
"""
<root first="Jan" middle="Jim">
<last>Dolowski</last>
</root>
"""
Then I expect HTTP code <code>
And I expect content contains 'Jan Dolowski'
When as user 'None:None' I <method> 'http://127.0.0.1:8082/formOrJson' with XML
"""
<root>
<first>Grzegorz</first>
<middle>Jim</middle>
<last>Brzeczyszczykiewicz</last>
</root>
"""
Then I expect HTTP code <code>
And I expect content contains 'Grzegorz Brzeczyszczykiewicz'
Examples:
| method | code |
| POST | 201 |
......
......@@ -29,4 +29,8 @@ def checkExpectedInterfaces(objects,expectedInterface):
if not expectedInterface.providedBy(obj):
raise RuntimeError("Object %s does not implement %s interface" % (obj,expectedInterface))
def safeDictUpdate(dictObject,key,value):
"""Only adds a key to a dictionary. If key exists, it leaves it untouched"""
if key not in dictObject:
dictObject[key] = value
\ 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