简体   繁体   English

如何为 REST API 建模以设计计算器服务

[英]How to model REST API for designing a calculator service

I have been reading about the best practices for designing API's for REST services that will be exposed to customers.我一直在阅读有关为将向客户公开的 REST 服务设计 API 的最佳实践。 For example, we should use Nouns to name all the URI's exposed.例如,我们应该使用名词来命名所有暴露的 URI。 Further the verbs should obey the semantics of the HTTP commands.此外,动词应遵守 HTTP 命令的语义。 For example, a GET request should never modify the resource, instead PUT request should be used here.例如,GET 请求不应修改资源,而应在此处使用 PUT 请求。 I was asked this question during an interview and couldn't answer this satisfactorily-- I am designing a calculator that provides following functions, add, multiply, divide, subtract on two operands.我在面试时被问到这个问题,无法令人满意地回答这个问题——我正在设计一个计算器,它提供以下功能,对两个操作数进行加、乘、除、减。 How do I expose these methods to clients following REST principles.我如何遵循 REST 原则向客户端公开这些方法。 What URI to use for these operations?用于这些操作的 URI 是什么? And I am not sure whether to map an add operation to GET,PUT or POST.而且我不确定是否将添加操作映射到 GET、PUT 或 POST。 Same for the other operations (divide, multiply etc).其他操作(除法、乘法等)也是如此。 What are the guidelines here ?这里的指导方针是什么?

What are the guidelines here ?这里的指导方针是什么?

How would you do it with a web site?你会怎么做一个网站?

That should be your first heuristic any time somebody asks you about REST .每当有人向您询问REST时,这应该是您的第一个启发式方法。 REST is an REST 是一个
architectural style , designed for "long-lived network-based applications that span multiple organizations". 架构风格,专为“跨多个组织的长寿命的基于网络的应用程序”而设计。 The reference application for this architectural style is the World Wide Web.这种架构风格的参考应用程序是万维网。

I am designing a calculator that provides following functions, add, multiply, divide, subtract on two operands.我正在设计一个计算器,它提供以下功能,对两个操作数进行加、乘、除、减。 How do I expose these methods to clients following REST principles.我如何遵循 REST 原则向客户端公开这些方法。

There are three pieces of information that the client has, which need to be communicated to the server - the operation and the two operands.客户端拥有三条信息,需要与服务器通信——操作和两个操作数。 One the web, the usual way to collect that sort of information is to provide a form.在网络上,收集此类信息的常用方法是提供表格。 In this case, it might be a dropdown list of operations, and a couple of text controls to accept numbers.在这种情况下,它可能是一个操作下拉列表和几个接受数字的文本控件。 Because a pure function is a safe , we would probably use GET as the form method.因为纯函数是安全的,我们可能会使用GET作为表单方法。 So the HTML processing rules would take the values described by the form and transcribe them as key value pairs in the query part.因此,HTML 处理规则将采用表单描述的值并将它们转录为查询部分中的键值对。

So the url would look something like所以网址看起来像

/22520c7f-6207-490e-99c9-bd1bb37f4056?op=add&firstArg=6&secondArg=9

The key generalization is to realize that the HTML form is playing the role of a URI Template - the server passes the template to the client, the client fills in the details and uses the result as the target of the request.关键的概括是要意识到 HTML 表单正在扮演URI 模板的角色——服务器将模板传递给客户端,客户端填写详细信息并将结果作为请求的目标。

What this implies is that if you wanted to replace HTML with some other media type, you would need to specify a description for URI Templates, some mechanism for describing the acceptable ranges and what values might be reasonable defaults.这意味着如果您想用其他媒体类型替换 HTML,您需要指定 URI 模板的描述、一些用于描述可接受范围的机制以及哪些值可能是合理的默认值。

What URI to use for these operations?用于这些操作的 URI 是什么?

With REST?用 REST? absolutely does not matter.绝对没有关系。 That's part of the point -- the client treats URI as opaque values.这就是重点的一部分——客户端将 URI 视为不透明值。

URI are just identifiers ; URI 只是标识符 you can think of them like variable names in a program.您可以将它们视为程序中的变量名。 The machines just don't care what the spellings are (so long as those spellings are consistent with RFC 3986).机器只是不关心拼写是什么(只要这些拼写与 RFC 3986 一致)。

Since the spelling doesn't matter, you can use whatever the local spelling conventions happen to be.由于拼写无关紧要,您可以使用任何当地的拼写约定。 A lot of people favor "hackable" URI -- the spelling of the identifier communicates useful information to the human reader.很多人喜欢“可破解”的 URI——标识符的拼写向人类读者传达了有用的信息。

URI are identifiers of resources ; URI 是资源的标识符; "any information that can be named can be a resource". “任何可以命名的信息都可以是资源”。 This motivates some of the "nouns not verbs" noise -- the resources are web pages, or documents, or images, or scripts, or ... the "resource" concept is deliberately vague, so that it can be used flexibly.这引发了一些“名词而不是动词”的噪音——资源是网页,或文档,或图像,或脚本,或者……“资源”概念故意模糊,以便灵活使用。

I am not sure whether to map an add operation to GET,PUT or POST.我不确定是否将添加操作映射到 GET、PUT 或 POST。

The key is to look at the semantics of the client's request.关键是看客户端请求的语义。 Anytime what you are doing is a query/lookup, something that is OK for the machine to do automatically for the user, then you are looking at safe semantics, and the method is likely to be GET or HEAD (special circumstances).任何时候您正在做的是查询/查找,机器可以自动为用户执行某些操作,那么您正在查看安全语义,并且该方法可能是 GET 或 HEAD(特殊情况)。

If we were asking the server to change the representations of its own resources, then PUT and POST come into play.如果我们要求服务器改变它自己资源的表示,那么PUTPOST发挥作用。

In this case, all of these operations are just doing lookups, so GET is appropriate.在这种情况下,所有这些操作都只是进行查找,因此GET是合适的。

(An interesting thing to note is that the none of these operations depend on server state; they are pure functions. So it might make sense to serve the client with code on demand -- javascript or some reasonable replacement -- and use the client's processor to perform the calculations rather than doing a bunch of round trips across the network). (需要注意的一个有趣的事情是,这些操作都不依赖于服务器状态;它们是纯函数。因此,使用code on demand为客户端提供服务可能是有意义的——javascript 或一些合理的替代品——并使用客户端的处理器执行计算,而不是在网络上进行大量往返)。

I think one issue with the term REST is that it can have a different meaning to different people.我认为 REST 一词的一个问题是它对不同的人可能有不同的含义。 It's possible that the interviewer has a different understanding of REST.面试官可能对 REST 有不同的理解。 When something like this comes up, my first inclination is to try to figure out what REST means to them.当出现这样的事情时,我的第一个倾向是尝试弄清楚 REST 对他们意味着什么。 Do they mean hypermedia?他们是指超媒体吗? Do they care about the verbs and nouns being used correctly as you mentioned?他们是否关心您提到的动词和名词的正确使用? Or is in their mind REST just some HTTP endpoint that returns JSON.或者在他们看来 REST 只是一些返回 JSON 的 HTTP 端点。 All of these are possible.所有这些都是可能的。

I would be inclined to answer the question as follows I think:我倾向于回答这个问题,我认为:

GET /add/1/2
GET /multiply/5/6
etc..

Others with much more experience than myself have answered with examples of good ways to do this.其他比我更有经验的人已经回答了这样做的好方法的例子。

I'd just like to point out what a really excellent question this is.我只想指出这是一个非常好的问题。 I'm reading a paper by Richard Taylor and others from UC Irvine.我正在阅读 Richard Taylor 和其他来自加州大学欧文分校的论文。 Richard Taylor was Roy Fielding's advisor back when Fielding described REST in his PhD thesis.当 Fielding 在他的博士论文中描述 REST 时,Richard Taylor 是 Roy Fielding 的顾问。 In a discussion comparing REST and SOAP, the paper includes this:在比较 REST 和 SOAP 的讨论中,该论文包括以下内容:

"...the closer the service semantics are to those of content, the more likely the service is to have a rich REST API. “...服务语义越接近内容语义,服务就越有可能拥有丰富的 REST API。

... ...

the division between REST and SOAP may reflect a lack of design guidance; REST 和 SOAP 之间的区别可能反映了缺乏设计指导; how can services that are not content-centric, such as auction bidding ... be cleanly constructed in a REST-consistent manner?"不以内容为中心的服务,例如拍卖……如何以 REST 一致的方式干净地构建?”

When they say "closer to content" they mean that the service looks more like some dynamic or static information to be retrieved by the end user, as opposed to a service looks like something that actively performs some function or calculation for the user.当他们说“更接近内容”时,他们的意思是服务看起来更像是最终用户要检索的一些动态或静态信息,而不是服务看起来像是为用户主动执行某些功能或计算的东西。

They go on to provide additional guidance on this - but I haven't finished reading the paper yet, so I'll let you look that up for yourself.他们继续为此提供额外的指导——但我还没有读完这篇论文,所以我会让你自己查一下。 :-) :-)

Search the web for: From Representations to Computations: The Evolution of Web Architectures, Richard N. Taylor, UCI在网络上搜索:从表示到计算:Web 架构的演变,Richard N. Taylor,UCI

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

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