Have you looked into the on_insert
hook? From the documentation:
When documents are about to be stored in the database, both
on_insert(resource, documents)
andon_insert_<resource>(documents)
events are raised. Callback functions could hook into these events to arbitrarily add new fields, or edit existing ones.on_insert
is raised on every resource being updated whileon_insert_<resource>
is raised when the<resource>
endpoint has been hit with a POST request. In both circumstances, the event will be raised only if at least one document passed validation and is going to be inserted.documents
is a list and only contains documents ready for insertion (payload documents that did not pass validation are not included).
So, if I get what you want to achieve, you could have something like this:
def telnet_service(resource, documents):
"""
fetch data from telnet device;
update 'documents' accordingly
"""
pass
app = Eve()
app.on_insert += telnet_service
if __name__ == "__main__":
app.run()
Note that this way you don't have to mess with the database directly as Eve will take care of that.
If you don't want to store the telnet data but only send it back along with the fetched documents, you can hook to on_fetch
instead.
Lastly, if you really want to use the data layer you can use app.data.driver
as seen in this example snippet.