User Tools

Site Tools


soft:server-apps-apis

Apis application

This application provides support for building APIs in a pluggable way. So far only has support for REST-style APIs.

In order to be automatically handled for this application, each app should provide its part of the API implementation in its own api.py module. More descriptive examples of how to register REST-style resources are provided on the following section.

Resource registration pattern

apis.rest module provides support for pluggable resources on the REST API.

  1. It takes care of the url management:
    # controller/urls.py
    from apis import rest
    ...
    rest.api.autodiscover()
    
    urlpatterns = patterns('',
        url(r'^admin/', include(admin.site.urls)),
        url(r'^api/', include(rest.api.urls)),
    )
  2. Automatically find resources registred on app/api.py modules. An examples:
    # nodes/api.py
    from apis import rest
    
    class NodeList(generics.ListCreateAPIView):
        model = Node
        serializer_class = NodeSerializer
    
    class NodeDetail(generics.RetrieveUpdateDestroyAPIView):
        model = Node
        serializer_class = NodeSerializerapi.register(NodeResource)
    
    # for each resource you have to provide a ListView and a DetailView:
    rest.api.register(NodeList, NodeDetail)
  3. Aggregate new fields to existing serializers (useful for Tinc pluggable app):
    # tinc/serializers.py
    from apis import rest
    class TincClientSerializer(serializers.ModelSerializer):
        connect_to = TincAddressSerializer()
        island = IslandSerializer()
        name = serializers.CharField()
        
        class Meta:
            model = TincClient
            exclude = ('object_id', 'content_type', 'id')
    
    rest.api.aggregate(Server, TincClientSerializer, name='tinc')
  4. Automatically generates the Base resource which basically lists all the registred resources.
soft/server-apps-apis.txt · Last modified: 2012/10/16 15:46 by marcay