mirror of
https://github.com/latinogino/dolibarr-mcp.git
synced 2026-04-20 09:12:41 +02:00
Improve config validation with better error messages and automatic URL formatting
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import BaseModel, Field, validator
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables from .env file
|
||||
@@ -28,20 +28,70 @@ class Config(BaseModel):
|
||||
default_factory=lambda: os.getenv("LOG_LEVEL", "INFO")
|
||||
)
|
||||
|
||||
def validate_config(self) -> None:
|
||||
"""Validate that required configuration is present."""
|
||||
if not self.dolibarr_url:
|
||||
@validator('dolibarr_url')
|
||||
def validate_dolibarr_url(cls, v):
|
||||
"""Validate Dolibarr URL."""
|
||||
if not v:
|
||||
raise ValueError("DOLIBARR_URL environment variable is required")
|
||||
|
||||
if not self.api_key:
|
||||
raise ValueError("DOLIBARR_API_KEY environment variable is required")
|
||||
|
||||
if not self.dolibarr_url.startswith(('http://', 'https://')):
|
||||
if not v.startswith(('http://', 'https://')):
|
||||
raise ValueError("DOLIBARR_URL must start with http:// or https://")
|
||||
|
||||
# Ensure it ends with the proper API path
|
||||
if not v.endswith('/api/index.php'):
|
||||
if v.endswith('/'):
|
||||
v = v + 'api/index.php'
|
||||
else:
|
||||
v = v + '/api/index.php'
|
||||
|
||||
return v
|
||||
|
||||
@validator('api_key')
|
||||
def validate_api_key(cls, v):
|
||||
"""Validate API key."""
|
||||
if not v:
|
||||
raise ValueError(
|
||||
"DOLIBARR_API_KEY environment variable is required. "
|
||||
"Please create an API key in Dolibarr at: Home → Setup → API/Web services"
|
||||
)
|
||||
|
||||
if len(v) < 10:
|
||||
raise ValueError("DOLIBARR_API_KEY appears to be too short. Please check your API key.")
|
||||
|
||||
return v
|
||||
|
||||
@validator('log_level')
|
||||
def validate_log_level(cls, v):
|
||||
"""Validate log level."""
|
||||
valid_levels = {'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'}
|
||||
if v.upper() not in valid_levels:
|
||||
raise ValueError(f"LOG_LEVEL must be one of: {', '.join(valid_levels)}")
|
||||
return v.upper()
|
||||
|
||||
@classmethod
|
||||
def from_env(cls) -> "Config":
|
||||
"""Create configuration from environment variables."""
|
||||
config = cls()
|
||||
config.validate_config()
|
||||
return config
|
||||
"""Create configuration from environment variables with validation."""
|
||||
try:
|
||||
return cls()
|
||||
except Exception as e:
|
||||
print(f"❌ Configuration Error: {e}")
|
||||
print()
|
||||
print("💡 Quick Setup Guide:")
|
||||
print("1. Copy .env.example to .env")
|
||||
print("2. Edit .env with your Dolibarr details:")
|
||||
print(" DOLIBARR_URL=https://your-dolibarr-instance.com")
|
||||
print(" DOLIBARR_API_KEY=your_api_key_here")
|
||||
print()
|
||||
print("🔧 Dolibarr API Key Setup:")
|
||||
print(" 1. Login to Dolibarr as admin")
|
||||
print(" 2. Go to: Home → Setup → Modules")
|
||||
print(" 3. Enable: 'Web Services API REST (developer)'")
|
||||
print(" 4. Go to: Home → Setup → API/Web services")
|
||||
print(" 5. Create a new API key")
|
||||
print()
|
||||
raise
|
||||
|
||||
class Config:
|
||||
"""Pydantic config."""
|
||||
env_file = '.env'
|
||||
env_file_encoding = 'utf-8'
|
||||
|
||||
Reference in New Issue
Block a user