Skip to content

Commit

Permalink
Update the rest-api scaffold to use generic controllers, *not* Rest…
Browse files Browse the repository at this point in the history
…Controller

Closes-Bug #1413038

Change-Id: I6b91479d9af754b1833abf212a20112e8372a948
  • Loading branch information
ryanpetrello committed May 21, 2015
1 parent 26d1d59 commit 5e48da3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
45 changes: 27 additions & 18 deletions pecan/scaffolds/rest-api/+package+/controllers/root.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pecan import expose, response, abort
from pecan.rest import RestController

people = {
1: 'Luke',
Expand All @@ -9,30 +8,40 @@
}


class PeopleController(RestController):
class PersonController(object):

@expose('json')
def get_all(self):
return people

@expose()
def get_one(self, person_id):
return people.get(int(person_id)) or abort(404)
def __init__(self, person_id):
self.person_id = person_id

@expose()
def post(self):
# TODO: Create a new person
response.status = 201
@expose(generic=True)
def index(self):
return people.get(self.person_id) or abort(404)

@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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 5e48da3

Please sign in to comment.