In Eve, how can you make a sub-resource of a collection and keep the parent collections endpoint?

≯℡__Kan透↙ 提交于 2019-12-18 13:33:46

问题


I want these three endpoints:

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

Here's an excerpt from my settings.py file

#...

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"
}
#...

If you leave out the url property, you get two expected endpoints when you GET /:

/games

/images

But if you include the url property, you can't hit /images and instead you can only hit /games and /games/<game_id>/images as shown here:

{
    "_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"
            }
        ]
    }
}

How can I keep the collection images and still make its documents available through a sub-resource query?


回答1:


You could setup 3 different endpoints whereas two of them are consuming the same database resource (images). Something like this:

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"}
}

For reference, see Multiple API Endpoints, One Datasource.



来源:https://stackoverflow.com/questions/26166344/in-eve-how-can-you-make-a-sub-resource-of-a-collection-and-keep-the-parent-coll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!