无论是通过一个ID或一个RESTful API鼻涕虫确定项目 [英] Identify item by either an ID or a slug in a RESTful API

查看:170
本文介绍了无论是通过一个ID或一个RESTful API鼻涕虫确定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在设计一个API和我来到跨一个小问题:
一个RESTful API的URL应该怎么样子时,你应该能够通过任何一个ID或蛞蝓,以确定一个项目?

I'm currently designing an API and I came a cross a little problem: How should a URL of a RESTful API look like when you should be able to identify an item by either an ID or a slug?

我能想到的三个选项:

GET /items/<id>
GET /items/<slug>

这要求蛞蝓和ID是可区别的,其不一定在这种情况下,给定的。我不认为这个问题一个干净的解决方案,除了你做这样的事情:

This requires that the slug and the ID are distinguishable, which is not necessarily given in this case. I can't think of a clean solution for this problem, except you do something like this:

GET /items/id/<id>
GET /items/slug/<slug>

这会工作得很好,但是这不是我想无论是塞或ID来标识项目的唯一地方,它会很快就会非常难看当一个人想要实现其他的操作相同的方法。这只是不是很可扩展性,这使我们这个方法:

This would work fine, however this is not the only place I want to identify items by either a slug or an ID and it would soon get very ugly when one wants to implement the same approach for the other actions. It's just not very extendable, which leads us to this approach:

GET /items?id=<id>
GET /items?slug=<slug>

这似乎是一个很好的解决方案,但我不知道这是否是人们所期望的是什么,因此它可能导致由于不正确的使用令人沮丧的错误。此外,它不是那么容易 - 或者让我们说清洁 - 来实现这一个路由。然而,这将是易于扩展,并会看起来非常相似的方法获得多个项目

This seems to be a good solution, but I don't know if it is what one would expect and thus it could lead to frustrating errors due to incorrect use. Also, it's not so easy - or let's say clean - to implement the routing for this one. However, it would be easily extendable and would look very similar to the method for getting multiple items:

GET /items?ids=<id:1>,<id:2>,<id:3>
GET /items?slugs=<slug:1>,<slug:2>,<slug:3>

但是,这也是一个缺点:如果有人想找出一些他想和ID来获取项目,但与塞其他人呢?混合这些标识符将不容易实现这一点。

But this has also a downside: What if someone wants to identify some of the items he want to fetch with IDs, but the others with a slug? Mixing these identifiers wouldn't be easy to achieve with this.

什么是这些的问题的最佳和最广泛接受的解决方案
一般情况下,是什么在设计这样的API事项?

What is the best and most widely-accepted solution for these problems? In general, what matters while designing such an API?

推荐答案

三我preFER第三个选项,它的并不少见看到语法;例如Twitter的API的部分允许语法:
<一href=\"https://dev.twitter.com/docs/api/1/get/statuses/show/%3Aid\">https://dev.twitter.com/docs/api/1/get/statuses/show/%3Aid

Of the three I prefer the third option, it's not uncommon to see that syntax; e.g. parts of Twitter's API allow that syntax: https://dev.twitter.com/docs/api/1/get/statuses/show/%3Aid

第四个选择是一种混合的方法,你挑一个(比方说,ID)作为单品的典型接入方式,也让基于蛞蝓查询。例如:

A fourth option is a hybrid approach, where you pick one (say, ID) as the typical access method for single items, but also allow queries based on the slug. E.g.:

GET /items/<id>
GET /items?slug=<slig>
GET /items?id=<id>

您将路由映射明显/项目/身份证/项目?ID =

Your routing will obvious map /items/id to /items?id=

可扩展到多个IDS /蛞蝓,但仍符合匹配的URI底层数据模型的REST模式。

Extensible to multiple ids/slugs, but still meets the REST paradigm of matching URIs to the underlying data model.

这篇关于无论是通过一个ID或一个RESTful API鼻涕虫确定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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