REST API 为自然划分为子集合的集合设计
REST API design for collection that is naturally divided into subcollections
我有一组请求从我的客户端应用程序发送到处理它们的服务器。我用这个请求创建新请求
POST api/v1/requests
发送请求后,它收到状态 PENDING
,经过评估后,状态更改为 RESOLVED
。因此,我的集合 requests
分为 2 个子集合:requests.pending
和 requests.resolved
。 我需要一种方法来获得对它们的分页访问并能够缓存这些页面。
像这样是不是REST方式:
GET api/v1/requests/page/:page - returns pages of all requests collection
GET api/v1/requests/pending/page/:page - returns pages of pending requests collection
GET api/v1/requests/resolved/page/:page - returns pages of resolved requests collection
我对这种方法有点不舒服,因为主要的 resource 是 requests
集合,我正在它上面创建 2 个人工集合(或存储) .尽管如此,我认为我不能只使用这样的查询参数来解决这个问题,因为缓存服务器不应该通过 REST 协议缓存查询参数:
GET api/v1/requests/?page=:page - returns pages of all requests
这里有 很多 种方法来制作你的 url,我相信会有几个 (strong) 的意见.
我的建议如下...
POST ../api/v1/request
v1 vs 1 是我个人的偏好,我认为 v1 比 1 更具描述性。
有很多关于资源名称的复数与单数的讨论。我偏爱单数。
// redirects to a paginated url
GET ../api/v1/request -> ../api/v1/request?page=1&rpp=10
// your default page that return all types of requests ordered
// however you want, most likely reverse date created.
GET ../api/v1/request?page=1&rpp=10
// returns pending requests.
GET ../api/v1/request?page=1&rpp=10&status=pending
// returns resolved requests.
GET ../api/v1/request?page=1&rpp=10&status=resolved
这是我的想法...据我所知,您实际上是在尝试查询您的请求。
通常,更多 specific/nested 网址用于子关系。
例如,获取特定客户的所有地址:
GET ../api/v1/customer/1/address
以上所有网址都可以缓存和添加书签。
我有一组请求从我的客户端应用程序发送到处理它们的服务器。我用这个请求创建新请求
POST api/v1/requests
发送请求后,它收到状态 PENDING
,经过评估后,状态更改为 RESOLVED
。因此,我的集合 requests
分为 2 个子集合:requests.pending
和 requests.resolved
。 我需要一种方法来获得对它们的分页访问并能够缓存这些页面。
像这样是不是REST方式:
GET api/v1/requests/page/:page - returns pages of all requests collection
GET api/v1/requests/pending/page/:page - returns pages of pending requests collection
GET api/v1/requests/resolved/page/:page - returns pages of resolved requests collection
我对这种方法有点不舒服,因为主要的 resource 是 requests
集合,我正在它上面创建 2 个人工集合(或存储) .尽管如此,我认为我不能只使用这样的查询参数来解决这个问题,因为缓存服务器不应该通过 REST 协议缓存查询参数:
GET api/v1/requests/?page=:page - returns pages of all requests
这里有 很多 种方法来制作你的 url,我相信会有几个 (strong) 的意见.
我的建议如下...
POST ../api/v1/request
v1 vs 1 是我个人的偏好,我认为 v1 比 1 更具描述性。
有很多关于资源名称的复数与单数的讨论。我偏爱单数。
// redirects to a paginated url
GET ../api/v1/request -> ../api/v1/request?page=1&rpp=10
// your default page that return all types of requests ordered
// however you want, most likely reverse date created.
GET ../api/v1/request?page=1&rpp=10
// returns pending requests.
GET ../api/v1/request?page=1&rpp=10&status=pending
// returns resolved requests.
GET ../api/v1/request?page=1&rpp=10&status=resolved
这是我的想法...据我所知,您实际上是在尝试查询您的请求。
通常,更多 specific/nested 网址用于子关系。
例如,获取特定客户的所有地址:
GET ../api/v1/customer/1/address
以上所有网址都可以缓存和添加书签。