- Integrated centralized logging system in all modules. - Extracted all IDs and ProgIds into a separate . - Refactored and for cleaner dependency management. - Included updated discovery and inspection utilities. - Verified end-to-end workflow stability.
76 lines
3.0 KiB
Python
76 lines
3.0 KiB
Python
import requests
|
|
import logging
|
|
from config import Config
|
|
from logging_config import setup_logging
|
|
|
|
# Use the centralized logging configuration
|
|
logger = setup_logging(__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):
|
|
# Prefer Config values, allow overrides
|
|
self.base_url = base_url or Config.CE_API_URL
|
|
self.api_user = api_user or Config.CE_API_USER
|
|
self.api_password = api_password or Config.CE_API_PASSWORD
|
|
|
|
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 |