Exa Service updated to use database

This commit is contained in:
ajaysi
2025-10-11 20:22:25 +05:30
parent 734a54acc3
commit bf65065265

View File

@@ -43,17 +43,30 @@ class ExaService:
self.api_key = os.getenv("EXA_API_KEY") self.api_key = os.getenv("EXA_API_KEY")
self.exa = None self.exa = None
self.enabled = False self.enabled = False
if not self.api_key: # Don't assume key is available at import time in production.
logger.warning("EXA_API_KEY not configured; Exa service will be disabled") # Keys may be injected per-request via middleware, so defer init.
else: self._try_initialize()
try:
self.exa = Exa(api_key=self.api_key) def _try_initialize(self) -> None:
self.enabled = True """Attempt to (re)initialize the Exa SDK from current environment."""
logger.info("Exa Service initialized successfully") if self.enabled and self.exa:
except Exception as e: return
logger.error(f"Failed to initialize Exa service: {e}") try:
self.api_key = os.getenv("EXA_API_KEY")
if not self.api_key:
# Leave disabled; caller may try again after middleware injection
logger.warning("EXA_API_KEY not configured; Exa service will be disabled")
self.enabled = False self.enabled = False
self.exa = None
return
self.exa = Exa(api_key=self.api_key)
self.enabled = True
logger.info("Exa Service initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize Exa service: {e}")
self.enabled = False
self.exa = None
async def discover_competitors( async def discover_competitors(
self, self,
@@ -78,6 +91,8 @@ class ExaService:
Dictionary containing competitor analysis results Dictionary containing competitor analysis results
""" """
try: try:
# Ensure we pick up any per-request injected key
self._try_initialize()
if not self.enabled: if not self.enabled:
raise ValueError("Exa Service is not enabled - API key missing") raise ValueError("Exa Service is not enabled - API key missing")
@@ -405,6 +420,8 @@ class ExaService:
Dictionary containing social media discovery results Dictionary containing social media discovery results
""" """
try: try:
# Ensure we pick up any per-request injected key
self._try_initialize()
if not self.enabled: if not self.enabled:
raise ValueError("Exa Service is not enabled - API key missing") raise ValueError("Exa Service is not enabled - API key missing")
@@ -712,6 +729,8 @@ class ExaService:
Dictionary containing service health status Dictionary containing service health status
""" """
try: try:
# Ensure latest env before health check
self._try_initialize()
if not self.enabled: if not self.enabled:
return { return {
"status": "disabled", "status": "disabled",