Using the Eve DB layer without HTTP

In my application, the MongoDB collection needs to be updated using a server-side script job (IE: a cron job that resets / removes from other APIs every 30 minutes). I really want to make updates for MongoDB collections, but the data must be checked against the schema and include metadata (updated, created, etc.).

Two ways to solve this:

  • Ask the fake client to execute HTTP POST / PUT / PATCHES. However, this means that this fake client will have to deal with things like authentication / authorization / last-modified-s.
  • Use PyMongo to interact directly with the database. However, this means that I would not have data validation or stored metadata.

Does Eve have database hooks so that I can do updates to the Eve database without HTTP?

+6
source share
3 answers

I managed to run this in a separate script that can be run periodically by jenkins. The application in run.py that I import is the one I had by the end of eve quickstart .

from run import app from eve.methods.post import post_internal payload = { "firstname": "Ray", "lastname": "LaMontagne", "role": ["contributor"] } with app.test_request_context(): x = post_internal('people', payload) print(x) 

post_internal runs eve.utils.parse_request, which relies on flask.request, so it requires with app.test_request_context() . app.app_context() not enough for this method.

Read the docs for appcontext and reqcontext if you are new to the bulb (like me).

+2
source

Starting with version v0.5 (currently on the development branch, but you can immediately pull it out and use it), you can use post_internal to add data:

  Intended for internal post calls, this method is not rate limited, authentication is not checked and pre-request events are not raised. Adds one or more documents to a resource. Each document is validated against the domain schema. If validation passes the document is inserted and ID_FIELD, LAST_UPDATED and DATE_CREATED along with a link to the document are returned. If validation fails, a list of validation issues is returned. 

It probably makes sense to add more internal methods to cover all the CRUD operations that are now available through HTTP. However, you can still reference them.

Update: v0.5 was released using the _internal methods available for all CRUD operations.

+3
source

If you want to do the following at a user endpoint ...

  • accept some POST ed data
  • somehow manipulate the data.
  • execute post_internal()
  • returns the result as an answer

... you would do something like this:

 from eve.methods.post import post_internal from eve.render import send_response def my_custom_endpoint(**kwargs): data = json.loads(request.data.decode()) # <manipulate data here> resp = post_internal('crew', data) return send_response('crew', resp) 

In fact, you're probably better off using Eve Event Hooks to do such things. But in the event of a situation where Event Hooks is not distributed, this approach may be useful.

+1
source

Source: https://habr.com/ru/post/973846/


All Articles