作为一个个人编程项目,我正在抓取我大学的课程目录并以 REST API 的形式提供数据。我已成功抓取所有数据并将其存储在数据库中,现在正在开发 API。
课程可以根据许多标准进行过滤:教师、学院、学分、时间、日期等。
在这种情况下提供 API 的最佳方式是什么?
Option 1
提供大量 URL,例如
example.com/api/byinstructor/<instructorcode>
example.come/api/bycollege/<collegecode>
example.com/api/bycollegeandinstructor/<collegecode>/<instructorcode>
...and so on
我需要有一个所有排列的 URL。对于我和 API 消费者来说,这似乎非常麻烦,而且非常不干燥。
Option 2
只提供主要选项的 API,例如:
example.com/api/byinstructor/<instructorcode>
example.come/api/bycollege/<collegecode>
如果消费者想要bycollegeandinstructor
,他自己进行过滤。
Option 3
用户将 JSON 字符串传递给我,我用它来获取过滤条件
example.com/api/getcourses/<jsonstring>
jsonstring =
{
instructor:<instructorcode>,
college:<collegecode>,
...and so on
}
我想除了 Json 字符串之外,我还可以需要一个 POST 数组,但这对于消费者来说似乎不直观,因为他正在获取数据。
或者还有另一种我不知道的方法吗?如果第三个选项是最佳选项,您能否提供一个简短的摘要,以便根据可能具有可变数量值的 JSOn 字符串准备 SQL 查询?