REST - API 客户端 "advance" 是否应该像浏览器一样访问 "next" 资源?

REST - Should an API client "advance" to the "next" resource like a browser?

在我指定和设计 REST APIs 的这些年里,我越来越发现它与设计一个网站非常相似,在该网站上,用户的旅程和操作以及 links 都是故事板对用户体验至关重要。

目前我的 API 设计,我 return link 在项目和资源的底部。他们执行操作、改变状态或带回其他资源。

但好像每个 link 都在新标签页中打开;客户探索了一条新路线,他们的下一个选择可能会随着他们的前进而缩小。

如果这是一个网站,它不一定是一个好的设计。用户必须在新选项卡中打开 links 或一直备份堆栈才能完成任务。

好的网站只转发,或者确实有办法指示主流的分支,即 links 在新的 windows 中自动打开(通过锚标记 target ).

那么一个好的REST API 应该设计成客户端丢弃当前资源并前进到下一个并且一直在前进吗?

或者我们假设客户正在构建地图,就像 Roomba 探索我们的客厅一样?

关于地图概念的事情是,一个人应该 return 到先前资源的知识,在它可能知道的许多资源中,是在一个有知觉的人类中,一个猜测。计算机无法猜测,因此需要编程,这意味着带外静态文档并破坏了 REST。

In my years specifying and designing REST APIs, I'm increasingly finding that its very similar to designing a website

是 - 一个好的 REST API 看起来很像一个机器可读的网站。

So should a good REST API be designed as if the client discards the current resource and advances to the next and is always advancing forward?

有点——允许客户端缓存表示;因此,如果您提供 link,客户端可能 "follow" 将 link 提供给缓存表示,而不是使用服务器。

这也意味着客户端可以自行决定 "hit the back button" 离开并做其他事情(例如,如果它希望找到的 link 不存在,它可能会尝试以另一种方式实现其目标)。这是 "stateless" 约束的部分动机;服务器不必假装知道客户端当前显示的页面来解释消息。

Computers are incapable of guessing and so its need programming, and this implies out-of-band static documentation and breaks REST.

菲尔丁,writing in 2008

Of course the client has prior knowledge. Every protocol, every media type definition, every URI scheme, and every link relationship type constitutes prior knowledge that the client must know (or learn) in order to make use of that knowledge. REST doesn’t eliminate the need for a clue. What REST does is concentrate that need for prior knowledge into readily standardizable forms. That is the essential distinction between data-oriented and control-oriented integration.

我在菲尔丁的原著中找到了这个金块。

https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

The model application is therefore an engine that moves from one state to the next by examining and choosing from among the alternative state transitions in the current set of representations. Not surprisingly, this exactly matches the user interface of a hypermedia browser. However, the style does not assume that all applications are browsers. In fact, the application details are hidden from the server by the generic connector interface, and thus a user agent could equally be an automated robot performing information retrieval for an indexing service, a personal agent looking for data that matches certain criteria, or a maintenance spider busy patrolling the information for broken references or modified content [39].

它看起来像一个伟大的 REST 应用程序将被构建为仅向前,就像一个伟大的网站即使没有后退按钮也应该易于使用,包括前进到以前看到的表示(主页和搜索链接始终可用).

有趣的是,我们倾向于真正考虑网页设计中的用户旅程,并且旅程一词是我们开发人员语言的常见部分,但在 API 设计中尚未渗透。