Skip to main content

A lightweight REST miniframework for Python.

Project description

https://travis-ci.org/toastdriven/restless.png?branch=master https://coveralls.io/repos/toastdriven/restless/badge.png

A lightweight REST miniframework for Python.

Documentation is at http://restless.readthedocs.org/.

Works great with Django, Flask, Pyramid, Tornado & Itty, but should be useful for many other Python web frameworks. Based on the lessons learned from Tastypie & other REST libraries.

Features

  • Small, fast codebase

  • JSON output by default, but overridable

  • RESTful

  • Python 3.3+ (with shims to make broke-ass Python 2.6+ work)

  • Flexible

Anti-Features

(Things that will never be added…)

  • Automatic ORM integration

  • Authorization (per-object or not)

  • Extensive filtering options

  • XML output (though you can implement your own)

  • Metaclasses

  • Mixins

  • HATEOAS

Why?

Quite simply, I care about creating flexible & RESTFul APIs. In building Tastypie, I tried to create something extremely complete & comprehensive. The result was writing a lot of hook methods (for easy extensibility) & a lot of (perceived) bloat, as I tried to accommodate for everything people might want/need in a flexible/overridable manner.

But in reality, all I really ever personally want are the RESTful verbs, JSON serialization & the ability of override behavior.

This one is written for me, but maybe it’s useful to you.

Manifesto

Rather than try to build something that automatically does the typically correct thing within each of the views, it’s up to you to implement the bodies of various HTTP methods.

Example code:

# posts/api.py
from django.contrib.auth.models import User

from restless.dj import DjangoResource
from restless.preparers import FieldsPreparer

from posts.models import Post


class PostResource(DjangoResource):
    # Controls what data is included in the serialized output.
    preparer = FieldsPreparer(fields={
        'id': 'id',
        'title': 'title',
        'author': 'user.username',
        'body': 'content',
        'posted_on': 'posted_on',
    })

    # GET /
    def list(self):
        return Post.objects.all()

    # GET /pk/
    def detail(self, pk):
        return Post.objects.get(id=pk)

    # POST /
    def create(self):
        return Post.objects.create(
            title=self.data['title'],
            user=User.objects.get(username=self.data['author']),
            content=self.data['body']
        )

    # PUT /pk/
    def update(self, pk):
        try:
            post = Post.objects.get(id=pk)
        except Post.DoesNotExist:
            post = Post()

        post.title = self.data['title']
        post.user = User.objects.get(username=self.data['author'])
        post.content = self.data['body']
        post.save()
        return post

    # DELETE /pk/
    def delete(self, pk):
        Post.objects.get(id=pk).delete()

Hooking it up:

# api/urls.py
from django.conf.urls.default import url, patterns, include

from posts.api import PostResource

urlpatterns = patterns('',
    # The usual suspects, then...

    url(r'^api/posts/', include(PostResource.urls())),
)

Licence

BSD

Running the Tests

Getting the tests running looks like:

$ virtualenv -p python3 env3
$ . env3/bin/activate
$ pip install -r test3_requirements.txt
$ export PYTHONPATH=`pwd`
$ py.test -s -v --cov=restless --cov-report=html tests

For Python 2:

$ virtualenv env2
$ . env2/bin/activate
$ pip install -r test2_requirements.txt
$ export PYTHONPATH=`pwd`
$ py.test -s -v --cov=restless --cov-report=html tests

Coverage is at about 94%, so please don’t make it worse. :D

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

restless-2.0.1.tar.gz (307.5 kB view details)

Uploaded Source

Built Distribution

restless-2.0.1-py2.py3-none-any.whl (21.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file restless-2.0.1.tar.gz.

File metadata

  • Download URL: restless-2.0.1.tar.gz
  • Upload date:
  • Size: 307.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for restless-2.0.1.tar.gz
Algorithm Hash digest
SHA256 4d100f505944c2f07c03960556c5431c5edad4aaced43f4239a1346570c654a5
MD5 303b14ce5d8ce25272cb94a3cae474a3
BLAKE2b-256 14ead28fc9441d1542cbf107c5b45d2b014e0e47b87bee5aec0a14dd4b59a103

See more details on using hashes here.

File details

Details for the file restless-2.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for restless-2.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2532748510c494304553bcd6b0d04095dd6a8800f724e1172d2b1e4fc68a2584
MD5 cdad73b941735f00d08508fbca979b24
BLAKE2b-256 8cc17b1f2f4d07d198210488e96e94f24128c61bb58277cdbf45702a5a882ab2

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