I have a resource in eve e.g. ABC, I want to manipulate another resource e.g. BCD when some condition meet while I am posting a new item to ABC, I know I can hook the event for post/pre_POST_ABC but is there a 'internal' way to do post on BCD without going through via the HTTP again?
Asked
Active
Viewed 2,237 times
1 Answers
7
In your callback function you could either:
A) use the data driver to store data directly to the database
Something like this:
def update_ABC(request, payload):
accounts = app.data.driver.db['abc_collection']
account = accounts.insert(docs)
app = Eve()
app.on_post_POST_ABC += update_ABC
app.run()
Would do the trick. You would be bypassing the framework this way, and storing directly on the database.
B) Use app.test_client.post()
to POST through directly through the application.
app.test_client().post('/bcd', json.dumps({"field":"value"}, content_type='application_json'))
This is probably a better option since the request goes through the framework (meta fields like data_created
are handled for you.)
Update: With v0.5+ you can now use post_internal
to achieve the same result. There are equivalent internal methods available for other CRUD methods as well.

Nicola Iarocci
- 6,606
- 1
- 20
- 33
-
option 2 is exactly what I am looking for, however it does not seems to be working? app.post is an 'instance' type instead of a func? – John Mar 05 '14 at 21:49
-
I also tried to do requests.post(url, data, headers) but it is freezing eve .... however using requests is not ideal as there will be some http overhead. ideally to call some eve internal function to do the post? – John Mar 05 '14 at 22:10
-
I realise, @nicola-iarocci , I can do eve.methods.post, however with this approach, some of the associated event hook is no longer working e.g. on_post_POST_BCD is not triggered, could you please advise? thanks – John Mar 06 '14 at 00:09
-
I fixed the snippet for option 2. Sorry about that. – Nicola Iarocci Mar 06 '14 at 06:55
-
thanks! it works like a charm! btw, so we properly can only know this kind of trick by reading the code or .. asking on stackoverflow? – John Mar 06 '14 at 08:16
-
The thing is, test_client() is a Flask feature not Eve's. On the docs I'm focusing on Eve features. But yes I will add a developers guide as soon as the framework is mature enough. – Nicola Iarocci Mar 06 '14 at 09:00
-
1For option B) I needed to add the following: `app.test_client().post('/bcd', data=json.dumps({"field":"value"}), content_type='application/json')` – kynan Apr 21 '14 at 21:57