Add support for manually specifying supported content types in @wsmeexpose.

Closes-bug: #1271317
Change-Id: Ia28a912f4444a6ff77b1feaf2ea6440b0c738e86
This commit is contained in:
Ryan Petrello 2014-09-23 13:23:48 -04:00
parent 7cee58bce9
commit 26a6acdadd
5 changed files with 44 additions and 6 deletions

View File

@ -71,6 +71,11 @@ class Criterion(Base):
class AuthorsController(RestController):
_custom_actions = {
'json_only': ['GET'],
'xml_only': ['GET']
}
books = BooksController()
@wsmeext.pecan.wsexpose([Author], [six.text_type], [Criterion])
@ -129,3 +134,11 @@ class AuthorsController(RestController):
@wsmeext.pecan.wsexpose(Book, int, body=Author)
def put(self, author_id, author=None):
return author
@wsmeext.pecan.wsexpose([Author], rest_content_types=('json',))
def json_only(self):
return [Author(id=1, firstname=u"aname", books=[])]
@wsmeext.pecan.wsexpose([Author], rest_content_types=('xml',))
def xml_only(self):
return [Author(id=1, firstname=u"aname", books=[])]

View File

@ -194,6 +194,24 @@ class TestWS(FunctionalTest):
assert a['faultcode'] == 'Server'
assert a['debuginfo'].startswith('Traceback (most recent call last):')
def test_json_only(self):
res = self.app.get('/authors/json_only.json')
assert res.status_int == 200
body = json.loads(res.body.decode('utf-8'))
assert len(body) == 1
assert body[0]['firstname'] == u"aname"
assert body[0]['books'] == []
assert body[0]['id'] == 1
res = self.app.get('/authors/json_only.xml', expect_errors=True)
def test_xml_only(self):
res = self.app.get('/authors/xml_only.xml')
assert res.status_int == 200
assert '<id>1</id>' in res.body.decode('utf-8')
assert '<firstname>aname</firstname>' in res.body.decode('utf-8')
assert '<books />' in res.body.decode('utf-8')
res = self.app.get('/authors/xml_only.json', expect_errors=True)
def test_body_parameter(self):
res = self.app.put(
'/authors/1/books/2.json',

View File

@ -112,10 +112,11 @@ class FunctionDefinition(object):
arg.resolve_type(registry)
def set_options(self, body=None, ignore_extra_args=False, status_code=200,
**extra_options):
rest_content_types=('json', 'xml'), **extra_options):
self.body_type = body
self.status_code = status_code
self.ignore_extra_args = ignore_extra_args
self.rest_content_types = rest_content_types
self.extra_options = extra_options
def set_arg_types(self, argspec, arg_types):

View File

@ -116,9 +116,11 @@ def wsexpose(*args, **kwargs):
result=result
)
pecan_xml_decorate(callfunction)
pecan_text_xml_decorate(callfunction)
pecan_json_decorate(callfunction)
if 'xml' in funcdef.rest_content_types:
pecan_xml_decorate(callfunction)
pecan_text_xml_decorate(callfunction)
if 'json' in funcdef.rest_content_types:
pecan_json_decorate(callfunction)
pecan.util._cfg(callfunction)['argspec'] = inspect.getargspec(f)
callfunction._wsme_definition = funcdef
return callfunction

View File

@ -449,7 +449,9 @@ def document_function(funcdef, docstrings=None, protocols=['restjson']):
u'',
])
codesamples.extend((
u' ' * 12 + line for line in sample.split('\n')))
u' ' * 12 + line
for line in six.text_type(sample).split('\n')
))
if funcdef.return_type:
codesamples.extend([
@ -467,7 +469,9 @@ def document_function(funcdef, docstrings=None, protocols=['restjson']):
u'',
])
codesamples.extend((
u' ' * 12 + line for line in sample.split('\n')))
u' ' * 12 + line
for line in six.text_type(sample).split('\n')
))
docstrings[0:0] = [codesamples]
return docstrings