Best practice for calling third party Rails APIs?

I have a rails app that calls a third party API for weather.

The problem is that the API call is usually very slow and sometimes fails. Showing the weather is optional, but it does add a nice little extra and pertinent information.

I am currently calling the Wunderground API using a barometer in my controller, which means that pages will load forever if the API is slow or not working.

I was hoping to move this call to an AJAX call from the page after the page has loaded. I don't mind if the information shows, but it is a little delayed because as already mentioned it is not very important.

I was curious about the best way to make such a call? What is Rails?

+2


source to share


2 answers


The recommended way is to call the API in the background (using the scheduler) and store the result in the database. Then in the controller, you can get the data from the database and there will be no delay.



+7


source


I would say that you are completely correct in jumping to the AJAX call from the browser - this way your page load is not affected and it can take as long as you like if your server doesn't have to wait. This is a classic case for loading data asynchronously (via callbacks and / or jQuery deferred

) so that everything else is available while the data is loading and your users don't expect any information that they might not be very interested in getting started.



From a Rails persistence perspective, your main concern is whether you can and / or want to make a call directly from the browser to the service, or whether you want to proxy it through your application to some extent to save on potential cross-domain request issues. Again this is very much your decision and will depend on whether you have any API keys that you have to pass with requests, etc., but if the request can be made directly from the user to the weather API, this will allow you to strip an intermediate step on your part.

+2


source







All Articles