Update the rest-api scaffold to use generic controllers, *not* RestController
Closes-Bug #1413038 Change-Id: I6b91479d9af754b1833abf212a20112e8372a948
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
from pecan import expose, response, abort
|
from pecan import expose, response, abort
|
||||||
from pecan.rest import RestController
|
|
||||||
|
|
||||||
people = {
|
people = {
|
||||||
1: 'Luke',
|
1: 'Luke',
|
||||||
@@ -9,30 +8,40 @@ people = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class PeopleController(RestController):
|
class PersonController(object):
|
||||||
|
|
||||||
@expose('json')
|
def __init__(self, person_id):
|
||||||
def get_all(self):
|
self.person_id = person_id
|
||||||
return people
|
|
||||||
|
|
||||||
@expose()
|
@expose(generic=True)
|
||||||
def get_one(self, person_id):
|
def index(self):
|
||||||
return people.get(int(person_id)) or abort(404)
|
return people.get(self.person_id) or abort(404)
|
||||||
|
|
||||||
@expose()
|
@index.when(method='PUT')
|
||||||
def post(self):
|
def put(self):
|
||||||
# TODO: Create a new person
|
|
||||||
response.status = 201
|
|
||||||
|
|
||||||
@expose()
|
|
||||||
def put(self, person_id):
|
|
||||||
# TODO: Idempotent PUT (returns 200 or 204)
|
# TODO: Idempotent PUT (returns 200 or 204)
|
||||||
response.status = 204
|
response.status = 204
|
||||||
|
|
||||||
@expose()
|
@index.when(method='DELETE')
|
||||||
def delete(self, person_id):
|
def delete(self):
|
||||||
# TODO: Idempotent DELETE
|
# TODO: Idempotent DELETE
|
||||||
response.status = 200
|
response.status = 204
|
||||||
|
|
||||||
|
|
||||||
|
class PeopleController(object):
|
||||||
|
|
||||||
|
@expose()
|
||||||
|
def _lookup(self, person_id, *remainder):
|
||||||
|
return PersonController(int(person_id)), remainder
|
||||||
|
|
||||||
|
@expose(generic=True, template='json')
|
||||||
|
def index(self):
|
||||||
|
return people
|
||||||
|
|
||||||
|
@index.when(method='POST', template='json')
|
||||||
|
def post(self):
|
||||||
|
# TODO: Create a new person
|
||||||
|
response.status = 201
|
||||||
|
|
||||||
|
|
||||||
class RootController(object):
|
class RootController(object):
|
||||||
|
|||||||
@@ -22,11 +22,11 @@ class TestRootController(FunctionalTest):
|
|||||||
assert response.status_int == 201
|
assert response.status_int == 201
|
||||||
|
|
||||||
def test_put(self):
|
def test_put(self):
|
||||||
response = self.app.put('/people/1')
|
response = self.app.put('/people/1/')
|
||||||
assert response.status_int == 204
|
assert response.status_int == 204
|
||||||
|
|
||||||
def test_delete(self):
|
def test_delete(self):
|
||||||
response = self.app.delete('/people/1')
|
response = self.app.delete('/people/1/')
|
||||||
assert response.status_int == 204
|
assert response.status_int == 204
|
||||||
|
|
||||||
def test_not_found(self):
|
def test_not_found(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user