Skip to main content

RestORM allows you to interact with resources as if they were objects.

Project description

RestORM

RestORM allows you to interact with resources as if they were objects (object relational mapping), mock entire webservices and incorporate custom client logic.

Description

Most RESTful webservices are very easy to access with very little code. RestORM is just a small layer on top of httplib2.Http to get a response from a webservice. However, instead of regular Python dict objects, you’ll get a dict-like object that knows how to access related resources as well.

Until a version 1.0 release, backwards incompatible changes may be introduced in future 0.x versions.

Features

  • Object relational mapping of webservice resources.

  • Flexible client architecture that can be used with your own or third party clients (like oauth).

  • Extensive mocking module allows you to mock webservice responses, or even complete webservices.

Getting started

Create a mock webservice

In order to test your client, you can emulate a whole webservice using the MockApiClient. However, sometimes it’s faster or easier to use a single, predefined response, using the MockClient and MockResponse (sub)classes.

You can also use FileResponse class to return the contents of a file as response in combination with the MockApiClient.

The mock webservice below contains a list of books and a list of authors. To keep it simple, both lists contain only 1 item

from restorm.clients.mockclient import MockApiClient

client = MockApiClient(
    responses={
        '/api/book/': {
            'GET': ({'Status': 200}, [{'id': 1, 'name': 'Dive into Python', 'resource_url': 'http://www.example.com/api/book/1'}]),
            'POST': ({'Status': 201, 'Location': 'http://www.example.com/api/book/2'}, ''),
        },
        '/api/book/1': {'GET': ({'Status': 200}, {'id': 1, 'name': 'Dive into Python', 'author': 'http://www.example.com/api/author/1'})},
        '/api/author/': {'GET': ({'Status': 200}, [{'id': 1, 'name': 'Mark Pilgrim', 'resource_url': 'http://www.example.com/api/author/1'}])},
        '/api/author/1': {'GET': ({'Status': 200}, {'id': 1, 'name': 'Mark Pilgrim'})}
    },
    root_uri='http://www.example.com'
)

Define resources

Setup your client side resource definitions:

from restorm.resource import Resource

class Book(Resource):
    class Meta:
        list = (r'^book/$', 'book_set')
        item = r'^book/(?P<id>\d)$'
        root = 'http://www.example.com/api/'

Make it work

You can simply access the Book resource:

>>> book = Book.objects.get(id=1, client=client) # Get book with ID 1.
>>> book.data['name'] # Get the value of the key "name".
u'Dive Into Python'
>>> book.data['author'] # Get the value of the key "author".
u'http://www.example.com/api/author/1'
>>> author = book.data.author # Perform a GET on the "author" resource.
>>> author.data['name']
u'Mark Pilgrim'

Contribute

  1. Get the code from Github:

    $ git clone git://github.com/joeribekker/restclient.git
  2. Create and activate a virtual environment:

    $ cd restclient
    $ virtualenv .
    $ source bin/activate
  3. Setup the project for development:

    $ python setup.py develop
  4. Start hacking!

Testing

RestORM has a whooping 90% test coverage. Although reaching 100% is not a goal by itself, I consider unit testing to be essential during development.

Performing the unit tests yourself:

python setup.py test

Changes

0.1

November 9, 2012

  • Initial version released on PyPI.

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

restorm-0.1.tar.gz (23.6 kB view details)

Uploaded Source

File details

Details for the file restorm-0.1.tar.gz.

File metadata

  • Download URL: restorm-0.1.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for restorm-0.1.tar.gz
Algorithm Hash digest
SHA256 9a6b2511750d4ea3afb8b7645603334540d6884b4ac4b3df66e56bc09402f27f
MD5 3a36be3939e0129a33df3ad9e68f275a
BLAKE2b-256 009d9938841aac72a1c3f1c8feba8fc48ba1975d8e68865d18a72afd1e4ffc21

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