Implement configurable API providers from configs.yaml
Co-authored-by: Berack96 <31776951+Berack96@users.noreply.github.com>
This commit is contained in:
@@ -2,29 +2,49 @@ from agno.tools import Toolkit
|
||||
from app.api.wrapper_handler import WrapperHandler
|
||||
from app.api.core.social import SocialPost, SocialWrapper
|
||||
from app.api.social import *
|
||||
from app.configs import AppConfig
|
||||
|
||||
|
||||
class SocialAPIsTool(SocialWrapper, Toolkit):
|
||||
"""
|
||||
Aggregates multiple social media API wrappers and manages them using WrapperHandler.
|
||||
This class supports retrieving top crypto-related posts by querying multiple sources:
|
||||
- RedditWrapper
|
||||
This class supports retrieving top crypto-related posts by querying multiple sources.
|
||||
Providers can be configured in configs.yaml under api.social_providers.
|
||||
|
||||
By default, it returns results from the first successful wrapper.
|
||||
Optionally, it can be configured to collect posts from all wrappers.
|
||||
If no wrapper succeeds, an exception is raised.
|
||||
"""
|
||||
|
||||
# Mapping of wrapper names to wrapper classes
|
||||
_WRAPPER_MAP = {
|
||||
'RedditWrapper': RedditWrapper,
|
||||
'XWrapper': XWrapper,
|
||||
'ChanWrapper': ChanWrapper,
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize the SocialAPIsTool with multiple social media API wrappers.
|
||||
The tool uses WrapperHandler to manage and invoke the different social media API wrappers.
|
||||
The following wrappers are included in this order:
|
||||
- RedditWrapper.
|
||||
Initialize the SocialAPIsTool with social media API wrappers configured in configs.yaml.
|
||||
The order of wrappers is determined by the api.social_providers list in the configuration.
|
||||
"""
|
||||
|
||||
wrappers: list[type[SocialWrapper]] = [RedditWrapper, XWrapper, ChanWrapper]
|
||||
self.handler = WrapperHandler.build_wrappers(wrappers)
|
||||
config = AppConfig()
|
||||
|
||||
# Get wrapper classes based on configuration
|
||||
wrappers: list[type[SocialWrapper]] = []
|
||||
for provider_name in config.api.social_providers:
|
||||
if provider_name in self._WRAPPER_MAP:
|
||||
wrappers.append(self._WRAPPER_MAP[provider_name])
|
||||
|
||||
# Fallback to all wrappers if none configured
|
||||
if not wrappers:
|
||||
wrappers = [RedditWrapper, XWrapper, ChanWrapper]
|
||||
|
||||
self.handler = WrapperHandler.build_wrappers(
|
||||
wrappers,
|
||||
try_per_wrapper=config.api.retry_attempts,
|
||||
retry_delay=config.api.retry_delay_seconds
|
||||
)
|
||||
|
||||
Toolkit.__init__( # type: ignore
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user