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

@@ -26,7 +26,7 @@ class SocialAPIsTool(SocialWrapper, Toolkit):
"""
wrappers = [RedditWrapper]
self.wrapper_handler: WrapperHandler[SocialWrapper] = WrapperHandler(wrappers)
self.wrapper_handler: WrapperHandler[SocialWrapper] = WrapperHandler.build_wrappers(wrappers)
Toolkit.__init__(
self,
@@ -38,7 +38,7 @@ class SocialAPIsTool(SocialWrapper, Toolkit):
# TODO Pensare se ha senso restituire i post da TUTTI i wrapper o solo dal primo che funziona
# la modifica è banale, basta usare try_call_all invece di try_call
def get_top_crypto_posts(self, limit:int = 5) -> list[SocialPost]:
def get_top_crypto_posts(self, limit: int = 5) -> list[SocialPost]:
return self.wrapper_handler.try_call(lambda w: w.get_top_crypto_posts(limit))

View File

@@ -17,6 +17,7 @@ class SocialWrapper:
Base class for social media API wrappers.
All social media API wrappers should inherit from this class and implement the methods.
"""
def get_top_crypto_posts(self, limit: int = 5) -> list[SocialPost]:
"""
Get top cryptocurrency-related posts, optionally limited by total.

View File

@@ -35,19 +35,19 @@ class RedditWrapper(SocialWrapper):
"""
def __init__(self):
self.client_id = os.getenv("REDDIT_API_CLIENT_ID")
assert self.client_id is not None, "REDDIT_API_CLIENT_ID environment variable is not set"
client_id = os.getenv("REDDIT_API_CLIENT_ID")
assert client_id is not None, "REDDIT_API_CLIENT_ID environment variable is not set"
self.client_secret = os.getenv("REDDIT_API_CLIENT_SECRET")
assert self.client_secret is not None, "REDDIT_API_CLIENT_SECRET environment variable is not set"
client_secret = os.getenv("REDDIT_API_CLIENT_SECRET")
assert client_secret is not None, "REDDIT_API_CLIENT_SECRET environment variable is not set"
self.tool = Reddit(
client_id=self.client_id,
client_secret=self.client_secret,
client_id=client_id,
client_secret=client_secret,
user_agent="upo-appAI",
)
def get_top_crypto_posts(self, limit:int = 5) -> list[SocialPost]:
def get_top_crypto_posts(self, limit: int = 5) -> list[SocialPost]:
subreddit = self.tool.subreddit("CryptoCurrency")
top_posts = subreddit.top(limit=limit, time_filter="week")
return [create_social_post(post) for post in top_posts]