inveniosoftware / invenio-records-rest

Invenio records REST API module.
https://invenio-records-rest.readthedocs.io
MIT License
4 stars 63 forks source link

Accessing settings of REST endpoints from other modules #245

Open mesemus opened 5 years ago

mesemus commented 5 years ago

Hello folks,

Intro: I'm in the middle of implementing our institutional repository on top of invenio 3. For now I have two record types: Thesis (metadata about student thesis) and Attachment (for example fulltext, defence transcript, ... Each attachment has associated ACLs and potentially multiple files, such as original pdf and converted pdf/a). Thesis and n attachments are referenced via thesis id and for a couple of reasons I need to keep the two data types and not denormalize/merge them into one.

So far I've created two helper modules: acls providing generic mechanism for evaluating ACLs directly within elasticsearch queries and links defining LinkedObjectSerializerMixin(PreprocessorMixin) that in generic manner adds to thesis representation its associated attachments' metadata.

Problem: What I'm facing is that I need to reference settings of REST endpoints - for example, when Attachments are searched for inside the mixin I would like to use the appropriate RecordsSearch - the same that is used for accessing Attachment through REST. So far I've ended up with code bloated with obj_or_import_string:

rest_configuration = current_app.config['RECORDS_REST_ENDPOINTS'][self.other_side_rest_endpoint]
search = obj_or_import_string(rest_configuration.get('search_class', None), default=RecordsSearch)

Suggestion:

What about adding a dictionary current_records_rest.endpoints that would contain the configuration of endpoints that would be already resolved via the obj_or_import_string code that is now present at the beginning of create_url_rules ? https://github.com/inveniosoftware/invenio-records-rest/blob/e3a33fc3ff0e2fb5351cc040d3beb80533874557/invenio_records_rest/views.py#L237-L298

Then my module would just call

current_records_rest.endpoints[self.other_side_rest_endpoint].search_class 
or 
current_records_rest.endpoints[self.other_side_rest_endpoint]['search_class']

and would get the actual implementation of RecordsSearch.

If it makes sense I'm willing to create a pull request. Your opinions?

Thanks for all the work, so far I'm really happy with the ease of implementation and repository speed,

Mirek