RESTful API 具有多租户和共享资源
RESTful API with multitenancy and shared resources
我正在尝试为具有多租户支持和共享资源的应用程序找出 "right" 结构的 url 实现。
资源:用户、项目
URL 架构是
host/api/tenant_id/resource[/id][/subresource][/id]
用户 A(宽度 id = 1)在
获取他的项目集合
GET http://example.com/api/1/projects/
用户 A 创建了一个新项目,
可读
GET http://example.com/api/1/projects/2
现在用户 A 授予另一个用户 B (id = 2) 访问项目 2 的权限。
用户 B 希望通过以下方式查看与其帐户相关的所有项目的集合:
GET http://example.com/api/2/projects/
共享项目(id = 2)除了用户B自己创建的那些之外,是否应该在这个集合中?或者共享资源有没有更好的命名结构?
专注于 URL 结构的设计实际上是 RESTful 架构的禁忌。罗伊菲尔丁:
A REST API must not define fixed resource names or hierarchies (an obvious coupling of client and server).
另见 this answer。
对于您的具体问题,我会 return 用户有权访问的项目的超文本链接列表(基本上是任意的)。链接将包含明确说明该项目是用户“拥有”还是“可访问”的属性。为了提高可读性,您可以将资源 URLs 设计为
http://example.com/user/{user id}
http://example.com/project/{project id}
GET http://example.com/user/2 之后的用户表示将包含链接列表,例如
<a href="http://example.com/project/1" class="owned"/>
<a href="http://example.com/project/2" class="access-permitted"/>
HATEOAS 原则是 REST 固有的,并且使大多数 »我如何设计我的 URI« 问题都过时了:
The principle is that a client interacts with a network application entirely through hypermedia provided dynamically by application servers. A REST client needs no prior knowledge about how to interact with any particular application or server beyond a generic understanding of hypermedia.
也许在路径中使用租户信息可能是一个优势。通过这种方式,我们可以很容易地获得例如对象列表。
在 get /tenant-id/projects 上查询 uri
我们可以为每个条目租户提供一个项目列表。
如何在没有租户信息的情况下进入 url?
我的 2 美分
我正在尝试为具有多租户支持和共享资源的应用程序找出 "right" 结构的 url 实现。
资源:用户、项目
URL 架构是
host/api/tenant_id/resource[/id][/subresource][/id]
用户 A(宽度 id = 1)在
获取他的项目集合GET http://example.com/api/1/projects/
用户 A 创建了一个新项目,
可读GET http://example.com/api/1/projects/2
现在用户 A 授予另一个用户 B (id = 2) 访问项目 2 的权限。 用户 B 希望通过以下方式查看与其帐户相关的所有项目的集合:
GET http://example.com/api/2/projects/
共享项目(id = 2)除了用户B自己创建的那些之外,是否应该在这个集合中?或者共享资源有没有更好的命名结构?
专注于 URL 结构的设计实际上是 RESTful 架构的禁忌。罗伊菲尔丁:
A REST API must not define fixed resource names or hierarchies (an obvious coupling of client and server).
另见 this answer。
对于您的具体问题,我会 return 用户有权访问的项目的超文本链接列表(基本上是任意的)。链接将包含明确说明该项目是用户“拥有”还是“可访问”的属性。为了提高可读性,您可以将资源 URLs 设计为
http://example.com/user/{user id}
http://example.com/project/{project id}
GET http://example.com/user/2 之后的用户表示将包含链接列表,例如
<a href="http://example.com/project/1" class="owned"/>
<a href="http://example.com/project/2" class="access-permitted"/>
HATEOAS 原则是 REST 固有的,并且使大多数 »我如何设计我的 URI« 问题都过时了:
The principle is that a client interacts with a network application entirely through hypermedia provided dynamically by application servers. A REST client needs no prior knowledge about how to interact with any particular application or server beyond a generic understanding of hypermedia.
也许在路径中使用租户信息可能是一个优势。通过这种方式,我们可以很容易地获得例如对象列表。 在 get /tenant-id/projects 上查询 uri 我们可以为每个条目租户提供一个项目列表。 如何在没有租户信息的情况下进入 url?
我的 2 美分