如何最好地设计出多个过滤器一个REST API? [英] How best to design a REST API with multiple filters?

查看:254
本文介绍了如何最好地设计出多个过滤器一个REST API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为个人的编程项目,我的工作我的刮大学的课程目录,并为REST API提供的数据。我已经成功地刮的所有数据,并存储在数据库中,和我现在工作的API。

As a personal programming project, I am working on scraping my University's course catalog and providing the data as a REST API. I have successfully scraped all the data and stored it in a database, and am now working on the API.

这些课程可以多种标准的基础上进行过滤:教练,大学,学分,时间,日期等。

The courses can be filtered on the basis of many criteria: instructor, college, credits, time, day etc.

什么是在这种情况下提供了一个API的最佳方式?

What is the best way to provide an API in this situation?

选项1

提供众多的URL,如


example.com/api/byinstructor/<instructor$c$c>
example.come / API / bycollege /&LT;大专code&GT;
example.com/api/bycollegeandinstructor/<college$c$c>/<instructor$c$c>
...等等

我需要对所有排列的URL。这似乎很繁琐,无论是对我和API消费者,并非常不干燥。

I would need to have a URL for all permutations. This seems very cumbersome, both for me and the API consumers, and very un-DRY.

选项2

只有像的主要选项提供的API:

Only provide APIs for the major options like:


example.com/api/byinstructor/<instructor$c$c>
example.come / API / bycollege /&LT;大专code&GT;

如果消费者想要 bycollegeandinstructor ,他是不是最终的过滤。

And if the consumer wants bycollegeandinstructor, he does the filtering on his end.

选项3

用户通过JSON字符串给我,我用它来获取过滤标准

The user passes a JSON string to me, and I use that to get the filtering criteria

example.com/api/getcourses/<jsonstring>

jsonstring = 
{ 
  instructor:<instructorcode>,
  college:<collegecode>,
  ...and so on
}

我想,而不是JSON字符串,我也可以要求一个POST阵列,但似乎未inituitive为消费者,因为他是歌厅的数据。

I suppose instead of the Json string, I could also require a POST array, but that seems un-inituitive for the consumer since he is GETing data.

还是没有这样做,我不知道的另一种方式?如果是,是最好的选择第三个选项,可能你prepare一个JSON字符串的基础上的SQL查询提供了一个简短的总结最好的,可能有变量值的数量?

Or is there another way of doing this I am not aware of? If it is the third option that is the best option, could you provide a short summary best to prepare a SQL query on the basis of a JSOn string that may have variable number of values?

推荐答案

要扩大从J.F.答案,这听起来像你有一个资源,集课程,这将是在URI:

To expand on the answer from J.F., it sounds like you have one resource, the set of courses, which would be at the URI:

/courses

过滤功能,资源通常使用查询参数来完成,以过滤单一的资源,例如:

Filtering that resource is usually accomplished using query parameters to filter that single resource, e.g:

/courses?college=123&instructor=321

通过这样做,你避免所有可能的排列创造资源的扩散问题。

By doing this, you avoid the issue with all possible permutations creating a proliferation of resources.

从根本上:有一个资源,其可以被过滤为必要

Fundamentally: There's one resource, which can be filtered as necessary.

这篇关于如何最好地设计出多个过滤器一个REST API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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