iOS app with Django
Asked Answered
C

3

20

So we currently have a website that was created using Django. Now, we would like to create a native iOS app that uses the same backend, so we don't have to re-code the whole thing. From my understanding, there are two alternative routes:

1) Call directly Django URLs, which then calls a function. Within that function, create a HTTPResponse, with encoded JSON data and send that back.

2) Create a REST Service from the Django server with something like Tastypie. However, aside from doing straight-forward GET calls to an object, I don't see how we can call custom functions in our Django Models from TastyPie. Can we even do that?

I find it surprising that there is not a lot of information about consuming a web service from iOS with existing backends like Django or RoR. For example, I know that instagram uses Django, but how do they communicate from iOS to their servers?!

Thanks a lot!

Creationism answered 19/10, 2012 at 19:28 Comment(0)
L
10

I am currently working on an iOS app for iPhone, with Django / Tastypie in the backend. We do both 1 and 2. The resources are offered REST-style (after auth) via Tastypie, and any custom function calls (for example, creating a new user) are handled by views.py at various REST endpoints, which returns JSON.

Lodovico answered 19/10, 2012 at 19:37 Comment(3)
How is performance? And also... why didn't you go all the way with Option 1 if you are still using it?! Thanks again!Creationism
I agree with @sampson-chen, we are doing the same. We have a REST interface with tastypie, and other methods are done with custom RPC services.Anastomose
Can you explain how the custom RPC services works? I am working on something similar and want to make sure follow some standards in terms of authentication and reusability.Commoner
A
7

When you can you should try to use a common way of doing something instead of reinventing the wheel. Given that, REST is a standard style of software architecture for distributed systems and it works very well when you work with entities/objects.

If you have an API where you interact with entities, it is recommended to use REST interfaces. On python you have Tastypie or the newer Django Rest Framework that does almost all the work. As you propose in 2)

If you have an API where you interact with services, like a login, then you should build an RPC service, basically a function with remote access as you explain on 1).

Normally you will need both ways on a robust application. And YES, it is possible to do that. I agree with @sampson-chen, we are doing the same. We have a REST interface with tastypie, and other methods are done with custom RPC services.

The performance in our case is still good, but mostly depends on the methods you call inside your services, for example, a DB query. You have a lot of ways to improve speed, for example using Celery to queue heavy jobs.

Hope it helps.

Anastomose answered 20/10, 2012 at 19:0 Comment(0)
C
0

REST APIs, while very useful, limit you to GET, POST, PUT, DELETE actions, which are performed upon resources. This can make it difficult to express other action types, such as sending an email. There are a few ways I've found to handle this within django/tastypie:

  1. Issue a PUT/PATCH request on an existing resource, setting a flag that lets your backend know to trigger an action. Detecting if a flag was set can be done inside post_save signal handlers (use django-model-utils FieldTracker to see if a field was changed from False to True); this also helps make sure your application logic works the same outside your REST API (such as changes via the admin site, a celery task, an HTML based view, or the Python shell).

  2. Create a non-ORM Resource (e.g. /api/v1/email/) and override the post_list() method, calling your function there.

  3. As mentioned elsewhere, create a subordinate resource (/api/v1/myresource/send/).

Chloral answered 15/1, 2016 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.