| 1 | import datetime |
| 2 | from typing import List |
| 3 | from typing import Optional |
| 4 | |
| 5 | from pydantic import BaseModel |
| 6 | from pydantic import ConfigDict |
| 7 | from pydantic import Field |
| 8 | |
| 9 | |
| 10 | class FlaggedRule(BaseModel): |
| 11 | id: str = Field(..., description="Unique identifier for the flagged rule") |
| 12 | name: str = Field(..., description="Name of the flagged rule") |
| 13 | severity: Optional[str] = Field( |
| 14 | None, |
| 15 | description="Severity level of the flagged rule", |
| 16 | ) |
| 17 | tags: List[str] = Field( |
| 18 | ..., |
| 19 | description="List of tags associated with the flagged rule", |
| 20 | ) |
| 21 | |
| 22 | model_config = ConfigDict(from_attributes=True) |
| 23 | |
| 24 | |
| 25 | class Mailbox(BaseModel): |
| 26 | external_id: Optional[str] = Field( |
| 27 | None, |
| 28 | description="External identifier for the mailbox", |
| 29 | ) |
| 30 | id: str = Field(..., description="Unique identifier for the mailbox") |
| 31 | |
| 32 | model_config = ConfigDict(from_attributes=True) |
| 33 | |
| 34 | |
| 35 | class Message(BaseModel): |
| 36 | canonical_id: str = Field(..., description="Canonical identifier for the message") |
| 37 | external_id: Optional[str] = Field( |
| 38 | None, |
| 39 | description="External identifier for the mailbox", |
| 40 | ) |
| 41 | id: str = Field(..., description="Unique identifier for the message") |
| 42 | mailbox: Mailbox = Field(..., description="Mailbox details") |
| 43 | message_source_id: str = Field(..., description="Source identifier for the message") |
| 44 | |
| 45 | |
| 46 | class TriggeredAction(BaseModel): |
| 47 | id: str = Field(..., description="Unique identifier for the triggered action") |
| 48 | name: str = Field(..., description="Name of the triggered action") |
| 49 | type: str = Field(..., description="Type of the triggered action") |
| 50 | |
| 51 | model_config = ConfigDict(from_attributes=True) |
| 52 | |
| 53 | |
| 54 | class Data(BaseModel): |
| 55 | flagged_rules: List[FlaggedRule] = Field(..., description="List of flagged rules") |
| 56 | message: Message = Field(..., description="Message details") |
| 57 | triggered_actions: List[TriggeredAction] = Field( |
| 58 | ..., |
| 59 | description="List of triggered actions", |
| 60 | ) |
| 61 | |
| 62 | |
| 63 | class AlertRequestBody(BaseModel): |
| 64 | api_version: str = Field(..., description="API version", alias="api_version") |
| 65 | created_at: str = Field( |
| 66 | ..., |
| 67 | description="Creation timestamp in ISO 8601 format", |
| 68 | alias="created_at", |
| 69 | ) |
| 70 | data: Data = Field(..., description="Nested data object") |
| 71 | id: str = Field(..., description="Unique identifier for the request body") |
| 72 | type: str = Field(..., description="Type of event, e.g., message.flagged") |
| 73 | |
| 74 | |
| 75 | class AlertResponseBody(BaseModel): |
| 76 | success: bool = Field(..., description="Success status of the request") |
| 77 | message: str = Field( |
| 78 | ..., |
| 79 | description="Message describing the result of the request", |
| 80 | ) |
| 81 | |
| 82 | |
| 83 | ### SQLModel Schema |
| 84 | class FlaggedRuleSchema(BaseModel): |
| 85 | rule_id: str |
| 86 | name: str |
| 87 | severity: Optional[str] = Field( |
| 88 | None, |
| 89 | description="Severity level of the flagged rule", |
| 90 | ) |
| 91 | tags: str |
| 92 | model_config = ConfigDict(from_attributes=True) |
| 93 | |
| 94 | |
| 95 | class MailboxSchema(BaseModel): |
| 96 | external_id: Optional[str] = Field( |
| 97 | None, |
| 98 | description="External identifier for the mailbox", |
| 99 | ) |
| 100 | mailbox_id: str |
| 101 | model_config = ConfigDict(from_attributes=True) |
| 102 | |
| 103 | |
| 104 | class TriggeredActionSchema(BaseModel): |
| 105 | action_id: str |
| 106 | name: str |
| 107 | type: str |
| 108 | model_config = ConfigDict(from_attributes=True) |
| 109 | |
| 110 | |
| 111 | class SenderSchema(BaseModel): |
| 112 | email: str |
| 113 | name: str |
| 114 | model_config = ConfigDict(from_attributes=True) |
| 115 | |
| 116 | |
| 117 | class RecipientSchema(BaseModel): |
| 118 | email: str |
| 119 | name: str |
| 120 | model_config = ConfigDict(from_attributes=True) |
| 121 | |
| 122 | |
| 123 | class SublimeAlertsSchema(BaseModel): |
| 124 | api_version: str |
| 125 | created_at: str |
| 126 | event_id: str |
| 127 | type: str |
| 128 | message_id: str |
| 129 | canonical_id: str |
| 130 | external_id: str |
| 131 | message_source_id: str |
| 132 | timestamp: datetime.datetime |
| 133 | flagged_rules: List[FlaggedRuleSchema] |
| 134 | mailbox: List[MailboxSchema] |
| 135 | triggered_actions: List[TriggeredActionSchema] |
| 136 | sender: List[SenderSchema] |
| 137 | recipients: List[RecipientSchema] |
| 138 | model_config = ConfigDict(from_attributes=True) |
| 139 | |
| 140 | |
| 141 | class SublimeAlertsResponse(BaseModel): |
| 142 | sublime_alerts: List[SublimeAlertsSchema] |
| 143 | success: bool |
| 144 | message: str |