Commit cd0e0bc8 authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

work on json/xml parsing

parent 21de6ca2
...@@ -11,6 +11,10 @@ class Http: ...@@ -11,6 +11,10 @@ class Http:
PUT = "PUT" PUT = "PUT"
DELETE = "DELETE" DELETE = "DELETE"
class HttpHeader:
"""Enumerates common HTTP headers"""
CONTENT_TYPE = "content-type"
class MediaType: class MediaType:
"""Enumerates media types""" """Enumerates media types"""
WILDCARD = "*/*" WILDCARD = "*/*"
...@@ -25,3 +29,4 @@ class MediaType: ...@@ -25,3 +29,4 @@ class MediaType:
TEXT_PLAIN = "text/plain" TEXT_PLAIN = "text/plain"
TEXT_XML = "text/xml" TEXT_XML = "text/xml"
TEXT_HTML = "text/html" TEXT_HTML = "text/html"
TEXT_YAML = "text/yaml"
\ No newline at end of file
...@@ -3,7 +3,7 @@ Stand-alone security module with support for role-based authorization and IP add ...@@ -3,7 +3,7 @@ Stand-alone security module with support for role-based authorization and IP add
@author: jacekf @author: jacekf
''' '''
from IPy import IP #from IPy import IP
class SecurityRole: class SecurityRole:
'''Represents a security role''' '''Represents a security role'''
......
...@@ -4,7 +4,7 @@ Main server classes ...@@ -4,7 +4,7 @@ Main server classes
@author: jacekf @author: jacekf
''' '''
from collections import defaultdict from collections import defaultdict
from corepost.enums import Http from corepost.enums import Http, HttpHeader
from corepost.utils import getMandatoryArgumentNames from corepost.utils import getMandatoryArgumentNames
from enums import MediaType from enums import MediaType
from formencode import FancyValidator, Invalid from formencode import FancyValidator, Invalid
...@@ -12,9 +12,8 @@ from twisted.internet import reactor, defer ...@@ -12,9 +12,8 @@ from twisted.internet import reactor, defer
from twisted.web.http import parse_qs from twisted.web.http import parse_qs
from twisted.web.resource import Resource from twisted.web.resource import Resource
from twisted.web.server import Site, NOT_DONE_YET from twisted.web.server import Site, NOT_DONE_YET
import re import re, copy, exceptions, json
import copy from xml.etree import ElementTree
import exceptions
class RequestRouter: class RequestRouter:
''' 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 '''
...@@ -208,7 +207,7 @@ class CorePost(Resource): ...@@ -208,7 +207,7 @@ class CorePost(Resource):
allargs[arg] = requestargs[arg][0] allargs[arg] = requestargs[arg][0]
# if POST/PUT, check if we need to automatically parse JSON # if POST/PUT, check if we need to automatically parse JSON
# TODO self.__parseRequestData(request)
#handle Deferreds natively #handle Deferreds natively
try: try:
...@@ -241,6 +240,20 @@ class CorePost(Resource): ...@@ -241,6 +240,20 @@ class CorePost(Resource):
request.setHeader("content-type", MediaType.TEXT_PLAIN) request.setHeader("content-type", MediaType.TEXT_PLAIN)
return message return message
def __parseRequestData(self,request):
'''Automatically parses JSON,XML,YAML if present'''
if HttpHeader.CONTENT_TYPE in request.headers.keys():
type = request.headers["content-type"]
if type == MediaType.APPLICATION_JSON:
request.json = json.loads(request.content.read())
pass
elif type in (MediaType.APPLICATION_XML,MediaType.TEXT_XML):
request.xml = ElementTree.XML(request.content.read())
pass
elif type == MediaType.TEXT_YAML:
#TODO: parse YAML
pass
def run(self,port=8080): def run(self,port=8080):
"""Shortcut for running app within Twisted reactor""" """Shortcut for running app within Twisted reactor"""
factory = Site(self) factory = Site(self)
......
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