检查API响应代码,无论API版本如何。

huangapple go评论60阅读模式
英文:

Check API response codes irrespective of API versions

问题

我正在尝试重构一些旧的API,通过更改响应代码来实现。但调用这些API的客户端在其逻辑中使用这些响应代码。那么如何更改API的响应代码,而不破坏客户端,并且不进行API版本控制?

我知道我们可以对API进行版本控制,但为此我们必须与客户端通信,以便他们可以相应更改其代码。那么是否有一种通用的方法来实现这一点?

英文:

I am trying to refactor some older APIs, by changing response codes. But the client which is calling these APIs is using these response codes in their logic. So how to change response codes of APIs without breaking the client, and without API versioning?

I know that we can version APIs, but for that we have to communicate to clients so that they can change their code accordingly. So is there a generic way to do this?

答案1

得分: 1

一种方法是让客户端告诉你它编码到哪个API版本,然后让服务器相应地适应。

可以处理向后兼容性:如果客户端在请求中不包括 'version indicator'(因为现有客户端不会这样做),那意味着最旧的版本。

然后,你将不得不拥有(至少)两套响应代码,即今天正在使用的代码和你想要迁移到的代码。这就是兼容性的代价。

根据你的用户群,你可以弃用并最终淘汰旧的API。通常这是一个宣布 '在版本x.y中将删除旧的API' 的公告。

英文:

One way is to make the client tell you what version of the API it is coded to, and have the server adapt accordingly.

Back-compatibility can be handled: if the client does not include the 'version indicator' in its request (as existing clients will not be doing) then that means the oldest version.

You're then going to have to have (at least) two sets of response codes, the ones in use today and the ones you want to move to. That's the price of compatibility.

Depending on your user base, you can deprecate and eventually eliminate the older API. Typically that is an announcement that 'the older API will be removed in version x.y'.

huangapple
  • 本文由 发表于 2023年3月31日 20:18:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定