【问题标题】:In Eve, how can you make a sub-resource of a collection and keep the parent collections endpoint?在 Eve 中,如何创建集合的子资源并保留父集合端点?
【发布时间】:2014-10-02 17:37:12
【问题描述】:

我想要这三个端点:

/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 属性,则在 GET / 时会得到两个预期端点:

/games

/images

但如果包含 url 属性,则无法点击 /images,而只能点击 /games 和 /games/&lt;game_id&gt;/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"
            }
        ]
    }
}

我怎样才能保留集合 images 并仍然通过子资源查询使其文档可用?

【问题讨论】:

    标签: python eve


    【解决方案1】:

    您可以设置 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"}
    }
    

    有关参考,请参阅Multiple API Endpoints, One Datasource

    【讨论】:

    • 那么如何 POST 到图像资源?
    • @frankV 这完全是另一个问题 :) 使用 curl 你可能会这样:$ curl -F "name=john" -F "pic=@profile.jpg" http://example.com/accounts
    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多