在Eve中,如何创建集合的子资源并保持父集合的端点?

发布于 2021-01-29 16:04:02

我想要这三个端点:

/games
/images
/games/<game_id>/images

这是我的settings.py文件的摘录

#...

games = {
    "schema": {
        "title": {
            "type": "string",
            "required": True
        },
        "name": {
            "type": "string",
            "required": True
        },
    }
}

images = {
    "schema": {
        "game_id": {
            "type": "string",
            "required": True,
        },
        "title": {
            "type": "string",
            "required": True,
        },
    },
    "url": "games/<regex('[a-f0-9]{24}'):game_id>/images"
}
#...

如果省略url属性,则在获取/时会得到两个预期的端点:

/games

/images

但是,如果您包含url属性,则不能点击/ images,而只能点击/ games,/games/<game_id>/images如下所示:

{
    "_links": {
        "child": [
            {
                "href": "/games/<regex('[a-f0-9]{24}'):game_id>/images",
                "title": "games/<regex('[a-f0-9]{24}'):game_id>/images"
            },
            {
                "href": "/games",
                "title": "games"
            }
        ]
    }
}

如何保留收藏 图像, 并仍通过子资源查询使其文档可用?

关注者
0
被浏览
48
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    您可以设置3个不同的终结点,而其中两个则消耗相同的数据库资源(图像)。像这样:

    images_schema: {
      "game_id": {
        "type": "string",
        "required": True,
      },
      "title": {
        "type": "string",
        "required": True,
      },
    }
    
    games = {
      "schema": {
        "title": {
          "type": "string",
          "required": True
        },
        "name": {
          "type": "string",
          "required": True
        },
      }
    }
    
    images = {
      "schema": images_schema,
      "url": "images"  # not really needed
    }
    
    games_images = {
      "schema": images_schema,
      "url": "games/<regex('[a-f0-9]{24}'):game_id>/images",
      "datasource": {"source": "images"}
    }
    

    作为参考,请参阅多个API端点,一个数据源



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看