derks/drest — Issue #1: Add models.APIModel
Author: derks
Created: 2012-01-31 19:35:38+00:00
State: open

Would be handy to be able to access the api like a model:

myapi = drest.api.API('http://localhost:8000/api/v1/')
myapi.auth(user='john.doe', password='XXXXXXXX')

class UserModel(drest.models.APIModel):
class Meta:
api = myapi
resource = 'users'
path = '/users/'

# New user
user = UserModel()
user.first_name = 'John'
user.last_name = 'Doe'

# POST http://localhost:8000/api/v1/users/
user.save()

### Update user

# GET http://localhost:8000/api/v1/users/?username='john.doe'
user = UserModel(username='john.doe') # exception if more than 1 result, or none

user.first_name
>>> 'john'

user.last_name
>>> 'doe'

user.first_name = 'Bob'

user.first_name
>>> 'Bob'

# PUT http://localhost:8000/api/v1/users/14/
user.save()