This repository was archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
URL Routing
Maigret Aurélien edited this page Mar 17, 2015
·
7 revisions
For add a new API route, you must create a new class herited from rest_api.API.API:
from rest_api.API import API
class ProjectAPI(API):
passAfter this, you must register this API for use the routing of Django:
# -*- coding: utf-8 -*-
from django.conf.urls import patterns, url, include
from rest_api.register import RegisterAPI
from example.api_v1.project import ProjectAPI
api_v1 = RegisterAPI("1.0") # Version of your API
api_v1.register(ProjectAPI()) # /api/1.0/project
urlpatterns = patterns(
'example',
url(r'^api/', include(api_v1.urls))
)At this point, Django listen in:
Type list: /api/<version_api>/<name_api>
Type detail: /api/<version_api>/<name_api>/<id>
-
version_api: Name or version of your API, this is the parameter of
RegisterAPI. - name_api: Name of the API, retrieve automatically in function of the name of your class (here "ProjectAPI", so this is "project").
After, you must create one method by route and method request, in this format:
method_<method>_<type>
- method: get, post, delete, and put are allowed.
- type: Type seen previously (list or detail). For the type detail, the id is send in parameter (called _id).
For example:
from rest_api.API import API
class ProjectAPI(API):
# GET /api/1.0/project
def method_get_list(self, session, request, **kwargs):
# ...
# GET /api/1.0/project/<id>
def method_get_detail(self, session, request, _id, **kwargs):
# ...
# POST /api/1.0/project
def method_post_list(self, session, request, data, **kwargs):
# ...
# PUT /api/1.0/project/<id>
def method_put_detail(self, session, request, _id, data, **kwargs):
# ...
# DELETE /api/1.0/project/<id>
def method_delete_detail(self, session, request, _id, **kwargs):
# ...- prepend_url
- _id2
- example