Best practice for Rails third party API calls?

前端 未结 2 1846
一向
一向 2020-12-21 15:59

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

The problem is that the API call is generally very slow and sometimes fails. Showing the weather is no

相关标签:
2条回答
  • 2020-12-21 16:16

    The recommended way is to call to the API in the background (using a scheduler) and save the result in the database. Then in the controller you can get the data from the database and there won't be any delay.

    0 讨论(0)
  • 2020-12-21 16:20

    I would say that you are quite correct in moving to an AJAX call from the browser- that way your page load is unaffected and it can take as long as it likes without your server having to wait on it. This is a classic case for loading the data asynchronously ( through callbacks and/or jQuery's deferredapproach ) so that everything else is available while the data loads and your users aren't waiting on some information that they might not be very interested in to start with.

    In terms of keeping it Rails, your main consideration is whether you can and/or want to make the call directly from the browser to the service, or whether you want to proxy it through your application to some degree, which would save on potential cross-domain request problems. Again this is very much your decision and will depend on whether you have any API keys you need to transmit with requests and so on, but if the request can run directly from the user to the weather API then that would allow you to cut out the intermediate step on your part.

    0 讨论(0)
提交回复
热议问题