DataStores

class rest_api_framework.datastore.base.DataStore(ressource_config, model, **options)[source]

define a source of data. Can be anything fron database to other api, files and so one

get(identifier)[source]

Should return a dictionnary representing the ressource matching the identifier or raise a NotFound exception.

Note

Not implemented by base DataStore class

create(data)[source]

data is a dict containing the representation of the ressource. This method should call validate(), create the data in the datastore and return the ressource identifier

Not implemented by base DataStore class

update(obj, data)[source]

should be able to call get() to retreive the object to be updated, validate_fields() and return the updated object

Note

Not implemented by base DataStore class

delete(identifier)[source]

should be able to validate the existence of the object in the ressource and remove it from the datastore

Note

Not implemented by base DataStore class

get_list(offset=None, count=None, **kwargs)[source]

This method is called each time you want a set of data. Data could be paginated and filtered. Should call filter() and return paginate()

Note

Not implemented by base DataStore class

filter(**kwargs)[source]

should return a way to filter the ressource according to kwargs. It is not mandatory to actualy retreive the ressources as they will be paginated just after the filter call. If you retreive the wole filtered ressources you loose the pagination advantage. The point here is to prepare the filtering. Look at SQLiteDataStore.filter for an example.

Note

Not implemented by base DataStore class

paginate(data, offset, count)[source]

Paginate sould return all the object if no pagination options have been set or only a subset of the ressources if pagination options exists.

validate(data)[source]

Check if data send are valid for object creation. Validate Chek that each required fields are in data and check for their type too.

Used to create new ressources

validate_fields(data)[source]

Validate only some fields of the ressource. Used to update existing objects

class rest_api_framework.datastore.simple.PythonListDataStore(ressource_config, model, **options)[source]

Bases: rest_api_framework.datastore.base.DataStore

a datastore made of list of dicts

get(identifier)[source]

return an object matching the uri or None

get_list(offset=0, count=None, **kwargs)[source]

return all the objects. paginated if needed

update(obj, data)[source]

Update a single object

class rest_api_framework.datastore.sql.SQLiteDataStore(ressource_config, model, **options)[source]

Bases: rest_api_framework.datastore.base.DataStore

Define a sqlite datastore for your ressource. you have to give __init__ a data parameter containing the information to connect to the database and to the table.

example:

data={"table": "tweets",
      "name": "test.db"}
model = ApiModel
datastore = SQLiteDataStore(data, **options)

SQLiteDataStore implement a naive wrapper to convert Field types into database type.

  • int will be saved in the database as INTEGER
  • float will be saved in the database as REAL
  • basestring will be saved in the database as TEXT
  • if the Field type is PKField, is a will be saved as PRIMARY KEY AUTOINCREMENT

As soon as the datastore is instanciated, the database is create if it does not exists and table is created too

Note

  • It is not possible to use :memory database either. The connection is closed after each operations
get_connector()[source]

return a sqlite3 connection to communicate with the table define in self.db

filter(**kwargs)[source]

Change kwargs[“query”] with “WHERE X=Y statements”. The filtering will be done with the actual evaluation of the query in paginate() the sql can then be lazy

paginate(data, **kwargs)[source]

paginate the result of filter using ids limits. Obviously, to work properly, you have to set the start to the last ids you receive from the last call on this method. The max number of row this method can give back depend on the paginate_by option.

get_list(**kwargs)[source]

return all the objects, paginated if needed, fitered if filters have been set.

get(identifier)[source]

Return a single row or raise NotFound

create(data)[source]

Validate the data with base.DataStore.validate() And, if data is valid, create the row in database and return it.

update(obj, data)[source]

Retreive the object to be updated (get() will raise a NotFound error if the row does not exist)

Validate the fields to be updated and return the updated row

delete(identifier)[source]

Retreive the object to be updated

(get() will raise a NotFound error if the row does not exist)

Return None on success, Raise a 400 error if foreign key constrain prevent delete.