koleS
koleS

Reputation: 1313

REST app architecture

I am working on a RESTful application, which goal is to allow user to track his results, progress and performance in sport activities.

I want to create two or more clients for this app:

1) web client

2) mobile client ( iOS / Android )

I am writting it in django using tastypie app and I wonder if I should make web client in the same application, which will provide the RESTful api or should I leave it as a pure REST service and build separate web client, which will contact it via the api?

As for now I don't see any drawbacks of making both in one, but I am not an expert in programs with such an architecture, so I am looking for some advise with argumentation behind it.

Upvotes: 1

Views: 1321

Answers (3)

kunl
kunl

Reputation: 1195

There is a fourth way to do this, which extends on @seppo-erviälä Approach 1 and Approach 2:

Approach 4: API View + Django View via Handler

Create a handler that returns the RESTful resource just as RESTful API view normally would. But this handler is callable from anywhere. It gets the same request dictionary that the view gets and it returns the same JSON the view returns. So now, the architecture is:

          Handler
         /   |   \
        /    |    \
       /     |     \
      /      |      \
RESTfulView  |    Normal Django View
             |
       Anything Else

The handler:

class ResourceHandler:

    def create_resource(self, data):
        # code

    def fetch_resource(self, rId):
        # code

    # and so on

and you call it from the views like so:

# /views/restfulview.py
# using django-rest-framework
from rest_framework.response import Response

class RESTCallView(APIView):

    h = ResourceHandler()

    def get(self, request, rId):
        return Response(self.h.fetch_resource(rId))


# /views/normalview.py
from django.views.generic.base import TemplateView

class SomeDjangoView(TemplateView):

    h = ResourceHandler()

    def get(self, request, rId):
        return HttpResponse(self.h.fetch_resource(rId))

Of course, this is just example code and not really pythonic but you get the idea.
I work in a large e-commerce company and some of my teammates have used this approach to good success.
Some other advantages are:

  • Migration of old data becomes a lot easier now since you just need to create the dict and send it to the Handler. Similarly, sync also becomes a lot easier.
  • Changes to APIs are in one place, and you can cut off the app without killing access to the data.

Hope this helps you too.. :)

Upvotes: 1

Seppo Erviälä
Seppo Erviälä

Reputation: 7460

It is not easy to answer to this as it depends a lot on what kind of service you are building.

Approach 1: Traditional Django app + API

Here your Django-app and the tastpie API share common data models but present them differently. One using Djangos templates and views and the other using tastypie.

Pros:

  • Building a traditinal web service is relatively easy and well understood problem
    • Django provides a lot of tools for this

Cons:

  • There is no gurantee that the API presents the same functionality as the web service
  • You have to maintain two different ways to interact with your data.

Approach 2: API only + JavaScript webapp that uses the API

There is only one interface to the service via the tastypie API. The web client is built separately using javascript tools like backbone.js and backbone-tastypie.

Pros:

  • You gurantee that the 3rd party developers can build a service with the same functionality as your web service (see dogfooding).
  • Works pretty nicely if your service is more of an application than a collection of pages.

Cons:

  • Client side JavaScript tools are not as good as Djangos (for example, templating).
  • Client side rendering of templates only happens after most of the resources are loaded.
    • First pageload is slow
  • Pre-IE9 browsers won't work without trick, IE9 may need tricks
  • You really need to mind about browser caches
  • SEO is not as straightforward as with traditional web service.

Approach 3: API only + call the API from Django views

Pretty much same as Approach 1 but instead of using your models directly you call the tastypie resources internally.

Pros:

  • You can still use most of the Django tools.
  • You mostly use the same API as potential 3rd party developers

Cons:

  • I do not know how much overhead this incurs.

Upvotes: 7

Praveen Prajapati
Praveen Prajapati

Reputation: 989

Better create a pure REST service and consume it from both the client. It will provide a clean layered architecture as you are not mixing service with client in one app. By having a common service separately you would have : Separation of concern, Clean Architecture, Proper Laying, Readability and better Maintainability.

Upvotes: 0

Related Questions