Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
子资源的API设计?_Api_Http_Rest - Fatal编程技术网

子资源的API设计?

子资源的API设计?,api,http,rest,Api,Http,Rest,我正在构建一个相当复杂的web API,但我有点困惑的是子资源的创建 具体来说,从游戏的角度考虑 一场比赛属于一轮,一轮属于一个赛季,一个赛季属于一场比赛。 在其父上下文之外,每个项目都没有相关性,但它们都是资源 所以我想知道我的url模式应该是什么来创建一个游戏 //full tree map PUT /competitions/1/seasons/2/rounds/3/games //each sub resource has it's own top level, but must be

我正在构建一个相当复杂的web API,但我有点困惑的是子资源的创建

具体来说,从游戏的角度考虑

一场比赛属于一轮,一轮属于一个赛季,一个赛季属于一场比赛。 在其父上下文之外,每个项目都没有相关性,但它们都是资源

所以我想知道我的url模式应该是什么来创建一个游戏

//full tree map
PUT /competitions/1/seasons/2/rounds/3/games

//each sub resource has it's own top level, but must be created under 
//it's parent
PUT /rounds/3/games 

//each sub resource has it's own top level, and we include the parent 
//id in the resource body.
PUT /games

我更喜欢使用/games,因为它有自己的顶级,因为它更深入,例如,一个游戏有一个统计数据,一个统计数据有一个视频,所以做完整的树映射可能会变得很难处理,也许我应该支持所有三个?

典型的模式是将顶级作为资源的规范URI,但是允许你离开整棵树。因此:

GET /competitions/1/seasons/2/rounds/3/games

GET /games
GET /games/12
PUT /games
    competition=1
    season=2
    round=3
这样做会给您带来更大的支持负担。你确定值得吗

GET /games?competition=1&season=2&round=3
这也是合理的。我希望竞赛资源的部分响应是该竞赛季节的URI列表

我建议不要有多个URI来支持PUT、POST、补丁或删除。您的代码将很快成为令人头痛的问题