Skip to main content
Join the official Python Developers Survey 2018 and win valuable prizes: Start the survey!

RESTful endpoints for aoihttp that bind directly to a model

Project description

aiohttp_rest makes it easy to create RESTful aiohttp endpoints that bind directly to models with minimal modification.

Usage

Create your model:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

Create an aiohttp application that makes use of a aiohttp_rest.RestResource:

from aiohttp.web import Application, run_app
from aiohttp_rest import RestResource

from person import Person


people = {}

app = Application()
person_resource = RestResource('people', people, Person, ('name', 'age'), 'name')
person_resource.register(app.router)

run_app(app)

Interact with the API:

http POST localhost:8000/people name=andrew age=24
http localhost:8000/people/andrew
http PUT localhost:8000/people/andrew/age age=25
http DELETE localhost:8000/people/andrew

Several things are required by the RestResource to make all this work:

  • A name for the resource, this will form the base of the URL.
  • A factory method for the model, as seen above, this can simply be the init method for the model or something more complex.
  • A collection to store the models, this should be a dict-like object.
  • A list of properties to expose to the API.
  • A property to treat as the id for the collection. In the above example we used the name as the id, so that’s what we used in our URLs to refer to a specific instance in the collection.

Installing

git clone github.com/atbentley/aiohttp_rest
cd aiohttp_rest
python setup.py install

Tests

pip install -r build-requirements.txt
py.test tests

License

MIT

Project details


Release history Release notifications

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
aiohttp-rest-0.1.0.tar.gz (6.8 kB) Copy SHA256 hash SHA256 Source None Apr 2, 2016

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page