Skip to content

Commit

Permalink
Make API caching configurable (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
edeckers authored Feb 27, 2022
1 parent 640f0a3 commit 1029fe3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/huemon/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,34 @@
COMMAND_PLUGINS_PATH = get_commands_path(CONFIG, "enabled")
HUE_HUB_URL = f"http://{CONFIG['ip']}/api/{CONFIG['key']}"
LOG = create_logger()
MAX_CACHE_AGE_SECONDS = int(CONFIG["cache"]["max_age_seconds"])
DEFAULT_MAX_CACHE_AGE_SECONDS = 10


def create_command_handlers(config: dict, api: ApiInterface, plugins: dict):
return reduce(
lambda p, c: {**p, c.name(): c(config, api)}, plugins, {})


def create_api(config: dict):
enable_cache = \
"cache" in config and \
"enable" in config["cache"] and \
bool(config["cache"]["enable"])

is_cache_age_configured = \
"cache" in config and \
"max_age_seconds" in config["cache"]

max_cache_age_seconds = \
int(config["cache"]["max_age_seconds"]) if \
is_cache_age_configured else \
DEFAULT_MAX_CACHE_AGE_SECONDS

api = Api(HUE_HUB_URL)

return CachedApi(api, max_cache_age_seconds) if enable_cache else api


class CommandHandler: # pylint: disable=too-few-public-methods
def __init__(self, handlers):
self.handlers = handlers
Expand Down Expand Up @@ -58,7 +78,7 @@ def main(argv):
command_handler_plugins = \
create_command_handlers(
CONFIG,
CachedApi(Api(HUE_HUB_URL), MAX_CACHE_AGE_SECONDS),
create_api(CONFIG),
load_plugins("command", COMMAND_PLUGINS_PATH, HueCommand))
LOG.debug("Finished loading command plugins (path=%s)",
COMMAND_PLUGINS_PATH)
Expand Down
1 change: 1 addition & 0 deletions src/huemon/config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: 1
ip: YOUR_HUB_IP
key: YOUR_HUB_API_KEY
cache:
enable: false
max_age_seconds: 10
# commands:
# available: /path/to/commands_available
Expand Down

0 comments on commit 1029fe3

Please sign in to comment.