Creating API endpointsΒΆ

To use this extension, you must have defined your database models using elixir.Entity as a base class.

First, change your model classes to inherit from flask.ext.restless.Entity instead of elixir.Entity:

#from elixir import Entity
from flask.ext.restless import Entity
from elixir import Date, DateTime, Field, Unicode
from elixir import ManyToOne, OneToMany

class Person(Entity):
    name = Field(Unicode, unique=True)
    birth_date = Field(Date)
    computers = OneToMany('Computer')

class Computer(Entity):
    name = Field(Unicode, unique=True)
    vendor = Field(Unicode)
    owner = ManyToOne('Person')
    purchase_time = Field(DateTime)


Attributes of these entities must not have a name containing two underscores. For example, this class definition is no good:

class Person(Entity):
    __mysecretfield = Field(Unicode)

This restriction is necessary because the search feature (see Making search queries) uses double underscores as a separator. This may change in the future.

Second, create your flask.Flask object and instantiate a flask.ext.restless.APIManager object with that Flask:

from flask import Flask
from flask.ext.restless import APIManager

app = Flask(__name__)
manager = APIManager(app)

Third, create the API endpoints which will be accessible to web clients:

person_blueprint = manager.create_api(Person, methods=['GET', 'PATCH',
                                                       'POST', 'DELETE'])
computer_blueprint = manager.create_api(Computer, method=['GET'])

Due to the design of Flask, these APIs must be created before your application handles any requests. The return value of APIManager.create_api() is the blueprint in which the endpoints for the specified database model live. The blueprint has already been registered on the Flask application, so you do not need to register it yourself. It is provided so that you can examine its attributes, but if you don’t need it then just ignore it:

manager.create_api(Person, methods=['GET', 'PATCH', 'POST', 'DELETE'])
manager.create_api(Computer, methods=['GET'])

By default, the API for Person, in the above code samples, will be accessible at http://<host>:<port>/api/person (note the lowercase name of the model in the URL):

>>> import json
>>> import requests  # python-requests is installable from PyPI...
>>> newperson = {'name': u'Lincoln', 'age': 23}
>>> r ='/api/person', data=json.dumps(newperson))
>>> r.status_code, r.headers['content-type'],
(201, 'application/json', '{"id": 1}')
>>> newid = json.loads(['id']
>>> r = requests.get('/api/person/{}'.format(newid))
>>> r.status_code, r.headers['content-type']
(200, 'application/json')
  "other": null,
  "name": "Lincoln",
  "birth_date": null,
  "age": 23.0,
  "computers": [],
  "id": 1

Related Topics

This Page