简体   繁体   English

如何设计可扩展的静态/非静态API?

[英]How to design a scalable restful / Non-restful API?

I have an API 'x' which does a CRUD on 4 params today - a, b, c, d. 我有一个API“ x”,它今天对4个参数-a,b,c,d进行了CRUD。 Tomorrow if I have to support few more params in the same API, due to change in requirement, then what is the best way to handle it? 明天如果由于需求的变化,如果我必须在同一API中支持更多的参数,那么最好的处理方法是什么?

Should I go ahead and update my DB with the new params introduced and continue doing this everytime there is a change in the requirement? 我是否应该继续使用引入的新参数更新数据库,并在需求发生变化时继续执行此操作?

One of my peers suggests that the API should accept a JSON Object which can accept any set of params and any set of key-value pairs. 我的一位同伴建议API应该接受一个JSON对象,该对象可以接受任何一组参数和任何一组键值对。 Every time I get a new set of key-value pair via this JSON, I update my params table with this value and then the requirement can change as and when they want. 每次通过此JSON获得一组新的键值对时,都会使用该值更新我的params表,然后需求可以随时随地更改。

I think that this will be very difficult to maintain across clients as the API will literally accept any key-value pair and hence one platform might debate on sending these key-value pairs in one format while the other would be willing to send it in another format. 我认为这很难在客户端之间维护,因为API实际上会接受任何键值对,因此一个平台可能会辩论以一种格式发送这些键值对,而另一种平台则愿意以另一种格式发送。格式。 Also, it will be hard to put in validations and check on optional and mandatory params. 另外,将很难进行验证并检查可选和强制参数。

Any leads is appreciated 任何线索表示赞赏

My answer has a bit of nuance, so beware. 我的回答有些细微差别,所以要当心。

I find that in practice we don't make that many changes to our API's, and when we do, we can often do it in a way where it's backwards compatible. 我发现在实践中,我们不会对API进行太多更改,而当我们这样做时,我们通常可以采用向后兼容的方式来进行更改。 This does mean we add optional fields. 这确实意味着我们添加了可选字段。

If you control all clients, it's also fairly easy because you can just upgrade all of them so it's not really an issue. 如果您控制所有客户端,这也相当容易,因为您可以升级所有客户端,因此这并不是真正的问题。

It's harder when you make changes more often, or are doing more than just adding some properties, and it's 3rd parties connecting to you, you can easily upgrade them. 当您进行更多更改或仅添加一些属性而做的事情时,这将变得更加困难,并且这是与您联系的第三方,您可以轻松地对其进行升级。

There's more than 1 way solve this, but my recommendation is the follow github's model. 有多种解决方法,但是我的建议是遵循github的模型。

For bigger breaking changes they change the Content-Type header. 对于较大的重大更改,它们将更改Content-Type标头。 Currently the Content-Type for github requests is: 当前,github请求的Content-Type为:

Accept: application/vnd.github.v3+json

Before they made a big breaking change it was: 在他们进行重大突破之前,它是:

Accept: application/vnd.github.v2+json

For experimental API's that are prone to change they use an experimental Content-Type : 对于易于更改的实验性API,可以使用实验性Content-Type

Accept: application/vnd.github.mister-fantastic-preview+json

Generally this idea is preferred for restful design, but it's not the only solution. 通常,此想法可用于宁静的设计,但不是唯一的解决方案。

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

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