uhd-urz / elAPI

An extensible API client for eLabFTW
GNU Affero General Public License v3.0
5 stars 0 forks source link

Introduce reinitiate_config - [merged] #110

Closed alexander-haller closed 1 month ago

alexander-haller commented 1 month ago

In GitLab by @mhxion on Jun 27, 2024, 02:52

Merges simple-config-initiation -> dev

This is mainly introduced and needed for "elAPI as a library" users. I.e., developers who will be using elAPI to write simple Python scripts, but not plugins. elAPI handles the initiation and validation of configuration for plugins itself. reinitiate_config replaces doing explicit configuration validation that we introduced in !55.

When we run elAPI from the CLI, it handles the configuration validation for us among many other types of validations. E.g., if export_dir: True is defined which doesn't make sense for export_dir as it must be a string (directory path), elAPI, instead of throwing an error or failing, uses a fallback export directory when we need to export something. We can check which export directory elAPI is using from elapi show-config. However, in the same situation, if we are using elAPI in a Python script, and we try to export a GETRequest response, elAPI will not use a fallback directory and fail. That's because no "smart" configuration validation is run automatically for scripts. This is meant to give a Python developer more control over elAPI and its behavior by making the least number of decisions. If we want to have the similar validation as elAPI does for CLI commands, we would have to just run this reinitiate_config function. _In other words, reinitiate_config is necessary for Python scripts, if a strict and smart validation of elapi.yml is desired._

from elapi.configuration import reinitiate_config
from elapi.api import GETRequest

reinitiate_config() # Validates and applies fallback to configuration from elapi.yml

session = GETRequest()
print(session("info").json())
alexander-haller commented 1 month ago

In GitLab by @mhxion on Jun 27, 2024, 02:56

I am not requesting a review as this is mainly a refactor of !55 (section: "Robust configuration") which had been approved.

alexander-haller commented 1 month ago

In GitLab by @mhxion on Jun 27, 2024, 02:56

approved this merge request

alexander-haller commented 1 month ago

In GitLab by @mhxion on Jun 27, 2024, 02:56

unapproved this merge request