- **Company Explorer Sync**: Added `explorer_client.py` and integrated Step 9 in `main.py` for automated data transfer to the intelligence engine. - **Holiday Logic**: Implemented `BusinessCalendar` in `utils.py` using the `holidays` library to automatically detect weekends and Bavarian holidays, ensuring professional timing for automated outreaches. - **API Discovery**: Created `parse_ce_openapi.py` to facilitate technical field mapping through live OpenAPI analysis. - **Project Stability**: Refined error handling and logging for a smooth end-to-end workflow.
75 lines
3.1 KiB
Python
75 lines
3.1 KiB
Python
import requests
|
|
import logging
|
|
import os
|
|
import base64
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class CompanyExplorerClient:
|
|
"""
|
|
Client for the Company Explorer API (Intelligence Engine).
|
|
Handles authentication and data synchronization.
|
|
"""
|
|
def __init__(self, base_url=None, api_user=None, api_password=None):
|
|
# Default to Docker bridge IP for testing from session container
|
|
# In production, this can be overridden via CE_API_URL env var
|
|
self.base_url = base_url or os.getenv("CE_API_URL", "http://172.17.0.1:8000")
|
|
self.api_user = api_user or os.getenv("CE_API_USER", "admin")
|
|
self.api_password = api_password or os.getenv("CE_API_PASSWORD", "gemini")
|
|
self.session = requests.Session()
|
|
|
|
# Setup Basic Auth
|
|
if self.api_user and self.api_password:
|
|
self.session.auth = (self.api_user, self.api_password)
|
|
|
|
def _get_url(self, path):
|
|
"""Constructs the full URL."""
|
|
base = self.base_url.rstrip('/')
|
|
p = path.lstrip('/')
|
|
return f"{base}/{p}"
|
|
|
|
def check_health(self):
|
|
"""Checks if the Company Explorer API is reachable."""
|
|
# Assuming a standard health check endpoint or root endpoint
|
|
# Trying root first as it's often a dashboard or redirect
|
|
url = self._get_url("/")
|
|
try:
|
|
resp = self.session.get(url, timeout=5)
|
|
# We accept 200 (OK) or 401 (Unauthorized - meaning it's there but needs auth)
|
|
# Since we provide auth, 200 is expected.
|
|
if resp.status_code in [200, 401, 403]:
|
|
logger.info(f"Company Explorer is reachable at {self.base_url} (Status: {resp.status_code})")
|
|
return True
|
|
else:
|
|
logger.warning(f"Company Explorer returned unexpected status: {resp.status_code}")
|
|
return False
|
|
except Exception as e:
|
|
logger.error(f"Company Explorer health check failed: {e}")
|
|
return False
|
|
|
|
def import_company(self, company_data):
|
|
"""
|
|
Imports a single company into the Company Explorer.
|
|
|
|
Args:
|
|
company_data (dict): Dictionary containing company details.
|
|
Must include 'name' and 'external_crm_id'.
|
|
"""
|
|
# Endpoint based on plan: POST /api/companies (assuming standard REST)
|
|
# Or bulk endpoint if specified. Let's try single create first.
|
|
url = self._get_url("api/companies")
|
|
|
|
try:
|
|
logger.info(f"Pushing company to Explorer: {company_data.get('name')} (CRM ID: {company_data.get('external_crm_id')})")
|
|
resp = self.session.post(url, json=company_data, timeout=10)
|
|
|
|
if resp.status_code in [200, 201]:
|
|
logger.info("Successfully imported company into Explorer.")
|
|
return resp.json()
|
|
else:
|
|
logger.error(f"Failed to import company. Status: {resp.status_code}, Response: {resp.text}")
|
|
return None
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error importing company: {e}")
|
|
return None |