Quickstart

Step One: Declare your resource

# note/client.py
from nap.resources import ResourceModel, Field


class Note(ResourceModel):

    pk = Field(api_name='id', resource_id=True)
    title = Field()
    content = Field()

    class Meta:
        root_url = 'http://127.0.0.1:8000/api/'
        resource_name = 'note'

Step Two: Access your api

from note.client import Note

n = Note(title='Some Title', content="some content")

# POST http://127.0.0.1:8000/api/note/
n.save()

n = Note.objects.get('note/1/')
# Some Title
n.title

# GET http://127.0.0.1:8000/api/note/1/
n = Note.objects.lookup(pk=1)
n.title = "New Title"
n.content = "I sure do love butterflies"

# PUT http://127.0.0.1:8000/api/note/1/
n.save()

n = Note.objects.get('note/1/')
# "New Title"
n.title

Step Three: Set up custom lookup_urls

from nap.resources import ResourceModel, Field
from nap.lookup import nap_url

class Note(ResourceModel):

    pk = Field(api_name='id', resource_id=True)
    title = Field()
    content = Field()

    class Meta:
        root_url = 'http://127.0.0.1:8000/api/'
        resource_name = 'note'
        additional_urls = (
            nap_url(r'%(resource_name)s/title/%(title)s/'),
        )

# GET http://127.0.0.1:8000/api/note/title/butterflies/
n = Note.objects.lookup(title='New Title')
# "I sure do love butterflies"
n.content

Step Four: What’s next?

  • Learn more about tweaking ResourceModel by looking at Tutorial: Part 1

  • Learn about LookupURLs, the glue between your resource and its API

  • Look deeper into the core modules behind nap:
    • ResourceModel API, The Pythonic representation of your resource.
    • engine, all the HTTP nuts-and-bolts powering nap.