from datetime import datetime
from typing import Any, List, Optional, Union

from pydantic import BaseModel, Field, HttpUrl, validator
from pydantic.dataclasses import dataclass


class Index(BaseModel):
    id: str
    name: str
    time_gate: HttpUrl = Field(alias="timegate")
    cdx_api: HttpUrl = Field(alias="cdx-api")


@dataclass(frozen=True)
class ResultBody:
    mime_detected: Optional[str]
    data: Optional[str]
    text: Optional[List[str]]


@dataclass(frozen=True)
class ResultMeta:
    # todo: these are still raw strings
    warc_request_meta: Optional[str]
    response_header: Optional[str]


class Result(BaseModel):
    url_key: str = Field(alias="urlkey")
    timestamp: datetime
    url: str
    mime: str
    mime_detected: str = Field(alias="mime-detected")
    status: int
    digest: str
    length: int
    offset: int
    filename: str
    languages: Optional[str]
    encoding: Optional[str]
    index_id: Optional[str]
    body: Optional[ResultBody]
    meta: Optional[ResultMeta]

    @validator("timestamp", pre=True)
    def parse_timestamp(cls, value: Any) -> Union[datetime, Any]:
        if isinstance(value, str):
            datetime_value = datetime.strptime(value, "%Y%m%d%H%M%S")
            return datetime_value
        return value


class SearchPagesRequest(BaseModel):
    """Request existing pages on one index for a given url."""

    index: Index
    url: str
    show_num_pages: str = Field(alias="showNumPages", default="true", const=True)
    output: str = "json"


class SearchPagesResponse(BaseModel):
    """Response with the total number of pages in this index for a given url."""

    index: Index
    url: str
    pages: int


class SearchIndexRequest(BaseModel):
    """One page that contains records to be fetched."""

    index: Index
    url: str
    page: int
    output: str = "json"

[((328, 350), 'pydantic.dataclasses.dataclass', 'dataclass', ([], {'frozen': '(True)'}), '(frozen=True)\n', (337, 350), False, 'from pydantic.dataclasses import dataclass\n'), ((459, 481), 'pydantic.dataclasses.dataclass', 'dataclass', ([], {'frozen': '(True)'}), '(frozen=True)\n', (468, 481), False, 'from pydantic.dataclasses import dataclass\n'), ((255, 278), 'pydantic.Field', 'Field', ([], {'alias': '"""timegate"""'}), "(alias='timegate')\n", (260, 278), False, 'from pydantic import BaseModel, Field, HttpUrl, validator\n'), ((302, 324), 'pydantic.Field', 'Field', ([], {'alias': '"""cdx-api"""'}), "(alias='cdx-api')\n", (307, 324), False, 'from pydantic import BaseModel, Field, HttpUrl, validator\n'), ((658, 679), 'pydantic.Field', 'Field', ([], {'alias': '"""urlkey"""'}), "(alias='urlkey')\n", (663, 679), False, 'from pydantic import BaseModel, Field, HttpUrl, validator\n'), ((756, 784), 'pydantic.Field', 'Field', ([], {'alias': '"""mime-detected"""'}), "(alias='mime-detected')\n", (761, 784), False, 'from pydantic import BaseModel, Field, HttpUrl, validator\n'), ((1020, 1052), 'pydantic.validator', 'validator', (['"""timestamp"""'], {'pre': '(True)'}), "('timestamp', pre=True)\n", (1029, 1052), False, 'from pydantic import BaseModel, Field, HttpUrl, validator\n'), ((1438, 1493), 'pydantic.Field', 'Field', ([], {'alias': '"""showNumPages"""', 'default': '"""true"""', 'const': '(True)'}), "(alias='showNumPages', default='true', const=True)\n", (1443, 1493), False, 'from pydantic import BaseModel, Field, HttpUrl, validator\n'), ((1183, 1223), 'datetime.datetime.strptime', 'datetime.strptime', (['value', '"""%Y%m%d%H%M%S"""'], {}), "(value, '%Y%m%d%H%M%S')\n", (1200, 1223), False, 'from datetime import datetime\n')]