简体   繁体   English

RESTful API应该返回带有代码的数据还是将代码转换为描述?

[英]Should a RESTful API return data with codes or translate codes into descriptions?

I'm developing an API that will return data to an end user. 我正在开发一个API,它将向最终用户返回数据。 In my server side model, I use a number of codes that have an associated lookup table to find descriptions and other related properties tied to the code. 在我的服务器端模型中,我使用许多具有关联的查找表的代码来查找描述和与该代码相关的其他相关属性。

I'm curious if there is a "Best Practice" in regards to returning data. 我很好奇在返回数据方面是否有“最佳实践”。 The options here would be: 这里的选项是:

Option 1: Return codes and provide APIs for lookup lists 选项1:返回码并为查找列表提供API

In this scenario, you might have something like this: 在这种情况下,您可能会遇到以下情况:

//An API call to get a person
let person = await this.dataService.getPerson(1); //performs fetch
//{id: 1, first: "Micky", last: "Mouse", gender: 1, countryOfOrigin: "US"}

//An API call to get lookups -- returned as Map();
let genders = await this.dataService.getGenders();
let countries = await this.dataService.getCountries();

//Now I can do the following to get the definition
let gender = genders.get(person.gender);
let country = countries.get(person.countryOfOrigin).description;

 console.log("gender.description");
 console.log("country.description");
 console.log("country.isoA3");

Option 2: Return an object fully populated 选项2:返回完全填充的对象

//API call to get a person
let person = dataService.getPerson(1);
//{id: 1, 
   first: "Micky", 
   last: "Mouse", 
   gender: "Male", 
   countryOfOrigin: {
      code: "US",
      description: "United States of America",
      isoA3: "USA",
      isoN3: 840
    }
  }

Option 3: Make it self referencing 选项3:使其自引用

I know there is an option whereby the countryOfOrigin might be a link to another API call for a given country. 我知道有一个选项,countryOfOrigin可能是指向给定国家/地区的另一个API调用的链接。 However, in my situation, most users will be requesting a large number of people as opposed to individual people and displaying those persons in a list form -- so it would be quite a hit on the server if the user had to query for 1000 people and then ping the server 1000 times to get each countryOfOrigin. 但是,在我的情况下,大多数用户将要求大量人员而不是单个人员,并以列表形式显示这些人员。因此,如果用户必须查询1000个人,这将对服务器造成很大的冲击然后ping服务器1000次以获取每个countryOfOrigin。

Is there a standard or best practice that would provide some direction here? 是否有标准或最佳实践可以在此提供一些指导?

Is there a standard or best practice that would provide some direction here? 是否有标准或最佳实践可以在此提供一些指导?

Not really, there are just different trade offs. 并非如此,这只是权衡取舍。

The easiest analogy I can think of on the web is java script -- should you embed the source in the page? 我在网络上想到的最简单的类比是Java脚本-您是否应将源代码嵌入页面中? or should you link to the source and download it separately? 还是应该链接到源并单独下载? "It depends" -- having the separate resource is great when you want fine grained control of the caching policy, but its lousy when caching is a liability rather than an asset. “这取决于”-当您希望对缓存策略进行细粒度的控制时,拥有单独的资源非常有用,但是在缓存时,它的糟糕是一种责任而不是资产。

One common approach, as noted by Preacher, is to provide different resources for the different use cases. 如Preacher所述,一种常见的方法是为不同的用例提供不同的资源。

Another possibility is to use a single resource, but with different representations (media-types) to support the different use cases. 另一种可能性是使用单个资源,但是使用不同的表示形式(媒体类型)来支持不同的用例。

As far as I can tell, these are just different ways of moving the pain around. 据我所知,这些只是缓解痛苦的不同方法。

You have a couple of potential options: 您有两种可能的选择:

A. Use an api standard that supports embedding resources. 答:使用支持嵌入资源的api标准。

I tend to use HAL for all responses, but HAL is not a great format to solve this problem. 我倾向于对所有响应都使用HAL,但是HAL并不是解决此问题的好方法。 JSON:API handles this case better. JSON:API可以更好地处理这种情况。 It allows sending relationships along with the response, an a good JSON:API client can take those relationships and put them in a temporary cache. 它允许发送关系以及响应,一个好的JSON:API客户端可以获取这些关系并将它们放在临时缓存中。

The reason HAL isn't good for this is that it doesn't have a good way to de-duplicate multiple relationships. HAL不适用于此的原因是它没有消除重复关系的好方法。 GraphQL handles this case also extremely well, but it's not REST. GraphQL可以很好地处理这种情况,但它不是REST。

B. Attack the true cause of the issue. B.找出问题的真正原因。

Really the issue you are describing is that you are worried that many HTTP requests will be made, and having many HTTP requests is a bad thing. 确实,您正在描述的问题是您担心会发出许多HTTP请求,而拥有许多HTTP请求是一件坏事。

There's several ways to potentially combat this. 有几种方法可以解决这个问题。

  1. Use HTTP/2. 使用HTTP / 2。 With HTTP/2 having many smaller requests is much less of an issue as it used to be. 对于HTTP / 2,具有许多较小的请求已不再是一个问题。 Maybe it's not as painful as you think. 也许没有你想的那么痛苦。
  2. Use HTTP/2 Push. 使用HTTP / 2推送。 Push can optimize this a little bit further and remove even a bit more latency. 推送可以进一步优化这一点,甚至可以消除更多的延迟。
  3. If your API is slow, choose a technology that can respond to HTTP requests even faster. 如果您的API速度很慢,请选择一种可以更快响应HTTP请求的技术。 Does your framework have a 100ms start-up time for every request? 您的框架是否对每个请求都有100毫秒的启动时间? That's gonna be very noticable. 这将是非常明显的。
  4. Use great cache headers. 使用出色的缓存头。 Countries don't change much. 国家变化不大。 You can put very aggressive Cache-Control headers, letting clients keep a copy of the headers for a very long time. 您可以放置​​非常激进的Cache-Control标头,让客户端将标头的副本保留很长时间。 This will especially benefit browser clients (HTTP clients running on servers tend to not use caching much). 这将特别有利于浏览器客户端(服务器上运行的HTTP客户端往往不使用太多缓存)。
  5. Use a good caching proxy or CDN. 使用良好的缓存代理或CDN。 This is related to 4, and might make 3 unnecessary. 这与4相关,并且可能使3不必要。

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

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