[30388f42] Infrastructure Hardening: Repaired CE/Connector DB schema, fixed frontend styling build, implemented robust echo shield in worker v2.1.1, and integrated Lead Engine into gateway.
This commit is contained in:
@@ -1,129 +1,172 @@
|
||||
import os
|
||||
import requests
|
||||
import json
|
||||
from dotenv import load_dotenv
|
||||
from config import settings
|
||||
import logging
|
||||
|
||||
load_dotenv(override=True)
|
||||
# Configure Logging
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger("superoffice-client")
|
||||
|
||||
class ContactNotFoundException(Exception):
|
||||
"""Custom exception for 404 errors on Contact/Person lookups."""
|
||||
pass
|
||||
|
||||
class SuperOfficeClient:
|
||||
"""A client for interacting with the SuperOffice REST API."""
|
||||
|
||||
def __init__(self):
|
||||
# Helper to strip quotes if Docker passed them literally
|
||||
def get_clean_env(key, default=None):
|
||||
val = os.getenv(key)
|
||||
if val and val.strip(): # Check if not empty string
|
||||
return val.strip('"').strip("'")
|
||||
return default
|
||||
# Configuration
|
||||
self.client_id = settings.SO_CLIENT_ID
|
||||
self.client_secret = settings.SO_CLIENT_SECRET
|
||||
self.refresh_token = settings.SO_REFRESH_TOKEN
|
||||
self.env = settings.SO_ENVIRONMENT
|
||||
self.cust_id = settings.SO_CONTEXT_IDENTIFIER
|
||||
|
||||
self.client_id = get_clean_env("SO_CLIENT_ID") or get_clean_env("SO_SOD")
|
||||
self.client_secret = get_clean_env("SO_CLIENT_SECRET")
|
||||
self.refresh_token = get_clean_env("SO_REFRESH_TOKEN")
|
||||
self.redirect_uri = get_clean_env("SO_REDIRECT_URI", "http://localhost")
|
||||
self.env = get_clean_env("SO_ENVIRONMENT", "sod")
|
||||
self.cust_id = get_clean_env("SO_CONTEXT_IDENTIFIER", "Cust55774") # Fallback for your dev
|
||||
logger.info(f"DEBUG CONFIG: Env='{self.env}', CustID='{self.cust_id}', ClientID='{self.client_id[:4]}...'")
|
||||
|
||||
if not all([self.client_id, self.client_secret, self.refresh_token]):
|
||||
raise ValueError("SuperOffice credentials missing in .env file.")
|
||||
# Graceful failure: Log error but allow init (for help/docs/discovery scripts)
|
||||
logger.error("❌ SuperOffice credentials missing in .env file (or environment variables).")
|
||||
self.base_url = None
|
||||
self.access_token = None
|
||||
return
|
||||
|
||||
self.base_url = f"https://app-{self.env}.superoffice.com/{self.cust_id}/api/v1"
|
||||
self.base_url = f"https://{self.env}.superoffice.com/{self.cust_id}/api/v1"
|
||||
self.access_token = self._refresh_access_token()
|
||||
if not self.access_token:
|
||||
raise Exception("Failed to authenticate with SuperOffice.")
|
||||
|
||||
self.headers = {
|
||||
"Authorization": f"Bearer {self.access_token}",
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
}
|
||||
print("✅ SuperOffice Client initialized and authenticated.")
|
||||
if not self.access_token:
|
||||
logger.error("❌ Failed to authenticate with SuperOffice.")
|
||||
else:
|
||||
self.headers = {
|
||||
"Authorization": f"Bearer {self.access_token}",
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
}
|
||||
logger.info("✅ SuperOffice Client initialized and authenticated.")
|
||||
|
||||
def _refresh_access_token(self):
|
||||
"""Refreshes and returns a new access token."""
|
||||
url = f"https://{self.env}.superoffice.com/login/common/oauth/tokens"
|
||||
print(f"DEBUG: Refresh URL: '{url}' (Env: '{self.env}')") # DEBUG
|
||||
# OAuth token endpoint is ALWAYS online.superoffice.com for production,
|
||||
# or sod.superoffice.com for sandbox.
|
||||
token_domain = "online.superoffice.com" if "online" in self.env.lower() else "sod.superoffice.com"
|
||||
url = f"https://{token_domain}/login/common/oauth/tokens"
|
||||
|
||||
logger.debug(f"DEBUG: Refresh URL: '{url}' (Env: '{self.env}')")
|
||||
|
||||
data = {
|
||||
"grant_type": "refresh_token",
|
||||
"client_id": self.client_id,
|
||||
"client_secret": self.client_secret,
|
||||
"refresh_token": self.refresh_token,
|
||||
"redirect_uri": self.redirect_uri
|
||||
"redirect_uri": settings.SO_REDIRECT_URI
|
||||
}
|
||||
|
||||
try:
|
||||
resp = requests.post(url, data=data)
|
||||
|
||||
# Catch non-JSON responses early
|
||||
if resp.status_code != 200:
|
||||
logger.error(f"❌ Token Refresh Failed (Status {resp.status_code})")
|
||||
logger.error(f"Response Body: {resp.text[:500]}")
|
||||
return None
|
||||
|
||||
resp.raise_for_status()
|
||||
return resp.json().get("access_token")
|
||||
except requests.exceptions.HTTPError as e:
|
||||
print(f"❌ Token Refresh Error: {e.response.text}")
|
||||
except requests.exceptions.JSONDecodeError:
|
||||
logger.error(f"❌ Token Refresh Error: Received non-JSON response from {url}")
|
||||
logger.debug(f"Raw Response: {resp.text[:500]}")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"❌ Connection Error during token refresh: {e}")
|
||||
logger.error(f"❌ Connection Error during token refresh: {e}")
|
||||
return None
|
||||
|
||||
def _request_with_retry(self, method, endpoint, payload=None, retry=True):
|
||||
"""Helper to handle 401 Unauthorized with auto-refresh."""
|
||||
if not self.access_token:
|
||||
if not self._refresh_access_token():
|
||||
return None
|
||||
|
||||
url = f"{self.base_url}/{endpoint}"
|
||||
try:
|
||||
if method == "GET":
|
||||
resp = requests.get(url, headers=self.headers)
|
||||
elif method == "POST":
|
||||
resp = requests.post(url, headers=self.headers, json=payload)
|
||||
elif method == "PUT":
|
||||
resp = requests.put(url, headers=self.headers, json=payload)
|
||||
elif method == "PATCH":
|
||||
resp = requests.patch(url, headers=self.headers, json=payload)
|
||||
elif method == "DELETE":
|
||||
resp = requests.delete(url, headers=self.headers)
|
||||
|
||||
# 401 Handling
|
||||
if resp.status_code == 401 and retry:
|
||||
logger.warning(f"⚠️ 401 Unauthorized for {endpoint}. Attempting Token Refresh...")
|
||||
new_token = self._refresh_access_token()
|
||||
if new_token:
|
||||
logger.info("✅ Token refreshed successfully during retry.")
|
||||
self.access_token = new_token
|
||||
self.headers["Authorization"] = f"Bearer {self.access_token}"
|
||||
# Recursive retry with the new token
|
||||
return self._request_with_retry(method, endpoint, payload, retry=False)
|
||||
else:
|
||||
logger.error("❌ Token Refresh failed during retry.")
|
||||
return None
|
||||
|
||||
if resp.status_code == 204:
|
||||
return True
|
||||
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
except requests.exceptions.HTTPError as e:
|
||||
# Explicitly handle 404 Not Found for GET requests
|
||||
if method == "GET" and e.response.status_code == 404:
|
||||
logger.warning(f"🔍 404 Not Found for GET request to {endpoint}.")
|
||||
raise ContactNotFoundException(f"Entity not found at {endpoint}") from e
|
||||
|
||||
logger.error(f"❌ API {method} Error for {endpoint} (Status: {e.response.status_code}): {e.response.text}")
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"❌ Connection Error during {method} for {endpoint}: {e}")
|
||||
return None
|
||||
|
||||
def _get(self, endpoint):
|
||||
"""Generic GET request."""
|
||||
try:
|
||||
resp = requests.get(f"{self.base_url}/{endpoint}", headers=self.headers)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
except requests.exceptions.HTTPError as e:
|
||||
print(f"❌ API GET Error for {endpoint}: {e.response.text}")
|
||||
return None
|
||||
return self._request_with_retry("GET", endpoint)
|
||||
|
||||
def _put(self, endpoint, payload):
|
||||
"""Generic PUT request."""
|
||||
try:
|
||||
resp = requests.put(f"{self.base_url}/{endpoint}", headers=self.headers, json=payload)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
except requests.exceptions.HTTPError as e:
|
||||
print(f"❌ API PUT Error for {endpoint}: {e.response.text}")
|
||||
return None
|
||||
return self._request_with_retry("PUT", endpoint, payload)
|
||||
|
||||
def get_person(self, person_id):
|
||||
"""Gets a single person by ID."""
|
||||
return self._get(f"Person/{person_id}")
|
||||
def _patch(self, endpoint, payload):
|
||||
return self._request_with_retry("PATCH", endpoint, payload)
|
||||
|
||||
def get_contact(self, contact_id):
|
||||
"""Gets a single contact (company) by ID."""
|
||||
return self._get(f"Contact/{contact_id}")
|
||||
def _post(self, endpoint, payload):
|
||||
return self._request_with_retry("POST", endpoint, payload)
|
||||
|
||||
def update_udfs(self, entity: str, entity_id: int, udf_payload: dict):
|
||||
"""
|
||||
Updates the UserDefinedFields for a given entity (Person or Contact).
|
||||
|
||||
Args:
|
||||
entity (str): "Person" or "Contact".
|
||||
entity_id (int): The ID of the entity.
|
||||
udf_payload (dict): A dictionary of ProgId:Value pairs.
|
||||
"""
|
||||
endpoint = f"{entity}/{entity_id}"
|
||||
|
||||
# 1. GET the full entity object
|
||||
existing_data = self._get(endpoint)
|
||||
if not existing_data:
|
||||
return False # Error is printed in _get
|
||||
def _delete(self, endpoint):
|
||||
return self._request_with_retry("DELETE", endpoint)
|
||||
|
||||
# 2. Merge the UDF payload
|
||||
if "UserDefinedFields" not in existing_data:
|
||||
existing_data["UserDefinedFields"] = {}
|
||||
existing_data["UserDefinedFields"].update(udf_payload)
|
||||
# --- Convenience Wrappers ---
|
||||
|
||||
# 3. PUT the full object back
|
||||
print(f"Updating {entity} {entity_id} with new UDFs...")
|
||||
result = self._put(endpoint, existing_data)
|
||||
|
||||
if result:
|
||||
print(f"✅ Successfully updated {entity} {entity_id}")
|
||||
return True
|
||||
return False
|
||||
def get_person(self, person_id, select: list = None):
|
||||
endpoint = f"Person/{person_id}"
|
||||
if select:
|
||||
endpoint += f"?$select={','.join(select)}"
|
||||
return self._get(endpoint)
|
||||
|
||||
def get_contact(self, contact_id, select: list = None):
|
||||
endpoint = f"Contact/{contact_id}"
|
||||
if select:
|
||||
endpoint += f"?$select={','.join(select)}"
|
||||
return self._get(endpoint)
|
||||
|
||||
def search(self, query_string: str):
|
||||
"""
|
||||
Performs a search using OData syntax and handles pagination.
|
||||
Example: "Person?$select=personId&$filter=lastname eq 'Godelmann'"
|
||||
"""
|
||||
if not self.access_token: return None
|
||||
all_results = []
|
||||
next_page_url = f"{self.base_url}/{query_string}"
|
||||
|
||||
@@ -133,91 +176,16 @@ class SuperOfficeClient:
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
|
||||
# Add the items from the current page
|
||||
all_results.extend(data.get('value', []))
|
||||
|
||||
# Check for the next page link
|
||||
next_page_url = data.get('next_page_url', None)
|
||||
|
||||
# Robust Pagination: Check both OData standard and legacy property
|
||||
next_page_url = data.get('odata.nextLink') or data.get('next_page_url')
|
||||
|
||||
except requests.exceptions.HTTPError as e:
|
||||
print(f"❌ API Search Error for {query_string}: {e.response.text}")
|
||||
logger.error(f"❌ API Search Error for {query_string}: {e.response.text}")
|
||||
return None
|
||||
|
||||
return all_results
|
||||
|
||||
def find_contact_by_criteria(self, name=None, org_nr=None, url=None):
|
||||
"""
|
||||
Finds a contact (company) by name, OrgNr, or URL.
|
||||
Returns the first matching contact or None.
|
||||
"""
|
||||
filter_parts = []
|
||||
if name:
|
||||
filter_parts.append(f"Name eq '{name}'")
|
||||
if org_nr:
|
||||
filter_parts.append(f"OrgNr eq '{org_nr}'")
|
||||
if url:
|
||||
filter_parts.append(f"UrlAddress eq '{url}'")
|
||||
|
||||
if not filter_parts:
|
||||
print("❌ No criteria provided for contact search.")
|
||||
return None
|
||||
|
||||
query_string = "Contact?$filter=" + " or ".join(filter_parts)
|
||||
results = self.search(query_string)
|
||||
if results:
|
||||
return results[0] # Return the first match
|
||||
return None
|
||||
|
||||
def _post(self, endpoint, payload):
|
||||
"""Generic POST request."""
|
||||
try:
|
||||
resp = requests.post(f"{self.base_url}/{endpoint}", headers=self.headers, json=payload)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
except requests.exceptions.HTTPError as e:
|
||||
print(f"❌ API POST Error for {endpoint} (Status: {e.response.status_code}): {e.response.text}")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"❌ Connection Error during POST for {endpoint}: {e}")
|
||||
return None
|
||||
|
||||
def create_contact(self, name: str, url: str = None, org_nr: str = None):
|
||||
"""Creates a new contact (company)."""
|
||||
payload = {"Name": name}
|
||||
if url:
|
||||
payload["UrlAddress"] = url
|
||||
if org_nr:
|
||||
payload["OrgNr"] = org_nr
|
||||
|
||||
print(f"Creating new contact: {name} with payload: {payload}...") # Added payload to log
|
||||
return self._post("Contact", payload)
|
||||
|
||||
def create_person(self, first_name: str, last_name: str, contact_id: int, email: str = None):
|
||||
"""Creates a new person linked to a contact."""
|
||||
payload = {
|
||||
"Firstname": first_name,
|
||||
"Lastname": last_name,
|
||||
"Contact": {"ContactId": contact_id}
|
||||
}
|
||||
if email:
|
||||
payload["EmailAddress"] = email
|
||||
|
||||
print(f"Creating new person: {first_name} {last_name} for Contact ID {contact_id}...")
|
||||
return self._post("Person", payload)
|
||||
|
||||
def create_sale(self, title: str, contact_id: int, person_id: int, amount: float = None):
|
||||
"""Creates a new sale (opportunity) linked to a contact and person."""
|
||||
payload = {
|
||||
"Heading": title,
|
||||
"Contact": {"ContactId": contact_id},
|
||||
"Person": {"PersonId": person_id}
|
||||
}
|
||||
if amount:
|
||||
payload["Amount"] = amount
|
||||
|
||||
print(f"Creating new sale: {title}...")
|
||||
return self._post("Sale", payload)
|
||||
|
||||
def create_project(self, name: str, contact_id: int, person_id: int = None):
|
||||
"""Creates a new project linked to a contact, and optionally adds a person."""
|
||||
payload = {
|
||||
@@ -235,29 +203,75 @@ class SuperOfficeClient:
|
||||
|
||||
print(f"Creating new project: {name}...")
|
||||
return self._post("Project", payload)
|
||||
|
||||
def create_appointment(self, subject: str, description: str, contact_id: int, person_id: int = None):
|
||||
"""Creates a new appointment (to simulate a sent activity)."""
|
||||
import datetime
|
||||
now = datetime.datetime.utcnow().isoformat() + "Z"
|
||||
|
||||
# SuperOffice UI limit: 42 chars.
|
||||
# We put exactly this in the FIRST line of the description.
|
||||
short_title = (subject[:39] + '...') if len(subject) > 42 else subject
|
||||
|
||||
# SuperOffice often 'steals' the first line of the description for the list view header.
|
||||
# So we give it exactly the subject it wants, then two newlines for the real body.
|
||||
formatted_description = f"{short_title}\n\n{description}"
|
||||
|
||||
payload = {
|
||||
"Description": formatted_description,
|
||||
"Contact": {"ContactId": contact_id},
|
||||
"StartDate": now,
|
||||
"EndDate": now,
|
||||
"MainHeader": short_title,
|
||||
"Task": {"Id": 1}
|
||||
}
|
||||
if person_id:
|
||||
payload["Person"] = {"PersonId": person_id}
|
||||
|
||||
logger.info(f"Creating new appointment: {short_title}...")
|
||||
return self._post("Appointment", payload)
|
||||
|
||||
def update_entity_udfs(self, entity_id: int, entity_type: str, udf_data: dict):
|
||||
"""
|
||||
Updates UDFs for a given entity (Contact or Person).
|
||||
Args:
|
||||
entity_id (int): ID of the entity.
|
||||
entity_type (str): 'Contact' or 'Person'.
|
||||
udf_data (dict): Dictionary with ProgId:Value pairs for UDFs.
|
||||
Returns:
|
||||
dict: The updated entity object from the API, or None on failure.
|
||||
Updates UDFs for a given entity (Contact or Person) using PATCH.
|
||||
entity_type: 'Contact' or 'Person'
|
||||
udf_data: {ProgId: Value}
|
||||
"""
|
||||
# We need to GET the existing entity, update its UDFs, then PUT it back.
|
||||
endpoint = f"{entity_type}/{entity_id}"
|
||||
existing_entity = self._get(endpoint)
|
||||
if not existing_entity:
|
||||
print(f"❌ Failed to retrieve existing {entity_type} {entity_id} for UDF update.")
|
||||
return None
|
||||
|
||||
if "UserDefinedFields" not in existing_entity:
|
||||
existing_entity["UserDefinedFields"] = {}
|
||||
|
||||
existing_entity["UserDefinedFields"].update(udf_data)
|
||||
# Construct PATCH payload
|
||||
payload = {
|
||||
"UserDefinedFields": udf_data
|
||||
}
|
||||
|
||||
print(f"Updating {entity_type} {entity_id} UDFs: {udf_data}...")
|
||||
return self._put(endpoint, existing_entity)
|
||||
logger.info(f"Patching {entity_type} {entity_id} UDFs: {udf_data}...")
|
||||
|
||||
# PATCH update
|
||||
result = self._patch(endpoint, payload)
|
||||
return bool(result)
|
||||
|
||||
def update_person_position(self, person_id: int, position_id: int):
|
||||
"""
|
||||
Updates the standard 'Position' field of a Person using PATCH.
|
||||
"""
|
||||
endpoint = f"Person/{person_id}"
|
||||
|
||||
# Construct PATCH payload
|
||||
payload = {
|
||||
"Position": {"Id": int(position_id)}
|
||||
}
|
||||
|
||||
logger.info(f"Patching Person {person_id} Position to ID {position_id}...")
|
||||
|
||||
# PATCH update
|
||||
result = self._patch(endpoint, payload)
|
||||
return bool(result)
|
||||
|
||||
def patch_contact(self, contact_id: int, patch_data: dict):
|
||||
"""
|
||||
Generic PATCH for Contact entity.
|
||||
"""
|
||||
endpoint = f"Contact/{contact_id}"
|
||||
logger.info(f"Patching Contact {contact_id} with data keys: {list(patch_data.keys())}...")
|
||||
result = self._patch(endpoint, patch_data)
|
||||
return bool(result)
|
||||
Reference in New Issue
Block a user