简体   繁体   English

REST API:单个 API 是否应该承担多项职责?

[英]REST API: Should single API have multiple responsibilities?

We have classified goods website where we do not have login but users can view products listed by other users.我们有分类商品网站,我们没有登录但用户可以查看其他用户列出的产品。 To view details of other users, they have to provide their contact details.要查看其他用户的详细信息,他们必须提供他们的联系方式。 To verify if user has provided the correct mobile number, we send back OTP code to the number.为了验证用户是否提供了正确的手机号码,我们将 OTP 代码发送回该号码。 The API flow looks like: API 流程如下所示:

  1. //API to be hit when user fills form to get seller details of particular stock (this expects "stockId" and "mobile" as input): //当用户填写表单以获取特定股票的卖家详细信息时将点击 API(这需要“stockId”和“mobile”作为输入):

POST /api/lead/ POST /api/lead/

{
  "stockId": 123,
  "mobile": 9890384328
}

Response of API if "mobile" is already verified (Response code: 200):如果已验证“移动”,则 API 的响应(响应代码:200):

{
  "sellerName": "xyz",
  "sellerMobile": "+123232312",
  "sellerAddress": "21, park street, new york"
}

Response if "mobile" is NOT already verified (Response code: 403):如果“移动”尚未验证,则响应(响应代码:403):

{
   "OTP verification required. OTP is sent to the mobile number."
}
  1. User sends back request again with OTP received on mobile to the same lead API:用户使用在移动设备上收到的 OTP 再次向同一个潜在客户 API 发送回请求:

Request Payload:请求有效载荷:

{
  "stockId": 123,
  "mobile": 9890384328,
  "otp": 1234
}

It sends back seller details in response if OTP is correct.如果 OTP 正确,它会发回卖家详细信息作为响应。 If OTP provided is not correct, the response is:如果提供的 OTP 不正确,则响应为:

{
  "Incorrect OTP."
}

I see few issues in this API design:我在这个 API 设计中看到了几个问题:

  1. This API is doing lots of working ie returning back seller details, returning back OTP, verifying OTP etc. We can easily break OTP related functionality to some other API.这个 API 做了很多工作,即返回卖家详细信息、返回 OTP、验证 OTP 等。我们可以轻松地将 OTP 相关功能分解为其他一些 API。 For example one API to generate OTP ie GET /api/otp/, other API to verify OTP ie POST api/verifyotp/.例如,一个用于生成 OTP 的 API,即 GET /api/otp/,另一个用于验证 OTP 的 API,即 POST api/verifyotp/。 This would increase number of API calls from client ie first client will initiate POST lead API, if number is not verified, client will hit OTP API.这会增加来自客户端的 API 调用次数,即第一个客户端将启动 POST 引导 API,如果未验证数量,客户端将命中 OTP API。 To verify by OTP it will call verifyOTP api.要通过 OTP 进行验证,它将调用 verifyOTP api。 If it gets verified, it will call leads API to fetch seller details.如果通过验证,它将调用 Lead API 以获取卖家详细信息。 So, basically it makes 4 API calls vs 2 API calls in above approach.因此,基本上它在上述方法中进行了 4 个 API 调用与 2 个 API 调用。
  2. This is non-complaint with HATEOS which suggests "A REST client enters a REST application through a simple fixed URL. All future actions the client may take are discovered within resource representations returned from the server."这与 HATEOS 不兼容,HATEOS 建议“REST 客户端通过简单的固定 URL 进入 REST 应用程序。客户端可能采取的所有未来操作都在从服务器返回的资源表示中发现。”

Can someone suggest which approach is better?有人可以建议哪种方法更好吗?

Simple answer: no .简单的回答:没有

It is called single responsibility principle for a reason.它被称为单一责任原则是有原因的。

Allowing for more than one responsibility in the your public API means that the API "endpoint" has to understand the different responsibilities to "dispatch" to the "correct" implementation for each of these aspects.在您的公共 API 中允许多个责任意味着 API“端点”必须了解不同的责任,以便“分派”这些方面中的每一个的“正确”实现。 Or you allow your dual-responsibility API design to corrupt your implementation by having a single thing providing that implementation.或者您允许您的双重职责 API 设计通过提供该实现的单一事物来破坏您的实现。

And beyond that: when you have different responsibilities, the range of OK/error return codes simply turns more complicated.除此之外:当你有不同的职责时,OK/error 返回码的范围会变得更加复杂。 That simply makes "everything" harder.这只会让“一切”变得更加困难。 For you to write tests - but also for the clients using your API.为您编写测试 - 也为使用您的 API 的客户编写测试。

In your case, the user does:在您的情况下,用户执行以下操作:

  • use /api/lead first首先使用 /api/lead
  • to be told about "not verified"被告知“未验证”
  • use OTP generation API to get the verification code使用OTP生成API获取验证码
  • to then use a specific OTP API to submit verification code然后使用特定的OTP API提交验证码
  • to then use /api/lead again然后再次使用 /api/lead

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

相关问题 REST API:我们是否应该将结果和结果计数分开 API? - REST API: Should we have separate API for result and result count? 将多个REST API请求合并为一个请求? - Combining multiple REST API request into a single request? REST API 是否应该在不应该有查询的请求时返回错误 400? - Should REST API returns error 400 on request that should not have query? 我必须通过多个<int:pk>在 django rest api 的单个端点中? - I have to pass the multiple <int:pk> in a single endpoint for django rest api? 当属性在 REST api 中没有值时应该返回什么? - What should be returned when a property does not have a value in a REST api? 我应该通过多个步骤在REST API上使用什么响应代码? - What response code should I use on a REST API with multiple steps? 设计REST API以在单个查询字符串中接受多个复合标识符 - Designing REST API to accept multiple composite identifiers in single query string REST API-更新单个资源会更改多个其他资源 - REST API - Update of single resource changes multiple others 应该 REST API 设置cookie - Should REST API set cookie 在SOA架构中,单个API应该执行所有操作,或者应将API拆分为多个操作 - In SOA architecture should single API do everything or API should be split as multiple action
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM