0

I have the following on_fetch hook, which populates a resource before it is retrieved the first time:

# bootstrap resources with default data if it's not already there
def before_returning_resource(resource, documents):
    if resource == 'vlans' or resource == 'switches':
        if not documents:
            r = app.data.driver.db[resource]
            filename = 'bootstrap_' + resource + '.json'
            with open(os.path.join(script_dir, filename), 'r') as f:
                to_insert = json.loads(f.read())
            r.insert(to_insert)
            documents.extend(to_insert)

On the first attempt to load the collection, the data it returns does not include the extra HATEOAS stuff like _links etc because it gets added upon insertion, and I have to return something now. After I reload it again, the stuff is there. Any ideas on how I can re-retrieve the data during this first request so that it includes all the things that would come in a normal request?

Example first request:

{
    "_items": [
        {
            "_id": "5307a7301f72fa82ae83474a",
            "ip_address": "10.50.159.196",
            "name": "RCG 1G Core Switch",
            "dns": "switch196",
            "transport": "telnet"
        },
        {
            "_id": "5307a7301f72fa82ae83474b",
            "ip_address": "10.47.152.2",
            "name": "Rack E1 sw1",
            "dns": "re1-sw1",
            "transport": "telnet"
        },
        ...

Example subsequent request:

{
    "_items": [
        {
            "_updated": "Thu, 01 Jan 1970 00:00:00 GMT",
            "name": "RCG 1G Core Switch",
            "_links": {
                "self": {
                    "href": "127.0.0.1:5000/switches/5307a7301f72fa82ae83474a",
                    "title": "Switche"
                }
            },
            "dns": "switch196",
            "_created": "Thu, 01 Jan 1970 00:00:00 GMT",
            "_id": "5307a7301f72fa82ae83474a",
            "ip_address": "10.50.159.196",
            "_etag": "4359b74209189b0bbbbfd4b92e647d84d10ede47",
            "transport": "telnet"
        },
        ...
cayblood
  • 1,838
  • 1
  • 24
  • 44

1 Answers1

0

So basically you are returning fake items, that do really not exist in your database, expecting the client to store them later? Why would you want to provide links to something that does not exist yet? A following GET on the document link would achieve a 404 anyway.

You should probably pre-populate your database with this data. The links would then be there, they would be legit (they would work), and you would not need to play tricks with the on_fetch hook.

Also, while you could fake 90% of the document link within in your hook, you could not know the real object_id that the new document will be assigned to once it is stored.

Nicola Iarocci
  • 6,606
  • 1
  • 20
  • 33
  • No, these are real items, as you can see from the fact that the insert does manage to populate the _id correctly. (Notice that the _id of the first element matches in the first and second request) What's happening is that I'm inserting these during the first request and returning them immediately. Interestingly enough, the call to ```r.insert``` does seem to add _ids to the items, but it doesn't add the other stuff. – cayblood Feb 23 '14 at 09:26
  • Is there any way to send back a redirect in an on_fetch call? That way I could send the user back to the same resource and have them re-retrieve it. – cayblood Feb 24 '14 at 14:26