Refactor WrapperHandler

- validation checks for initialization logic
- fix SocialAPIsTool
- fix RedditWrapper
This commit is contained in:
2025-10-01 11:05:44 +02:00
parent e4e7023c17
commit 73dcbbe12b
5 changed files with 41 additions and 9 deletions

View File

@@ -24,6 +24,8 @@ class WrapperHandler(Generic[W]):
try_per_wrapper (int): Number of retries per wrapper before switching to the next.
retry_delay (int): Delay in seconds between retries.
"""
assert not WrapperHandler.__check(wrappers), "All wrappers must be instances of their respective classes. Use `build_wrappers` to create the WrapperHandler."
self.wrappers = wrappers
self.retry_per_wrapper = try_per_wrapper
self.retry_delay = retry_delay
@@ -87,6 +89,10 @@ class WrapperHandler(Generic[W]):
raise Exception("All wrappers failed")
return results
@staticmethod
def __check(wrappers: list[W]) -> bool:
return all(w.__class__ is type for w in wrappers)
@staticmethod
def build_wrappers(constructors: Iterable[Type[W]], try_per_wrapper: int = 3, retry_delay: int = 2) -> 'WrapperHandler[W]':
"""
@@ -102,6 +108,8 @@ class WrapperHandler(Generic[W]):
Raises:
Exception: If no wrappers could be initialized.
"""
assert WrapperHandler.__check(constructors), f"All constructors must be classes. Received: {constructors}"
result = []
for wrapper_class in constructors:
try: