As requested, I didn't raise a PR for this, instead asking the community first.
Would it be worthwhile to add a small handler for environment variables in pyodk?
Note that I created a branch/PR on my fork with these changes already, with tests and docs. If this is approved on the forums, I could make the PR against the official repo.
# the toml file variable is base_url
PYODK_BASE_URL=https://example.com
PYODK_USERNAME=user@example.com
PYODK_PASSWORD=yourpassword
PYODK_DEFAULT_PROJECT_ID=1
By small handler I basically just mean a little bit of code to handle the env vars - what I implemented in the PR
Interesting idea for the token too - the cache_path variable is in a lot of code though, so this would be quite a large refactor, perhaps best for another issue / v2?
Although doing that might have some complications. It's probably unlikely, but say someone is running pyodk in a service that has multiple replicas. It's easy to share the cache file between replicas and use the same token, but if this was set dynamically in the environment of a single replica, each replica would have a unique environment and wouldn't benefit from the shared session token. Not sure if this is a big deal though!
new_config = config.CentralConfig(
base_url="https://obj.config.com",
username="user@obj.config.com",
password="ConfigPassword",
)
with Client(config_path=new_config) as client:
return client.submissions.create(
project_id=xxx,
form_id=xxx,
xml=xxx,
)
The loading order would be:
Directly passed CentralConfig object.
Env vars.
User specified config_path.
Default config_path.
Error.
The reason I mention this is because I am using pyodk in an app where there are potentially multiple different ODK Central servers to contend with, so I need to easily swap between them. Doing this via env var or config file isn't ideal