Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django calling REST API from models or views? [closed]

I have to call external REST APIs from Django. The external data source schemas resemble my Django models. I'm supposed to keep the remote data and local ones in sync (maybe not relevant for the question)

Questions:

  1. What is the most logical place from where to call external web services: from a model method or from a view?
  2. Should I put the code that call the remote API in external modules that will be then called by the views?
  3. Is it possible to conditionally select the data source? Meaning presenting the data from the REST API or the local models depending on their "freshness"?

Thanks

EDIT: for the people willing to close this question: I've broken down the question in three simple questions from the beginning and I've received good answers so far, thanks.

like image 564
Leonardo Avatar asked Mar 18 '14 12:03

Leonardo


2 Answers

  1. I think it is an opinion where to call web services. I would say don't pollute your models because it means you probably need instances of those models to call these web services. That might not make any sense. Your other choice there is to make things @classmethod on the models, which is not very clean design I would argue.

    Calling from the view is probably more natural if accessing the view itself is what triggers the web service call. Is it? You said that you need to keep things in sync, which points to a possible need for background processing. At that point, you can still use views if your background processes issue http requests, but that's often not the best design. If anything, you would probably want your own REST API for this, which necessitates separating the code from your average web site view.

    My opinion is these calls should be placed in modules and classes specifically encapsulated for your remote calls and processing. This makes things flexible (background jobs, signals, etc.) and it is also easier to unit test. You can trigger calling this code in the views or elsewhere, but the logic itself should be separate from both the views and the models to decouple things nicely.

    You should imagine that this logic should exist on its own if there was no Django around it, then build other pieces that connect that logic to Django (ex: syncing the models). In other words, keep things atomic.

  2. Yes, same reasons as above, especially flexibility. Is there any reason not to?

  3. Yes, simply create the equivalent of an interface. Have each class map to the interface. If the fields are the same and you are lazy, in python you can just dump the fields you need as dicts to the constructor (using **kwargs) and be done with it, or rename the keys using some convetion you can process. I usually build some sort of simple data mapper class for this and process the django or rest models in a list comprehension, but no need if things match up as I mentioned.

    Another related option to the above is you can dump things into a common structure in a cache such as Redis or Memcache. It might be wise to atomically update this info if you are concerned with "freshness." But in general you should have a single source of authority that can tell you what is actually fresh. In sync situations, I think it's better to pick one or the other to keep things predictable and clear though.

One last thing that might influence your design is that by definition, keeping things in sync is a difficult process. Syncs tend to be very prone to failure, so you should have some sort of durable mechanism such as a task queue or job system for retries. Always assume when calling a remote REST API that calls can fail for crazy reasons such as network hicups. Also keep in mind transactions and transactional behavior when syncing. Since these are important, it points again to the fact that if you put all this logic in a view directly, you will probably run into trouble reusing it in the background without abstracting things a bit anyway.

like image 50
therewillbesnacks Avatar answered Oct 19 '22 15:10

therewillbesnacks


What is the most logical place from where to call external web services: from a model method or from a view?

Ideally your models should only talk to database and have no clue what's happening with your business logic.

Should I put the code that call the remote API in external modules that will be then called by the views?

If you need to access them from multiple modules, then yes, placing them in a module makes sense. That way you can reuse them efficiently.

Is it possible to conditionally select the data source? Meaning presenting the data from the REST API or the local models depending on their "freshness"?

Of course it's possible. You can just implement how you fetch your data on request. But the more efficient way might just be avoiding that logic and just sync your local data with remote data and show the local data on the views.

like image 36
Bibhas Debnath Avatar answered Oct 19 '22 15:10

Bibhas Debnath