Skip to main content

Pyramid RESTful Framework is designed to help coding REST CRUD endpoints with couple of lines of code.

Project description

Pyramid RESTful Framework is designed to help coding REST CRUD endpoints with couple of lines of code.

Setup.

1. virtualenv myapp
2. pip install git+https://github.com/vahana/prf
3. pcreate -s starter myapp
4. cd myapp
5. pip install -e .

Run.

pserve development.ini

This will run a server and you can navigate your browser to http://0.0.0.0:6543


Adding prf resources.

In the __init__.main function of your pyramid app declare your resources:

def main():
...
config.include('prf')
root = config.get_root_resource()
user = root.add('user', view='prf.view.NoOp')
user_story = user.add('story', 'stories', view='prf.view.NoOp')
...

The following endpoints are declared with the code above:
/users/{id}
/users/{user_id}/stories/{id}

You can now navigate to http://0.0.0.0:6543/users or http://0.0.0.0:6543/users/1/stories

'NoOp' view as name suggests does not do much. You will need to create your own views for each resource.
In our case UsersView and UserStoriesView.

UsersView could look something like this:

from prf.view import BaseView

Users = [
{
'id': 0,
'name':'Alice',
},
{
'id': 1,
'name':'Bob',
},
{
'id': 2,
'name':'Katy',
},
]

class UsersView(BaseView):

def index(self):
return Users

def show(self, id):
return Users[int(id)]

def create(self):
Users.update(**self._params)

def delete(self, id):
del Users[id]

You need to change the view for the users resource to point to this class:
user = root.add('user', view=UsersView)

Restart the server and navigate to http://0.0.0.0:6543/users

Above we declared `index`, `show`, `create` and `delete` actions which correspond to: GET collection, GET resource, POST resource and DELETE resource respectively. You could also declare `update`, which would correspond to the PUT method. You dont need to declare all of them, only those you need. The missing ones will automatically return 405 Method Not Allowed error.

Comment out the `index` action and try.

Happy RESTing !



0.0
---

- Initial version


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prf-0.0.22.tar.gz (44.1 kB view details)

Uploaded Source

Built Distribution

prf-0.0.22-py2-none-any.whl (57.4 kB view details)

Uploaded Python 2

File details

Details for the file prf-0.0.22.tar.gz.

File metadata

  • Download URL: prf-0.0.22.tar.gz
  • Upload date:
  • Size: 44.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for prf-0.0.22.tar.gz
Algorithm Hash digest
SHA256 c0ea291799613def308a8a33231a83934a0326576c11404c603ea86bbc7ce0e7
MD5 ab649d11d6c8f09698e8575f833d7e66
BLAKE2b-256 462638f280cb24c76039647fd064800fea5c1b90c2fec9b2455d56949a1f6413

See more details on using hashes here.

File details

Details for the file prf-0.0.22-py2-none-any.whl.

File metadata

  • Download URL: prf-0.0.22-py2-none-any.whl
  • Upload date:
  • Size: 57.4 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for prf-0.0.22-py2-none-any.whl
Algorithm Hash digest
SHA256 d81a2c4e57497706077c399afec51d9b6050bec3851c0c257fe2eafb98ab4a00
MD5 1a023e5263377618cb83458fe20a15ee
BLAKE2b-256 ff282f417b3ca9da4c2f2abf525f85b8362584a2595b532d18611771b09225d4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page