Refactor: Extract common utilities to shared module
- Created calendar_sync_utils.py with shared functions: - log_operation: Centralized logging with context support - connect_db: Database connection wrapper - get_google_service: Google Calendar API initialization - Updated imports in calendar_sync_event_step.py and audit_calendar_sync.py - Removed duplicated function definitions - Maintained function logic without changes - Improved code maintainability and reduced duplications
This commit is contained in:
@@ -7,6 +7,7 @@ import pytz
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
|
||||
from config import Config
|
||||
from services.advoware import AdvowareAPI
|
||||
from .calendar_sync_utils import connect_db, get_google_service
|
||||
from googleapiclient.discovery import build
|
||||
from googleapiclient.errors import HttpError
|
||||
from google.oauth2 import service_account
|
||||
@@ -23,35 +24,7 @@ BERLIN_TZ = pytz.timezone('Europe/Berlin')
|
||||
now = datetime.now(BERLIN_TZ)
|
||||
current_year = now.year
|
||||
|
||||
async def connect_db():
|
||||
"""Connect to Postgres DB from Config."""
|
||||
try:
|
||||
conn = await asyncpg.connect(
|
||||
host=Config.POSTGRES_HOST or 'localhost',
|
||||
user=Config.POSTGRES_USER,
|
||||
password=Config.POSTGRES_PASSWORD,
|
||||
database=Config.POSTGRES_DB_NAME,
|
||||
timeout=10
|
||||
)
|
||||
return conn
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to connect to DB: {e}")
|
||||
raise
|
||||
|
||||
async def get_google_service():
|
||||
"""Initialize Google Calendar service."""
|
||||
try:
|
||||
service_account_path = Config.GOOGLE_CALENDAR_SERVICE_ACCOUNT_PATH
|
||||
if not os.path.exists(service_account_path):
|
||||
raise FileNotFoundError(f"Service account file not found: {service_account_path}")
|
||||
creds = service_account.Credentials.from_service_account_file(
|
||||
service_account_path, scopes=Config.GOOGLE_CALENDAR_SCOPES
|
||||
)
|
||||
service = build('calendar', 'v3', credentials=creds)
|
||||
return service
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to initialize Google service: {e}")
|
||||
raise
|
||||
|
||||
async def ensure_google_calendar(service, employee_kuerzel):
|
||||
"""Ensure Google Calendar exists for employee."""
|
||||
|
||||
Reference in New Issue
Block a user