简体   繁体   English

Rest API - 客户端与服务器

[英]Rest API - Client vs Server

I'm building an application where I have 2 types of entities: Template entity & Runnable entity.我正在构建一个应用程序,其中有两种类型的实体:模板实体和可运行实体。 In the UI when I open template page I have 2 button 'Save' & 'Run'.在 UI 中,当我打开模板页面时,我有 2 个按钮“保存”和“运行”。 Save updates the entity.保存更新实体。 Run creates runnable entity and sends him to run executor. Run 创建可运行实体并将其发送到运行执行程序。 I can also open runnable entity in the UI.我还可以在 UI 中打开可运行实体。 for runnable entity: Save creates a new template entity and run creates another runnable entity and sends him to run executor.对于可运行实体:Save 创建一个新的模板实体并运行创建另一个可运行实体并将他发送到运行执行程序。 I'm wondering how to expose the save/run APIs and if the client should care on the type of entity it's currently working on.我想知道如何公开保存/运行 API,以及客户端是否应该关心它当前正在处理的实体类型。 which option is better?哪个选项更好?

  1. Create PUT API for save entity.为保存实体创建PUT API。 In the PUT logic I'll check the type of the entity and decide if I can update or should I create another entity out of it and return this to the client.PUT逻辑中,我将检查实体的类型并决定是否可以更新,或者我应该从中创建另一个实体并将其返回给客户端。 in this solution the client is agnostic to the type of entity it saves/runs在这个解决方案中,客户端不知道它保存/运行的实体类型
  2. The client will need to know the type of entity it's currently trying to update/run.客户端需要知道它当前尝试更新/运行的实体类型。 if it's trying to save template entity it will need to call PUT of TemplateResource and the server will return an error if the entity is actually a runnable entity.如果它试图保存模板实体,则需要调用TemplateResourcePUT ,如果实体实际上是可运行实体,服务器将返回错误。 If it tries to save a runnable entity it will need to call POST for RunnableResource如果它试图保存一个可运行实体,它将需要调用POST来获取RunnableResource

I dont understand why on 2我不明白为什么在 2

The server will return an error if the entity is actually a runnable entity.如果实体实际上是可运行实体,服务器将返回错误。

If I understand your spec correctly from the template page - Save does a PUT /api/template - while Run does a POST /api/runnable.如果我从模板页面正确理解了您的规范 - Save 执行 PUT /api/template - 而 Run 执行 POST /api/runnable。

So there is no cunfusion here.所以这里没有cunfusion。

Then on the Run page: - Save does a POST /api/template - while Run does a POST /api/runnable.然后在 Run 页面上: - Save 执行 POST /api/template - Run 执行 POST /api/runnable。

So no confusion here either.所以这里也没有混淆。

It is quite straitforward IMO since the HTTP verbs bear the intent.由于 HTTP 动词具有这种意图,因此 IMO 非常严格。 It seems simpler than implementing some logic server side to discover the intent.它似乎比实现一些逻辑服务器端来发现意图更简单。

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

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