|
import os |
|
import requests |
|
from typing import Dict, Any |
|
|
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
|
|
TIMEOUT = int(os.getenv("REMOTE_SERVICE_TIMEOUT", "300")) |
|
|
|
def execute_remote_task(task_name: str, payload: Dict[str, Any]) -> Dict[str, Any]: |
|
""" |
|
Execute a remote task using the configured remote service. |
|
|
|
Args: |
|
task_name: Name of the task to execute (e.g., 'summarization', 'translation') |
|
payload: Dictionary containing task-specific parameters |
|
|
|
Returns: |
|
Dictionary containing the task result |
|
|
|
Raises: |
|
requests.RequestException: If there's an error with the remote request |
|
""" |
|
|
|
endpoint = os.getenv(f"REMOTE_ENDPOINT_{task_name.upper()}") |
|
if not endpoint: |
|
raise ValueError(f"No endpoint configured for task: {task_name}") |
|
|
|
try: |
|
response = requests.post( |
|
url=endpoint, |
|
json={"task": task_name, **payload}, |
|
timeout=TIMEOUT |
|
) |
|
response.raise_for_status() |
|
return response.json() |
|
except requests.RequestException as e: |
|
error_msg = f"Error calling remote service for {task_name}: {str(e)}" |
|
if hasattr(e, 'response') and e.response is not None: |
|
error_msg += f" - {e.response.text}" |
|
return {"error": error_msg} |
|
|