REST风格的URL设计:大众VS私有API,API hierhachy设计模式,URI VS URL设计? [英] RESTful URL design: public vs private API, hierhachy API design pattern, URI vs URL design?

查看:200
本文介绍了REST风格的URL设计:大众VS私有API,API hierhachy设计模式,URI VS URL设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我常常陷入的问题就是这样,非常相似,这层次REST风格的URL设计

I often get into issue like this, very similar to this Hierarchical RESTful URL design

咋办服务不仅提供用户上传的文档。

Supposed the service only offers the user to upload a document.

POST, GET /accounts
PUT, DELETE /accounts/{name}  or /accounts/{id}

现在文档被附连到特定的用户,不论是公共的或根本不是关心这里不

Now a document is attached to a specific user, whether it is public or not at all is not of concerned here.

这两种方式是 POST /文件VS POST /用户/文件

为什么呢?由于创建一个文档资源后,当,该文件是用户的控制之下。所以,我希望有

Why? Because later when a document resource is created, that document is under the user's control. So I would expect to have

GET,PUT,DELETE /用户/ {名} /文件获取,修改和删除的用户自己的文件逃走了。

GET, PUT, DELETE /users/{name}/documents for getting, changing and deleting a bunk of documents the users own.

我可以有 GET,PUT,DELETE /用户/ {名} /文件/ {名称/ ID}

但同样可以实现只 /文件/ {用户} / ....或/文件/ {ID} 。这类似于一个如何组织UNIX文件(尽管 /用户/...也是另一种方式来组织文件...)你看,还有一个理念[URI VS URL设计的PHY。

But the same can be achieved just /documents/{users}/.... or /documents/{id}. And this is similar to how one might organize unix files (though /users/... is also another way to organize files...) You see, there is also a philosophy[phy of uri vs url design.

的另一个考虑是API是否是可见的用户或没有。如果这是一个简单的后端API,只有开发人员具有(后端< - 前端服务器16; - 前端AJAX),那么一个网站的用户可能会更快乐 /用户/ {名} /文件/ {ID /名称} 而如果API是公开的(如Twitter API)一些程序员会不喜欢这个长的URL。

Another consideration is whether the API is visible to user or not. If this is simply a backend API, only the developer has access (backend <- frontend server <- frontend ajax), then a site user may be happier with /users/{name}/documents/{id/name} while some programmers will dislike this long url if the API is public (like twitter api).

什么人认为这些问题?

推荐答案

务实,显然上述所有的都是正确的。

Pragmatically, obviously all of the above are correct.

但哲学,我认为它归结为REST的S......这真是什么是真正的资源是你管理状态的问题。如果您的应用程序旨在处理的文件,那么需要将资源是在URL中明显。如果您的应用程序更多的是执行一些工作流程的用户,那么很可能是你想在URL中的用户明显。

But philosophically, I think it comes down to the "S" in REST ... This is really a question of what the resource actually is that you are managing state for. If your app is intended to deal with documents, then that needs to be resource that is apparent in the URL. If your app is more about Users performing some workflow, then it is likely that you want to make the User apparent in the URL.

在内部应用程序,事情很快得到专为方便起见,这样就可以有一个网址,混合和匹配资源,使所有权明显(如你的例子)。它认为这种方式。用户来使用你的应用程序来处理自己的东西,其中一些文件。他们知道他们登录,然后他们可以访问他们的文件,他们的一些信息周游与他们的会话,并从这一角度

Internally in an app, things quickly get built for the sake of convenience, so that you can have URLs that mix and match resources to make ownership apparent (as in your example). Think of it this way. Users come to use your app to handle their stuff, some of which are documents. They know they logged in, and then they have access to their documents, some of their information is traveling around with them in a session, and from that perspective

user/{name}/documents

是有道理的。他们有背景

makes sense. They have context

如果你作出这样的API公开......也就是说,如果没有相同的观点在你的App用户谁的人正在消耗你的API ...那么面对公众需要清楚地说明。如果您的应用程序与其他应用程序的合同是您管理文档,那么我会说,你是关闭使这种明显的在您的网址更好。给用户的路线的概念是没有意义的,因为这里的背景是看不出来的。

If you make that API public ... that is, if people who don't have the same perspective as the user in your App are consuming your API ... then that public face needs to be really clear. If your app's contract with other apps is that you are managing documents, then I would say that you are better off making that apparent in your URL. The notion of a route to a user doesn't make sense here because the context is not apparent.

你带了一个有趣的例子,清楚地显示了这个pretty。以

You bring up an interesting example that shows this pretty clearly. Take

文件/ ID 文件/名称

文件的命名可以按照约定或者是应用特定的。很好的例子是图片分享应用或特定行业,如会计应用程序。但是,公共API不能假设的命名规则是明显或不明显(除非你允许的API访问消费者这样的信息)。因为它是一个面向公众的API,你最好还是不要用

The naming of files can follow conventions or be app specific. Good example are image sharing apps or apps for particular industries like accounting. But the public API cannot assume that the naming convention is apparent or obvious (unless you allow consumers of the API access to this kind of information). Because it is a public facing API, you are probably better off going with

document/id

由于IDS通常被理解为是不变的,甚至可以跨越一个API和语义是pretty清楚。

because ids are typically understood to be immutable, even across an API and the semantic is pretty clear.

最后,该技术可以做任何我们想要的。但事情就像路线和URL是很重要的理解语义的API本身。如果要管理什么的,消耗的API时,不应该由当地的或特定的技术的公约陷入下来,应该是显而易见的。

Ultimately, the technology can do anything we want. But things like routes and URLs are important to understand the semantic of the API itself. If you are managing something, that should be obvious when consuming the API, and should not get bogged down by your local or technologically-specific conventions.

这篇关于REST风格的URL设计:大众VS私有API,API hierhachy设计模式,URI VS URL设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆