简体   繁体   English

使用 HATEOAS 进行高效的 REST-API 调用

[英]Make efficient REST-API calls with HATEOAS

I think I basically understand what HATEOAS is and why you should use it.我想我基本上了解 HATEOAS 是什么以及为什么要使用它。 However, I have some problems with applying this to a real world scenario.但是,我在将其应用于现实世界场景时遇到了一些问题。

For example:例如:

I would like to display a list of appointments in my client application.我想在我的客户端应用程序中显示约会列表。 An appointment consists of a date, a location and a person.约会由日期、地点和人员组成。 All of this information should be displayed in a table.所有这些信息都应显示在表格中。

My API's response currently looks something like this:我的 API 的响应目前看起来像这样:

{
  "id": 1,
  "date": "2020-01-01",
  "location": {
    "name": "my office",
    "address": "my street 22"
  },
  "person": {
    "name": "john",
  } 
},   
{
  "id": 2,
  "date": "2020-01-22",
  "location": {
    "name": "your office",
    "address": "your street 30"
  },
  "person": {
    "name": "peter",
  } ​
}

Each appointment object therefore also contains all related objects.因此,每个约会 object 也包含所有相关对象。 This makes fetching the data from the REST API efficient from my point of view, since I only need one API call.从我的角度来看,这使得从 REST API 获取数据非常高效,因为我只需要一个 API 调用。 For example:例如:

api.com/appointments

With HATEOAS the API response would probably look something like this:使用 HATEOAS,API 响应可能看起来像这样:

{
  "id": 1,
  "date": "2020-01-01",
  "location": {
    "url": "api.com/location/{id}",
  },
  "person": {
    "url": "api.com/person/{id}",
  } 
},    
{
  "id": 2,
  "date": "2020-01-22",
  "location": {
    "url": "api.com/location/{id}",
  },
  "person": {
    "url": "api.com/person/{id}",
  } ​
}

I would have to get the related objects through additional API calls.我必须通过额外的 API 调用来获取相关对象。

Let us now assume the following:现在让我们假设以下内容:

I would like to display a list of 100 appointments.我想显示 100 个约会的列表。 Each appointment is assigned to a different person and a different location.每个约会都分配给不同的人和不同的位置。 So I would need an API call to get all appointments, another 100 API calls to get all locations and another 100 API calls to get all person.所以我需要一个 API 电话来获取所有约会,另外 100 个 API 电话来获取所有位置,另外 100 个 API 电话来获取所有人。 That is a total of 202 API calls.总共有 202 个 API 调用。

That doesn't sound very efficient to me.这对我来说听起来不是很有效。 Do I have a mistake here or how would HATEOAS work in such a scenario?我在这里有错误吗,或者 HATEOAS 在这种情况下如何工作?

You confuse REST with a database.您将 REST 与数据库混淆了。 You can expand nested resources if that's what your clients want to.如果这是您的客户想要的,您可以扩展嵌套资源。 HATEOAS is more about adding useful operations to your resources, so you can search, read, paginate, modify, create, delete them if you want to. HATEOAS 更多的是为您的资源添加有用的操作,因此您可以根据需要搜索、阅读、分页、修改、创建、删除它们。 As of the REST API, you should look at it as if it was a webpage.从 REST API 开始,您应该将其视为网页。 If you want to see all the appointments in a table along with the edit or delete buttons, then you send back a view model for that table along with the links for edit or delete.如果您想查看表格中的所有约会以及编辑或删除按钮,那么您将发送回该表格的视图 model 以及用于编辑或删除的链接。 The REST client normally runs on server side and the application that uses it is a consumer of the REST service. REST 客户端通常在服务器端运行,使用它的应用程序是 REST 服务的消费者。 So the REST service is normally not in direct contact with the user and the REST client does not run in the browser though it can be used this way too with single page JS applications, but it was not designed for this usage.因此,REST 服务通常不与用户直接接触,并且 REST 客户端不在浏览器中运行,尽管它也可以用于单页 JS 应用程序,但它不是为这种用途而设计的。

how would HATEOAS work in such a scenario? HATEOAS 在这种情况下如何工作?

How would you do this with a web page?您将如何使用 web 页面执行此操作?

There are a lot of designs you could choose, but a perfectly ordinary, backwards compatible to 1990s, choice would be to create a table with 100 rows, with the immediately useful information directly encoded into the table cells, and links to other web pages that might be useful.您可以选择很多设计,但完全普通的、向后兼容 1990 年代的选择是创建一个包含 100 行的表格,将立即有用的信息直接编码到表格单元格中,并链接到其他 web 页面可能有用。

And that's fine .很好

Sometimes, we use this webpage to display a digest of the data, and provide a link that instructs the client how to find a richer view of the data.有时,我们使用这个网页来显示数据的摘要,并提供一个链接来指导客户端如何找到更丰富的数据视图。

And that's also fine.很好。

In other words, you get to choose for yourself which tradeoffs are appropriate for your needs, and design your resource model such that it is in alignment with those needs.换句话说,您可以自己选择适合需求的折衷方案,并设计您的资源 model 使其在 alignment 中满足这些需求。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM