简体   繁体   English

我应该使用哪个资源来保留我的API RESTFul?

[英]Which resource should I use to keep my API RESTFul?

I'm building an API to allow the client of the API to send notifications to remind a user to update an Order status. 我正在构建一个API,以允许API的客户端发送通知以提醒用户更新订单状态。 So far, there are two notifications: 到目前为止,有两个通知:

  • when the user hasn't marked the order as received; 当用户尚未将订单标记为已接收时;
  • when the user hasn't marked the order as done. 当用户尚未将订单标记为完成时。

I want to build this API to make it simple to extend to other notifications related to the order, but keep a simple URI for the clients of this API. 我想要构建此API,以使其易于扩展到与订单相关的其他通知,但为该API的客户端保留一个简单的URI。 How can I define my resources in order to keep my API RESTFul? 如何定义我的资源以保留我的API RESTFul?

I was thinking about having one of these structures: 我正在考虑采用以下一种结构:

Option 1 : 选项1

POST: /api/ordernotification/receive/{id}
POST: /api/ordernotification/complete/{id}

Option 2 (omit the status from the resource and post it instead) : 选项2(从资源中省略状态,然后发布)

POST: /api/ordernotification/?id={id}&statusID={statusID}

EDIT 编辑

Option 2.1 (keeping an articulated URI, as suggested by @Jazimov) : 选项2.1(保留明确的URI,如@Jazimov建议)

POST: /api/ordernotification/{statusID}/{id}. 

Which option is more appropriate? 哪个选项更合适? Is there any advantage that one option has over the other? 一个选择相对于另一个有什么优势吗? Or are there any other option I haven't thought of? 还是我没有想到的其他选择?

If I understand you correctly, you have two types of ordernotifications : those for notifying receive and those for notifying complete . 如果我对您的理解正确,那么您有两种类型的ordernotifications通知:用于通知receive的通知和用于通知complete的通知。 If those are two separate data models then I think nesting them is a good idea (ie a table called ReceiveOrderNotification and CompleteOrderNotification ). 如果这是两个单独的数据模型,那么我认为将它们嵌套是一个好主意(即称为ReceiveOrderNotificationCompleteOrderNotification的表)。 If that's the case then you may want to expose two different endpoints entirely, such as POST /api/receiveordernotification and POST /api/completeordernotification . 如果是这种情况,那么您可能希望完全公开两个不同的端点,例如POST /api/receiveordernotificationPOST /api/completeordernotification

But I don't think that's the best you can do, given so many overlapping similarities there probably are between order notifications. 但是,鉴于订单通知之间可能存在太多重叠的相似点,因此我认为这不是您能做的最好的事情。 Now, option 2 is more like a GET , since you're using query parameters, so with your first option let's collapse them into this: 现在,选项2更像是GET ,因为您使用的是查询参数,因此,使用第一个选项时,让我们将其折叠为:

POST: /api/ordernotification/

and then pass it some JSON data to create the notifications 然后将一些JSON数据传递给它以创建通知

{
    "orderId": "orderId",
    "userId": "userId",
    "prompt": "not marked received/not marked done"
}

I also removed the /{id} because when you POST you create a brand new thing and the id has not been created yet, usually. 我还删除了/{id}因为在您发布POST时,您会创建一个全新的事物,并且通常尚未创建ID。 Even if the client is creating an id and sending it to the API it is a good practice to leave it open so your API can handle creating a new, unique resource in its own way. 即使客户端正在创建一个id并将其发送给API,也应将其保持打开状态,这样您的API可以以自己的方式处理创建新的唯一资源的良好做法。

This is RESTful is because a POST creates a resource ordernotification with certain data points. 这是RESTful的,因为POST使用某些数据点创建资源ordernotification Your first option made actions a resource in themselves but that's probably not represented in any data model in your backend. 您的第一个选择是使操作本身成为一种资源,但是在后端的任何数据模型中都可能没有表示。 To be as RESTful as possible, your API endpoints should represent the database domains (tables, collections, etc). 为了尽可能RESTful,您的API端点应代表数据库域(表,集合等)。 Then you let your controllers choose what service methods to use given the data sent in the request. 然后,根据请求中发送的数据,让您的控制器选择要使用的服务方法。 Otherwise REST endpoints expose all the logic up front and get to be a long list of unmaintainable endpoints. 否则,REST端点会预先暴露所有逻辑,并且会成为一堆无法维护的端点。

I would go with something along these lines 我会遵循这些思路

POST /api/order/1234/notifications/not-received
POST /api/order/1234/notifications/not-completed

Which can later be accessed via 以后可以通过访问

GET /api/order/1234/notifications/not-received
GET /api/order/1234/notifications/not-completed

Or 要么

GET /api/order/1234/notification/8899

There's no real limitation on how semantically rich a URI can be, so you might as well take advantage of that and be explicit. URI在语义上的丰富程度没有真正的限制,因此您最好利用它,并使其明确。

I think, to update status of already inserted records, your endpoint should be PUT instead of POST. 我认为,要更新已插入记录的状态,您的端点应为PUT而不是POST。

You can use 您可以使用

PUT: /api/ordernotification/:id/status/

with clients json data 与客户json数据

 {
     "status": "your_status"
 }

according to request data, endpoint should update the record. 根据请求数据,端点应更新记录。

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

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