-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support redis as cache backend for http requests and installati…
…on tokens
- Loading branch information
Showing
13 changed files
with
543 additions
and
350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# ******************************************************************************* | ||
# Copyright (c) 2024 Eclipse Foundation and others. | ||
# This program and the accompanying materials are made available | ||
# under the terms of the Eclipse Public License 2.0 | ||
# which is available at http://www.eclipse.org/legal/epl-v20.html | ||
# SPDX-License-Identifier: EPL-2.0 | ||
# ******************************************************************************* | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
from aiohttp_client_cache import CacheBackend | ||
from redis.asyncio.client import Redis | ||
|
||
|
||
class CacheStrategy(ABC): | ||
@abstractmethod | ||
def get_cache_backend(self) -> CacheBackend: ... | ||
|
||
|
||
_AIOHTTP_CACHE_DIR = ".cache/async_http" | ||
|
||
|
||
class _FileCache(CacheStrategy): | ||
def __init__(self, cache_dir: str): | ||
self._cache_dir = cache_dir | ||
|
||
def get_cache_backend(self) -> CacheBackend: | ||
from aiohttp_client_cache.backends import FileBackend | ||
|
||
return FileBackend( | ||
cache_name=self._cache_dir, | ||
use_temp=False, | ||
) | ||
|
||
|
||
class _RedisCache(CacheStrategy): | ||
def __init__(self, redis_uri: str, connection: Redis | None): | ||
self._redis_uri = redis_uri | ||
self._connection = connection | ||
|
||
def get_cache_backend(self) -> CacheBackend: | ||
from aiohttp_client_cache.backends import RedisBackend | ||
|
||
return RedisBackend(address=self._redis_uri, connection=self._connection) | ||
|
||
|
||
def file_cache(cache_dir: str = _AIOHTTP_CACHE_DIR) -> CacheStrategy: | ||
return _FileCache(cache_dir) | ||
|
||
|
||
def redis_cache(uri: str, connection: Redis | None = None) -> CacheStrategy: | ||
return _RedisCache(uri, connection) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# ******************************************************************************* | ||
# Copyright (c) 2024 Eclipse Foundation and others. | ||
# This program and the accompanying materials are made available | ||
# under the terms of the Eclipse Public License 2.0 | ||
# which is available at http://www.eclipse.org/legal/epl-v20.html | ||
# SPDX-License-Identifier: EPL-2.0 | ||
# ******************************************************************************* | ||
|
||
|
||
def register_filters(app): | ||
@app.template_filter("status") | ||
def status_color(status): | ||
from otterdog.webapp.db.models import InstallationStatus | ||
|
||
match status: | ||
case InstallationStatus.INSTALLED: | ||
return "success" | ||
case InstallationStatus.NOT_INSTALLED: | ||
return "danger" | ||
case InstallationStatus.SUSPENDED: | ||
return "warning" | ||
case _: | ||
return "info" | ||
|
||
@app.template_filter("is_dict") | ||
def is_dict(value): | ||
return isinstance(value, dict) | ||
|
||
@app.template_filter("length_to_color") | ||
def length_to_color(value): | ||
if len(value) == 0: | ||
return "primary" | ||
else: | ||
return "success" | ||
|
||
@app.template_filter("has_dummy_secret") | ||
def has_dummy_secret(value): | ||
return value.has_dummy_secret() | ||
|
||
@app.template_filter("has_dummy_secrets") | ||
def any_has_dummy_secrets(value): | ||
return any(map(lambda x: x.has_dummy_secret(), value)) | ||
|
||
@app.template_filter("pretty_format_model") | ||
def pretty_format_model(value): | ||
from otterdog.models import ModelObject | ||
from otterdog.utils import PrettyFormatter | ||
|
||
assert isinstance(value, ModelObject) | ||
return PrettyFormatter().format(value.to_model_dict(False, False)) |
Oops, something went wrong.