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