# This file is auto-generated by oagen. Do not edit.

from __future__ import annotations

from dataclasses import dataclass
from datetime import datetime
from typing import Any, Dict, Literal, Optional
from workos._types import _raise_deserialize_error
from workos._types import _format_datetime, _parse_datetime


@dataclass(slots=True)
class DsyncTokenCreatedData:
    """The event payload."""

    object: Literal["directory_token"]
    """Distinguishes the directory token object."""
    id: str
    """Unique identifier of the directory token."""
    directory_id: str
    """The ID of the directory the token authenticates to."""
    token_suffix: str
    """The trailing characters of the bearer token, for identification only. The full token value is never included in events."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    organization_id: Optional[str] = None
    """The ID of the organization the directory belongs to."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "DsyncTokenCreatedData":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "directory_token"),
                id=data["id"],
                directory_id=data["directory_id"],
                token_suffix=data["token_suffix"],
                created_at=_parse_datetime(data["created_at"]),
                organization_id=data.get("organization_id"),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("DsyncTokenCreatedData", e)

    def to_dict(self) -> Dict[str, Any]:
        """Serialize to a dictionary."""
        result: Dict[str, Any] = {}
        result["object"] = self.object
        result["id"] = self.id
        result["directory_id"] = self.directory_id
        result["token_suffix"] = self.token_suffix
        result["created_at"] = _format_datetime(self.created_at)
        if self.organization_id is not None:
            result["organization_id"] = self.organization_id
        return result
