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