简体   繁体   English

关于应执行多少次API调用的最佳实践是什么?

[英]What is the best practice on how many calls to the API shall be done?

I have a question to ask. 我有一个问题要问。 What do you think is the best practice for an Android application(or probably any front-end application) to make calls to the API? 您认为Android应用程序(或可能是任何前端应用程序)调用API的最佳做法是什么?

For example, say we have car companies that each company has cars and cars have a profile as well with information. 例如,假设我们有汽车公司,每个公司都有汽车,并且汽车也具有个人资料以及信息。

Let's assume that we have a login call, in order to authenticate. 假设我们有一个登录呼叫,以便进行身份验证。 Shall the call be done to authenticate and retrieve all the data available(for companies and for cars that owned by the companies and all the available data -color, name, HP, etc.) or shall we split all the calls to several smaller ones(which I think is the most efficient-proper way)? 应该进行呼叫以认证和检索所有可用数据(用于公司和公司拥有的汽车以及所有可用数据-颜色,名称,HP等),还是应将所有呼叫分成几个较小的呼叫(我认为这是最有效的方法)?

How should I know, when to split a more generic call to another one? 我应该怎么知道,何时将一个更通用的调用拆分为另一个?

I am open to discuss any further. 我愿意进一步讨论。

Thanks in advance. 提前致谢。

Every resource end point should be very focused in it's responsibilities. 每个资源端点都应该非常专注于其职责。 We shouldn't be overloading it. 我们不应该超载它。 If it's an authentication end point, it should just be authenticating the user and not sending back additional data. 如果这是身份验证的终点,则应该只是在对用户进行身份验证,而不是发回其他数据。 If you have separation of concerns it makes it easier to test as well as extend your application in the future. 如果您将关注点分开,那么将来可以轻松测试和扩展您的应用程序。 This is very similar to microservices pattern where every service end point serves a single purpose and is not entirely dependent on the other service end points which means if one end point is down for some reason, it shouldn't affect the other end points. 这与微服务模式非常相似,在微服务模式中,每个服务端点都服务于一个目的,并且不完全依赖于其他服务端点,这意味着,如果某个端点由于某种原因而关闭,则不应影响其他端点。

If you want to build a reusable API, try to follow the CRUD guidelines for accessing/writing to resources. 如果要构建可重用的API,请尝试遵循CRUD准则来访问/写入资源。 If you create an endpoint for login + getting all the car data in one endpoint, that will be a very specific endpoint that won't really get used anywhere else. 如果您创建一个用于登录的端点+在一个端点中获取所有汽车数据,那将是一个非常特定的端点,在其他任何地方都不会真正使用。 You want to minimize how much code is dedicated to one action only, so that you can modularize and use the code everywhere in your app. 您希望最大程度地减少仅用于一项操作的代码,以便您可以在应用程序中的任何地方模块化和使用代码。

If it were me, I would have a separate authorize endpoint and then an endpoint to get all the car data once the user has been authorized. 如果是我,我将有一个单独的授权端点,然后有一个端点,一旦用户被授权,该端点即可获取所有汽车数据。 This way, you will have created a reusable endpoint for getting that car data that you can use elsewhere. 这样,您将创建一个可重用的端点,以获取可以在其他地方使用的汽车数据。

You should have api's information specific to the page you are going to load, if its not then you would loose its efficiency. 您应该具有要加载页面的特定api信息,如果没有,则会降低其效率。

For example: you have a huge list of items to be displayed on a particular screen but in that api you are giving information about items every detail that would make the api heavier and make the app take more time to load, instead give only that information that you are going to show on that page. 例如:您有一个巨大的项目列表要显示在特定屏幕上,但是在该api中,您会提供有关项目的信息,每个细节都会使api变重,并使应用程序花费更多的时间来加载,而只给出该信息您将要显示在该页面上。 You can also use pagination with recycle view to make it much more faster. 您还可以在回收视图中使用分页,以使其更快。

So as you said "Shall the call be done to authenticate and retrieve all the data available" no make it into two apis one for login other for retrieving cars detail. 因此,正如您所说的“应完成呼叫以验证和检索所有可用数据”,不要将其放入两个api中,一个用于登录,另一个用于检索汽车详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有相同参数的多个 REST API 调用的最佳实践是什么 - What is the best practice for multiple REST API calls with the same parameters REST API客户端实现,最佳实践是什么? - REST API client implementation, what is the best practice? 根据外部调用构造域对象的最佳实践是什么? - What is the best practice to construct a Domain Object depending on external calls? API库的最佳做法,涉及许多可选参数 - Best practice for API library involving many optional parameters 如何对课程进行专业化处理有哪些选择或最佳实践? - What are the options or best practice in how to specialize a class ? 如何以与API调用相同的顺序在RecyclerView中加载项目? - How to load items in a RecyclerView in the same order as the API calls are done? 在rest API响应中返回登录凭据的最佳实践是什么? - What is the best practice to return login credentials in a rest API response? UncheckedIOException和Stream API的成语/最佳实践是什么? - What's idiom/best practice with UncheckedIOException and Stream API? Mybatis 多对多最佳实践 - Mybatis Many-to-Many best practice Java最佳实践:Controller中的最佳调用数 - Java best practice: optimum number of calls in the Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM