Skip to content

pycensus.api_keys

Resolve API keys without hardcoding them.

One standard place for every key of every package of the project: ~/.config/ptlos/api_keys.json, a JSON object {"<key name>": "<key>"} outside every repository (so it can never be committed). Order: explicit argument, the source's environment variable, then the keys file: API_KEYS_FILE (or PYCENSUS_API_KEYS_FILE) if set, else ~/.config/ptlos/api_keys.json; older locations (api_keys.json in the pyCensus folder, the working directory or its parents, ~/.pycensus/api_keys.json) are still read. Key names: api_keys.example.json.

save_api_key(name, key, path=None)

Store key under name in the standard keys file (~/.config/ptlos/api_keys.json, permissions 600), keeping the other keys.

Parameters:

Name Type Description Default
name str

Key name.

required
key str

The key.

required
path Optional[Path]

Keys file; default STANDARD_PATH.

None

Returns:

Type Description
Path

The keys file.

resolve_api_key(name, env_var=None, explicit=None)

Return the key for name or None if none is configured.

Parameters:

Name Type Description Default
name str

Key name in api_keys.json.

required
env_var Optional[str]

Environment variable checked first.

None
explicit Optional[str]

A key passed directly (wins).

None

Returns:

Type Description
Optional[str]

The key, or None.