Rails第三方API调用的最佳实践?

时间:2022-07-16 22:58:53

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

我有一个rails应用程序,可以调用第三方API进行天气预报。

The problem is that the API call is generally very slow and sometimes fails. Showing the weather is not a necessity but it adds a nice bit of extra and pertinent information.

问题是API调用通常很慢,有时会失败。显示天气不是必需的,但它增加了一些额外和相关的信息。

Right now I call the Wunderground API using Barometer gem in the controller which means the pages takes forever to load if the API is slow or fails.

现在我在控制器中使用Barometer gem调用Wunderground API,这意味着如果API缓慢或失败,页面将永远加载。

I was hoping to move to this call to an AJAX call from the page once the page is loaded. I don't mind if the information shows but a bit delayed because as mentioned it is not hugely important.

我希望在页面加载后从页面调用此AJAX调用。我不介意信息显示但有点延迟,因为如上所述它并不是非常重要。

I was just curious the best practices for making such a call? What is the Rails way?

我只是好奇发出这样一个电话的最佳做法?什么是Rails方式?

2 个解决方案

#1


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.

建议的方法是在后台调用API(使用调度程序)并将结果保存在数据库中。然后在控制器中,您可以从数据库中获取数据,并且不会有任何延迟。

#2


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.

我会说你在从浏览器转移到AJAX调用时非常正确 - 这样你的页面加载不会受到影响,并且它可能需要多长时间而不需要服务器等待它。这是异步加载数据的典型案例(通过回调和/或jQuery的deferredapproach),以便在数据加载时您可以使用其他所有内容,并且您的用户不会等待他们可能不感兴趣的某些信息。 。

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.

在保持Rails方面,您的主要考虑因素是您是否可以和/或想要直接从浏览器拨打电话到服务,或者是否要在某种程度上通过您的应用程序代理它,这将节省潜在的交叉 - 域请求问题。同样,这是您的决定,并且将取决于您是否有任何需要通过请求传输的API密钥等等,但如果请求可以直接从用户运行到天气API,那么这将允许您删除你的中间步骤。

#1


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.

建议的方法是在后台调用API(使用调度程序)并将结果保存在数据库中。然后在控制器中,您可以从数据库中获取数据,并且不会有任何延迟。

#2


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.

我会说你在从浏览器转移到AJAX调用时非常正确 - 这样你的页面加载不会受到影响,并且它可能需要多长时间而不需要服务器等待它。这是异步加载数据的典型案例(通过回调和/或jQuery的deferredapproach),以便在数据加载时您可以使用其他所有内容,并且您的用户不会等待他们可能不感兴趣的某些信息。 。

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.

在保持Rails方面,您的主要考虑因素是您是否可以和/或想要直接从浏览器拨打电话到服务,或者是否要在某种程度上通过您的应用程序代理它,这将节省潜在的交叉 - 域请求问题。同样,这是您的决定,并且将取决于您是否有任何需要通过请求传输的API密钥等等,但如果请求可以直接从用户运行到天气API,那么这将允许您删除你的中间步骤。