Skip to main content

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 !

Metadata

Release files for prf 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prf 1.0.2
File Size Uploaded Provenance
prf-1.0.2.tar.gz 46.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for prf 1.0.2
File Interpreter ABI Platform
prf-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 103.5 kB

Release files / prf-1.0.2.tar.gz

Download URL prf-1.0.2.tar.gz
Size 46.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c9ec9da49fb31e4a4c7b875530e576fa93ae884b337f7d6616d831b268fc38c6
BLAKE2b-256 checksum
How to use checksums
b7c6ad6ad9eba9527caf8b88cd879a61dbbfbf66c3cc14b59f034b1d114cba62
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / prf-1.0.2-py3-none-any.whl

Download URL prf-1.0.2-py3-none-any.whl
Size 57.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0224c04be324f41d1a20be6a323682e66005ff2b16fe070ed90a13a4fbf91ec6
BLAKE2b-256 checksum
How to use checksums
d78edc318a4da5fc74a7003160317a9cbe4c3265784ccb15d3c0b9810de701a6
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for prf 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page