如何通过RESTful API公开有关资源的统计信息?

ilu*_*mos 7 rest laravel

我建立了一个API我的web应用程序,并尽可能公开所有资源,我的应用程序使用,例如,它已得到了/users,/roles,/posts没有问题等.

我现在仍然坚持如何以RESTful方式公开其中一些资源的统计信息.拥有statistics资源似乎没有权利GET /statistics/1,结果可能会改变每个请求,因为统计数据是实时的,因此它不会被缓存.

背景:

对于系统中的每个人/users,应用程序会定期查询Steam /games正在播放的API,以及他们正在播放的API /servers,并将此信息与/states资源中的时间戳一起存储.

此类信息汇总显示最流行的游戏和服务器上的理货/statistics/games/current-usagestatistics/servers/current-usage标准的HTML页面.说明性截图:服务器,游戏(在不同时间拍摄).

编辑:基本资源的示例数据

"state": {
    "id": 292002,
    "user_id": 135,
    "game_id": 24663,
    "server_id": 135,
    "created_at":"2014-06-22 21:12:03"
},
"user": {
    "id": 112,
    "username": "ilumos",
    "steam_id_64": "76561197970613738"
},
"server": {
    "id": 135,
    "application_id": 24663,
    "name": null,
    "address": "192.168.241.65",
    "port": "0"
},
"game": {
    "id": 24663,
    "name": "DEFCON",
    "steam_app_id": 1520
}
Run Code Online (Sandbox Code Playgroud)

编辑2:REST是否允许使用时间戳作为资源标识符的端点?例如:

GET /statistics/1403681498/games 得到这样的回应:

[
    "game": {
        "id": 123,
        "name": "DEFCON",
        "users": [
            {
                "id": 7654,
                "username": "daryl",
                "server": {
                    "id": 127,
                    "ip": "123.123.123.123",
                    "port": "27960"
                }
            },
            {
                "id": 135,
                "username": "ilumos"
            },
        ]
    }
]
Run Code Online (Sandbox Code Playgroud)

Eri*_*ein 6

你有各种不完全不合理的选择.

您可以

  1. 包括每个响应的统计信息.所有客户都想要统计吗?有很多统计数据吗?也许像GET /游戏这样的东西?orderBy = numPlayers-&offset = 0&limit = 10如果所有你跟踪的是玩家数量都会有效.
  2. 有一个/statistics/{statisticId}终点.这本身并不是不合时宜的.
  3. 有一个/games/{gameId}/statistics终点.
  4. 有一个/statistics/games/{gameId}终点.

真的,我们没办法告诉你实现这个的最佳方法是什么,因为我们没有足够的信息.


ilu*_*mos 1

我将继续创建资源usage,因为所有这些统计数据都将是其他资源的使用情况,无论是“现在”还是在历史时间点。

我的 URI 将如下所示: GET /usage/{resource-name}/{resource-id}

GET /usage/games/                           collection of games in use right now (with user totals)
GET /usage/servers/                         collection of servers in use right now
GET /usage/games/?timestamp=1234567890      collection of games in use at {timestamp}

GET /usage/games/1                          usage of game with id 1 right now
GET /usage/games/1?timestamp=1234567890     usage of game with id 1 at {timestamp}
GET /usage/games/?user_id=123               usage of game with id 1 filtered to show only user with id 123
Run Code Online (Sandbox Code Playgroud)

将来我可以将资源扩展到例如用电的返回使用

GET /usage/phases/                          collection of phases in use right now (with power draw totals)
GET /usage/phases/1                         usage of phase with id 1 right now
GET /usage/phases/?timestamp=1234567890     collection of phases in use at {timestsamp} (with power draw totals)
Run Code Online (Sandbox Code Playgroud)

除非这有本质上不安全的地方,否则这似乎是公开此信息的最合适的方式。