nexus-forge
nexus-forge copied to clipboard
Refactor batch request mechanism
Motivated by: Future batch retrieval feature, that could benefit from the existing mechanism, under some changes. + Aligning batch/single request logics (remove duplicate logic)
For the methods: Deprecate, Tag, Update, Update Schema, Register:
-
The logic of knowing which http method to use, which url to target, what query params to use, what headers to use, what payload to give and what exception to throw on error is prepared in
prepared_methods.py
. This information will either be used by therequests.request
method (for synchronous calls, when performing these operations for one resource) oraiohttp.ClientSession.request
(for asynchronous calls, when performing these operations for multiple resources) -
When using
BatchRequestHandler.batch_request_for_resources
, the appropriateprepare_x
should be provided. Everything else can either be found in theservice
instance or thekwargs
. -
BatchRequestHandler.batch_request
provides a way to run a list of tasks. These tasks will be initialised using thetask_creator
parameter this method requires. -
In the case of the methods cited above (deprecate, tag, update…), they all have the same
task_creator
method, which is defined byBatchRequestHandler.create_tasks_for_resources
In the effort to remove duplication of logic, some differences were spotted:
- Batch registration only targeted POST regardless of the presence of an id specified in the payload (https://bluebrainnexus.io//docs/delta/api/resources-api.html#create-using-post). This has been fixed, and now batch registration targets either POST or PUT, like single-resource registration did before (and still does).
- The setting of the context after registration of a resource is done differently between single resource registration and multiple resource registration. https://github.com/BlueBrain/nexus-forge/blob/02da1021378ccd8bc231f45c8f40f941c6e282f5/kgforge/specializations/stores/bluebrain_nexus.py#L119 vs https://github.com/BlueBrain/nexus-forge/blob/02da1021378ccd8bc231f45c8f40f941c6e282f5/kgforge/specializations/stores/bluebrain_nexus.py#L190 This has not been addressed in this MR but should be in the future.