Skip to main content

PRF is designed to help coding RESTful endpoints with minimal code

Project description

Pyramid RESTful Framework (PRF) is designed to help coding RESTful endpoints with minimal code. It takes care of lots of reasonable defaults and boilerplate code.

Setup.

First, lets install pyramid and create an app:

virtualenv myapp
pip install pyramid
pcreate -s starter myapp
pip install -e .

Now if we run

pserve development.ini

and navigate to http://localhost:6543 we will see the standard pyramid app. Boring.

Lets install httpie to use it for doing requests to our endpoints. Feel free to use curl or any other http client as long as it supports CRUDs.

And lets add prf to the mix!

pip install git+https://github.com/vahana/prf

And add resources.

Modify __init__.main function of myapp to look like:

def main(global_config, **settings):
    config = Configurator(settings=settings)

    config.include('prf') #pyramid way of adding external packages.
    root = config.get_root_resource() #acquire root resource.
    user = root.add('user', 'users', view='prf.view.NoOp') # declare `users` root resource
    user_story = user.add('story', 'stories', view='prf.view.NoOp') # declare `nested resource `users/stories`

    #per pyramid, must return wsgi app
    return config.make_wsgi_app()

The following endpoints are declared with the code above:

users/{id}
users/{user_id}/stories/{id}

Try these:

# will get all declared resources
http 0.0.0.0:6543/_
# will get users
http 0.0.0.0:6543/users
will get stories for a user with id 1
http 0.0.0.0:6543/users/1/stories

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

Lets modify views.py to add the following:

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):
    Users.pop(int(id))

We need to change the view argument for the users resource to point to our new class in the main:

user = root.add('user', view='myapp.views.UsersView')

Restart the server and try:

# list users
http 0.0.0.0:6543/users
# delete a user with id 1
http DELETE 0.0.0.0:6543/users/1
# user 1 is gone
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:

http 0.0.0.0:6543/users

Happy RESTing !

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-1.0.2.tar.gz (46.4 kB view details)

Uploaded Source

Built Distribution

prf-1.0.2-py3-none-any.whl (57.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: prf-1.0.2.tar.gz
  • Upload date:
  • Size: 46.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.6

File hashes

Hashes for prf-1.0.2.tar.gz
Algorithm Hash digest
SHA256 c9ec9da49fb31e4a4c7b875530e576fa93ae884b337f7d6616d831b268fc38c6
MD5 6c381974062e9160b5bbe787fd70826a
BLAKE2b-256 b7c6ad6ad9eba9527caf8b88cd879a61dbbfbf66c3cc14b59f034b1d114cba62

See more details on using hashes here.

File details

Details for the file prf-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: prf-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 57.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.6

File hashes

Hashes for prf-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0224c04be324f41d1a20be6a323682e66005ff2b16fe070ed90a13a4fbf91ec6
MD5 c5858acb7a2b5f5541384b7a49b898da
BLAKE2b-256 d78edc318a4da5fc74a7003160317a9cbe4c3265784ccb15d3c0b9810de701a6

See more details on using hashes here.

Supported by

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