Download src/core/tools/integration/models.py from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 7.02 kB
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/core/tools/integration/models.py
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/core/tools/integration/models.py
-
curl -L -o models.py https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/core/tools/integration/models.py
7.02 kB
| """ | |
| Integration Tool - Domain Models | |
| ==================================== | |
| Domain-agnostic external system integration engine. | |
| Manages webhooks, API connectors, HL7 FHIR mapping, | |
| event routing, and data transformation pipelines. | |
| Entities: | |
| IntegrationDefinition - Connector/webhook blueprint | |
| IntegrationInstance - Active connection instance | |
| WebhookDelivery - Outbound webhook attempt | |
| IntegrationEvent - Hash-chained audit trail | |
| Enums: | |
| ConnectorType, AuthMethod, DataFormat, | |
| DeliveryStatus, IntegrationEventType | |
| """ | |
| from __future__ import annotations | |
| from dataclasses import dataclass, field | |
| from datetime import datetime, timezone | |
| from enum import Enum | |
| from typing import Any, Dict, List, Optional | |
| from uuid import UUID, uuid4 | |
| from pydantic import BaseModel, Field | |
| # ============================================================================ | |
| # ENUMS | |
| # ============================================================================ | |
| class ConnectorType(str, Enum): | |
| WEBHOOK_OUTBOUND = "webhook_outbound" | |
| WEBHOOK_INBOUND = "webhook_inbound" | |
| REST_API = "rest_api" | |
| HL7_FHIR = "hl7_fhir" | |
| HL7_V2 = "hl7_v2" | |
| DICOM = "dicom" | |
| SMTP_EMAIL = "smtp_email" | |
| SMS_GATEWAY = "sms_gateway" | |
| FILE_IMPORT = "file_import" | |
| FILE_EXPORT = "file_export" | |
| DATABASE_SYNC = "database_sync" | |
| CUSTOM = "custom" | |
| class AuthMethod(str, Enum): | |
| NONE = "none" | |
| API_KEY = "api_key" | |
| BEARER_TOKEN = "bearer_token" | |
| OAUTH2 = "oauth2" | |
| BASIC_AUTH = "basic_auth" | |
| HMAC_SIGNATURE = "hmac_signature" | |
| MUTUAL_TLS = "mutual_tls" | |
| class DataFormat(str, Enum): | |
| JSON = "json" | |
| XML = "xml" | |
| HL7_V2 = "hl7_v2" | |
| FHIR_JSON = "fhir_json" | |
| FHIR_XML = "fhir_xml" | |
| CSV = "csv" | |
| DICOM = "dicom" | |
| CUSTOM = "custom" | |
| class DeliveryStatus(str, Enum): | |
| PENDING = "pending" | |
| SENDING = "sending" | |
| DELIVERED = "delivered" | |
| FAILED = "failed" | |
| RETRYING = "retrying" | |
| DEAD_LETTER = "dead_letter" | |
| class IntegrationStatus(str, Enum): | |
| ACTIVE = "active" | |
| PAUSED = "paused" | |
| ERROR = "error" | |
| DISABLED = "disabled" | |
| class IntegrationEventType(str, Enum): | |
| DEFINITION_CREATED = "integration.definition_created" | |
| INSTANCE_ACTIVATED = "integration.instance_activated" | |
| INSTANCE_PAUSED = "integration.instance_paused" | |
| WEBHOOK_SENT = "integration.webhook_sent" | |
| WEBHOOK_RECEIVED = "integration.webhook_received" | |
| DELIVERY_FAILED = "integration.delivery_failed" | |
| DATA_TRANSFORMED = "integration.data_transformed" | |
| SYNC_COMPLETED = "integration.sync_completed" | |
| # ============================================================================ | |
| # DOMAIN ENTITIES | |
| # ============================================================================ | |
| class IntegrationDefinition: | |
| """Reusable connector / webhook blueprint.""" | |
| id: UUID = field(default_factory=uuid4) | |
| tenant_id: UUID = field(default_factory=uuid4) | |
| key: str = "" | |
| title: str = "" | |
| description: str = "" | |
| version: str = "1.0.0" | |
| domain: str = "" | |
| connector_type: ConnectorType = ConnectorType.WEBHOOK_OUTBOUND | |
| auth_method: AuthMethod = AuthMethod.NONE | |
| data_format: DataFormat = DataFormat.JSON | |
| endpoint_url: str = "" | |
| headers: Dict[str, str] = field(default_factory=dict) | |
| transform_template: str = "" | |
| retry_policy: Dict[str, Any] = field(default_factory=lambda: { | |
| "max_retries": 3, "backoff_seconds": [5, 30, 300] | |
| }) | |
| events_subscribed: List[str] = field(default_factory=list) | |
| is_active: bool = True | |
| is_system: bool = False | |
| created_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc)) | |
| class IntegrationInstance: | |
| """Active integration connection.""" | |
| id: UUID = field(default_factory=uuid4) | |
| tenant_id: UUID = field(default_factory=uuid4) | |
| definition_id: UUID = field(default_factory=uuid4) | |
| status: IntegrationStatus = IntegrationStatus.ACTIVE | |
| credentials: Dict[str, str] = field(default_factory=dict) | |
| config_overrides: Dict[str, Any] = field(default_factory=dict) | |
| last_sync_at: Optional[datetime] = None | |
| error_message: str = "" | |
| stats: Dict[str, int] = field(default_factory=lambda: { | |
| "total_sent": 0, "total_received": 0, | |
| "total_failed": 0, "total_retried": 0 | |
| }) | |
| created_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc)) | |
| updated_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc)) | |
| class WebhookDelivery: | |
| """Individual webhook delivery attempt.""" | |
| id: UUID = field(default_factory=uuid4) | |
| tenant_id: UUID = field(default_factory=uuid4) | |
| instance_id: UUID = field(default_factory=uuid4) | |
| event_type: str = "" | |
| payload: Dict[str, Any] = field(default_factory=dict) | |
| endpoint_url: str = "" | |
| status: DeliveryStatus = DeliveryStatus.PENDING | |
| response_code: Optional[int] = None | |
| response_body: str = "" | |
| attempt_number: int = 1 | |
| next_retry_at: Optional[datetime] = None | |
| created_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc)) | |
| completed_at: Optional[datetime] = None | |
| class IntegrationEvent: | |
| """Hash-chained integration event.""" | |
| id: UUID = field(default_factory=uuid4) | |
| tenant_id: UUID = field(default_factory=uuid4) | |
| event_type: IntegrationEventType = IntegrationEventType.WEBHOOK_SENT | |
| data: Dict[str, Any] = field(default_factory=dict) | |
| prev_hash: str = "" | |
| hash: str = "" | |
| timestamp: datetime = field(default_factory=lambda: datetime.now(timezone.utc)) | |
| # ============================================================================ | |
| # PYDANTIC SCHEMAS | |
| # ============================================================================ | |
| class IntegrationInstanceCreateRequest(BaseModel): | |
| definition_id: UUID | |
| credentials: Dict[str, str] = Field(default_factory=dict) | |
| config_overrides: Dict[str, Any] = Field(default_factory=dict) | |
| tenant_id: Optional[UUID] = None | |
| user_id: Optional[UUID] = None | |
| class WebhookTestRequest(BaseModel): | |
| instance_id: UUID | |
| event_type: str = "test.ping" | |
| payload: Dict[str, Any] = Field(default_factory=lambda: {"test": True}) | |
| tenant_id: Optional[UUID] = None | |
| class IntegrationDefinitionResponse(BaseModel): | |
| id: UUID | |
| key: str | |
| title: str | |
| description: str | |
| version: str | |
| domain: str | |
| connector_type: str | |
| auth_method: str | |
| data_format: str | |
| endpoint_url: str | |
| events_subscribed: List[str] | |
| retry_policy: Dict[str, Any] | |
| is_active: bool | |
| is_system: bool | |
| class IntegrationInstanceResponse(BaseModel): | |
| id: UUID | |
| definition_id: UUID | |
| status: str | |
| last_sync_at: Optional[datetime] | |
| stats: Dict[str, int] | |
| error_message: str | |
| created_at: datetime | |
| class WebhookDeliveryResponse(BaseModel): | |
| id: UUID | |
| instance_id: UUID | |
| event_type: str | |
| status: str | |
| response_code: Optional[int] | |
| attempt_number: int | |
| created_at: datetime | |
| completed_at: Optional[datetime] | |