3

My project

I have a backend web application written with Django and a mobile application written with HTML/CSS/jQuery and wrapped in PhoneGap. I need the mobile app to communicate with the web app and I first thought I would build my own REST API.


My understanding

WSGI is a specification (based on HTTP) that defines how to handle communications between a web server and a Python application, basically how to translate HTTP requests into Python objects and vice versa. All we need to provide to the WSGI server (gunicorn for instance) is a WSGI application. In Django we will typically have a wsgi.py file at the root of the project with

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

So Django has a built-in support for the WSGI specification and we, programmers, do not even have to think about how our application interfaces with the web server. Nice.

REST is an architecture style based on 6 constraints defining how data are transmitted between components within a program or separated programs (not necessarily web applications). A RESTful API is an API that fulfills those constraints. I heard that there is a whole framework for Django, surprisingly called django-rest-framework, that helps developers to build REST APIs.


My questions

Hey, why would I need a REST API if the WSGI application already provides a way to parse HTTP requests and return responses? I mean, since my mobile application is nothing less than an embedded webpage, why can't I just use regular urls to access my web application and return HTML code that depends on the type of device that sent the request?

If indeed a REST API is not needed in this case, what is the purpose of a REST API in a Django project? When is it useful?

Buddyshot
  • 1,614
  • 1
  • 17
  • 44
  • 1
    how big is your client app project? how many views? is all about GET and some POST, or you would like have benefit of another HTTP methods like PUT, DELETE? Are you considering a more web service like architecture of Django app? If yes then it is worth to design RESTfull API. More about REST concept you can find here: http://stackoverflow.com/questions/4663927/what-is-rest-slightly-confused – andilabs Sep 07 '14 at 00:27

1 Answers1

2

Well as far as using Django Rest Framework is it has support built in to handle a lot of things like throttling, serializing and de-serializing django query sets into json and back again. It's got built in support to work with django's auth system.

As far as not using it, I suppose there is nothing wrong with having django view methods peruse posted data and serialize to and from json to send responses back, handle authentication, authorization and throttling etc...However that is a crap load of code which you will ultimately need to write for any api and not having to do that is a huge break.

Another benefit to using something like Django Rest Framework is the contribution from the community. Major companies and small companies use it alike. Typically the purpose of an API is to have external parties consume it. Something like Django Rest Framework tries to follow best practices in this regard making it easier for your customers to consume your api.

Chris Hawkes
  • 11,923
  • 6
  • 58
  • 68