Should my web application be a consumer of my api?

In the near future I will develop a mobile application (iPhone) and a web application (Django). For a mobile application, I will create a REST api (most likely using Django) to send data from the phone to the server.

When I take the time to create a web version, it makes sense to just create it like any other api client. In other words, the mobile application and the web application will receive data from the external API via HTTP. Or should a web application have direct access to the database that the api uses and just get their data this way?

+3
source share
4 answers

"". Python API . API REST API- Python. API Python.

+4

- API . , - . XML/JSON RESTful.

+1

, , API HTML. Django, , API HTML-. - Django , , -.

iPhone- , HTML. App1 ( -) views.py / iPhone. App2, App1.models, views.py. , , , , , .

:

App1.views:

def list(request, template="list.json"):
    list = Model.objects.filter(deleted=False).filter(user=request.user)
    list.reverse()
    ## Lots of other logic to work on the list.
    return render_to_response(template, {list: list,})

App2.views:

def list(request, template="list.html"):
    return App1.views.list(request, template=template)
+1

I think the answer to this question has changed over time. A year ago, when asked, it was probably too much trouble to do this, but now I will definitely say yes - using your API, since the basis is the smart thing. As websites use more HTML5 and mobile applications become smarter, it makes sense to have all of your “user interfaces” read / write from the same API level. This will give you more flexibility in the future.

0
source

Source: https://habr.com/ru/post/1749662/


All Articles