Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

eve

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?

like image 472
sam Avatar asked Sep 29 '22 17:09

sam


1 Answers

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.

like image 52
Nicola Iarocci Avatar answered Oct 03 '22 01:10

Nicola Iarocci