Commit 65689030 authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

Code is broken now: working on separating router from actual Http Resource, in...

Code is broken now: working on separating router from actual Http Resource, in preparation for ZeroMQ multicore support...
parent d2897764
'''
Common classes
'''
class Response:
"""
Custom response object, can be returned instead of raw string response
"""
def __init__(self,code=200,entity=None,headers={}):
self.code = code
self.entity=entity
self.headers=headers
\ No newline at end of file
This diff is collapsed.
...@@ -186,7 +186,7 @@ total: 4443.52 ...@@ -186,7 +186,7 @@ total: 4443.52
| Unable to convert String response to XML automatically | application/xml | 500 | # not supported yet | Unable to convert String response to XML automatically | application/xml | 500 | # not supported yet
| - {test1: Test1}\n- {test2: Test2} | text/yaml | 200 | | - {test1: Test1}\n- {test2: Test2} | text/yaml | 200 |
@json @yaml @xml @return_accept_deferred @json @yaml @xml @return_accept_deferred @tmp
Scenario Outline: Return content type based on caller's Accept from Deferred methods Scenario Outline: Return content type based on caller's Accept from Deferred methods
When I prepare HTTP header 'Accept' = '<accept>' When I prepare HTTP header 'Accept' = '<accept>'
When as user 'None:None' I GET 'http://127.0.0.1:8080/return/by/accept/deferred' When as user 'None:None' I GET 'http://127.0.0.1:8080/return/by/accept/deferred'
...@@ -196,10 +196,10 @@ total: 4443.52 ...@@ -196,10 +196,10 @@ total: 4443.52
Examples: Examples:
| content | accept | code | | content | accept | code |
| [{"test1": "Test1"}, {"test2": "Test2"}] | application/json | 200 | | [{"test1": "Test1"}, {"test2": "Test2"}] | application/json | 200 |
| Unable to convert String response to XML automatically | application/xml | 500 | # not supported yet #| Unable to convert String response to XML automatically | application/xml | 500 | # not supported yet
| - {test1: Test1}\n- {test2: Test2} | text/yaml | 200 | #| - {test1: Test1}\n- {test2: Test2} | text/yaml | 200 |
@json @yaml @xml @return_accept @json @yaml @xml @return_accept
Scenario Outline: Return class content type based on caller's Accept Scenario Outline: Return class content type based on caller's Accept
When I prepare HTTP header 'Accept' = '<accept>' When I prepare HTTP header 'Accept' = '<accept>'
When as user 'None:None' I GET 'http://127.0.0.1:8080/return/by/accept/class' When as user 'None:None' I GET 'http://127.0.0.1:8080/return/by/accept/class'
...@@ -208,6 +208,6 @@ total: 4443.52 ...@@ -208,6 +208,6 @@ total: 4443.52
Examples: Examples:
| content | accept | code | | content | accept | code |
| is not JSON serializable | application/json | 500 | # not supported yet | [{}, {}] | application/json | 200 | # not supported yet
| Unable to convert String response to XML automatically | application/xml | 500 | # not supported yet | Unable to convert String response to XML automatically | application/xml | 500 | # not supported yet
\ No newline at end of file
...@@ -7,6 +7,7 @@ from corepost.web import CorePost, route ...@@ -7,6 +7,7 @@ from corepost.web import CorePost, route
from corepost.enums import Http, MediaType, HttpHeader from corepost.enums import Http, MediaType, HttpHeader
from twisted.internet import defer from twisted.internet import defer
from xml.etree import ElementTree from xml.etree import ElementTree
from UserDict import UserDict
import json, yaml import json, yaml
class HomeApp(CorePost): class HomeApp(CorePost):
...@@ -86,18 +87,20 @@ class HomeApp(CorePost): ...@@ -86,18 +87,20 @@ class HomeApp(CorePost):
def test_return_content_by_accept_deferred(self,request,**kwargs): def test_return_content_by_accept_deferred(self,request,**kwargs):
"""Ensure support for inline callbacks and deferred""" """Ensure support for inline callbacks and deferred"""
val = yield [{"test1":"Test1"},{"test2":"Test2"}] val = yield [{"test1":"Test1"},{"test2":"Test2"}]
defer.returnValue(val) defer.returnValue(val)
@route("/return/by/accept/class") @route("/return/by/accept/class")
def test_return_class_content_by_accepts(self,request,**kwargs): def test_return_class_content_by_accepts(self,request,**kwargs):
"""Uses Python class instead of dict/list""" """Uses Python class instead of dict/list"""
class Test: pass class Test(UserDict,dict):
pass
t1 = Test() t1 = Test()
t1.test1 = "Test1" t1.test1="Test1"
t2 = Test() t2 = Test()
t2.test2 = "Test2" t2.test2="Test2"
val = [t1,t2] val = [t1,t2]
return val return (t1,t2)
......
...@@ -3,6 +3,7 @@ Various CorePost utilities ...@@ -3,6 +3,7 @@ Various CorePost utilities
''' '''
from inspect import getargspec from inspect import getargspec
import json import json
from corepost.enums import MediaType
def getMandatoryArgumentNames(f): def getMandatoryArgumentNames(f):
'''Returns a tuple of the mandatory arguments required in a function''' '''Returns a tuple of the mandatory arguments required in a function'''
...@@ -22,3 +23,10 @@ def convertToJson(obj): ...@@ -22,3 +23,10 @@ def convertToJson(obj):
return json.dumps(obj) return json.dumps(obj)
except Exception as ex: except Exception as ex:
raise RuntimeError(str(ex)) raise RuntimeError(str(ex))
def applyResponse(self,request,code,headers={"content-type":MediaType.TEXT_PLAIN}):
"""Applies response to current request"""
request.setResponseCode(code)
if headers != None:
for header,value in headers.iteritems():
request.setHeader(header, value)
This diff is collapsed.
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