# This file was auto-generated by Fern from our API Definition.

import typing
from json.decoder import JSONDecodeError

from .core.api_error import ApiError
from .core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from .core.http_response import AsyncHttpResponse, HttpResponse
from .core.pydantic_utilities import parse_obj_as
from .core.request_options import RequestOptions
from .errors.bad_request_error import BadRequestError
from .errors.internal_server_error import InternalServerError
from .types.get_v_10_whois_response import GetV10WhoisResponse
from .types.get_whois_historical_response import GetWhoisHistoricalResponse
from .types.get_whois_reverse_response import GetWhoisReverseResponse
from .types.post_bulkwhois_response import PostBulkwhoisResponse

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class RawWhoisfreaksApi:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def get_live_whois_information_for_a_domain(
        self, *, api_key: str, domain_name: str, whois: str, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[GetV10WhoisResponse]:
        """
        Fetch live WHOIS information for a given domain

        Parameters
        ----------
        api_key : str
            Your API key

        domain_name : str
            The domain name for WHOIS lookup

        whois : str
            The type of WHOIS lookup (live)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[GetV10WhoisResponse]
            Successful WHOIS lookup
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1.0/whois",
            method="GET",
            params={
                "apiKey": api_key,
                "domainName": domain_name,
                "whois": whois,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetV10WhoisResponse,
                    parse_obj_as(
                        type_=GetV10WhoisResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response.text)
        raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response_json)

    def get_live_whois_information_for_multiple_domains(
        self,
        *,
        api_key: typing.Optional[str] = OMIT,
        format: typing.Optional[str] = OMIT,
        domain_names: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[PostBulkwhoisResponse]:
        """
        Fetch WHOIS information for a list of domains in bulk

        Parameters
        ----------
        api_key : typing.Optional[str]
            Your API key

        format : typing.Optional[str]
            The response format (optional)

        domain_names : typing.Optional[typing.Sequence[str]]
            List of domain names to lookup

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[PostBulkwhoisResponse]
            Successful bulk WHOIS lookup
        """
        _response = self._client_wrapper.httpx_client.request(
            "bulkwhois",
            method="POST",
            json={
                "apiKey": api_key,
                "format": format,
                "domainNames": domain_names,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PostBulkwhoisResponse,
                    parse_obj_as(
                        type_=PostBulkwhoisResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response.text)
        raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response_json)

    def get_historical_whois_records_for_a_domain(
        self,
        *,
        api_key: str,
        domain_name: str,
        whois: str,
        page: typing.Optional[str] = None,
        format: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[GetWhoisHistoricalResponse]:
        """
        Fetch historical WHOIS records for a given domain

        Parameters
        ----------
        api_key : str
            Your API key

        domain_name : str
            The domain name for historical WHOIS lookup

        whois : str
            The type of WHOIS lookup (historical)

        page : typing.Optional[str]
            The page number of the historical records (optional)

        format : typing.Optional[str]
            The format of the response (optional)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[GetWhoisHistoricalResponse]
            Successful historical WHOIS lookup
        """
        _response = self._client_wrapper.httpx_client.request(
            "whois/historical",
            method="GET",
            params={
                "apiKey": api_key,
                "domainName": domain_name,
                "whois": whois,
                "page": page,
                "format": format,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetWhoisHistoricalResponse,
                    parse_obj_as(
                        type_=GetWhoisHistoricalResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response.text)
        raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response_json)

    def perform_a_reverse_whois_lookup_based_on_registrant_information(
        self,
        *,
        api_key: str,
        whois: str,
        keyword: typing.Optional[str] = None,
        email: typing.Optional[str] = None,
        owner: typing.Optional[str] = None,
        company: typing.Optional[str] = None,
        mode: typing.Optional[str] = None,
        exact: typing.Optional[str] = None,
        format: typing.Optional[str] = None,
        includes: typing.Optional[str] = None,
        page: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[GetWhoisReverseResponse]:
        """
        Fetch domains associated with a given registrant

        Parameters
        ----------
        api_key : str
            Your API key

        whois : str
            The type of WHOIS lookup (reverse)

        keyword : typing.Optional[str]
            Keyword to search for in registrant information (optional)

        email : typing.Optional[str]
            Email to search for (optional)

        owner : typing.Optional[str]
            Owner to search for (optional)

        company : typing.Optional[str]
            Company to search for (optional)

        mode : typing.Optional[str]
            Mode of search (optional)

        exact : typing.Optional[str]
            Exact match flag (optional)

        format : typing.Optional[str]
            Format of response (optional)

        includes : typing.Optional[str]
            Include specific details (optional)

        page : typing.Optional[str]
            The page number of the reverse records (optional)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[GetWhoisReverseResponse]
            Successful reverse WHOIS lookup
        """
        _response = self._client_wrapper.httpx_client.request(
            "whois/reverse",
            method="GET",
            params={
                "apiKey": api_key,
                "whois": whois,
                "keyword": keyword,
                "email": email,
                "owner": owner,
                "company": company,
                "mode": mode,
                "exact": exact,
                "format": format,
                "includes": includes,
                "page": page,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetWhoisReverseResponse,
                    parse_obj_as(
                        type_=GetWhoisReverseResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response.text)
        raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response_json)


class AsyncRawWhoisfreaksApi:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def get_live_whois_information_for_a_domain(
        self, *, api_key: str, domain_name: str, whois: str, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[GetV10WhoisResponse]:
        """
        Fetch live WHOIS information for a given domain

        Parameters
        ----------
        api_key : str
            Your API key

        domain_name : str
            The domain name for WHOIS lookup

        whois : str
            The type of WHOIS lookup (live)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[GetV10WhoisResponse]
            Successful WHOIS lookup
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1.0/whois",
            method="GET",
            params={
                "apiKey": api_key,
                "domainName": domain_name,
                "whois": whois,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetV10WhoisResponse,
                    parse_obj_as(
                        type_=GetV10WhoisResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response.text)
        raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response_json)

    async def get_live_whois_information_for_multiple_domains(
        self,
        *,
        api_key: typing.Optional[str] = OMIT,
        format: typing.Optional[str] = OMIT,
        domain_names: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[PostBulkwhoisResponse]:
        """
        Fetch WHOIS information for a list of domains in bulk

        Parameters
        ----------
        api_key : typing.Optional[str]
            Your API key

        format : typing.Optional[str]
            The response format (optional)

        domain_names : typing.Optional[typing.Sequence[str]]
            List of domain names to lookup

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[PostBulkwhoisResponse]
            Successful bulk WHOIS lookup
        """
        _response = await self._client_wrapper.httpx_client.request(
            "bulkwhois",
            method="POST",
            json={
                "apiKey": api_key,
                "format": format,
                "domainNames": domain_names,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PostBulkwhoisResponse,
                    parse_obj_as(
                        type_=PostBulkwhoisResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response.text)
        raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response_json)

    async def get_historical_whois_records_for_a_domain(
        self,
        *,
        api_key: str,
        domain_name: str,
        whois: str,
        page: typing.Optional[str] = None,
        format: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[GetWhoisHistoricalResponse]:
        """
        Fetch historical WHOIS records for a given domain

        Parameters
        ----------
        api_key : str
            Your API key

        domain_name : str
            The domain name for historical WHOIS lookup

        whois : str
            The type of WHOIS lookup (historical)

        page : typing.Optional[str]
            The page number of the historical records (optional)

        format : typing.Optional[str]
            The format of the response (optional)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[GetWhoisHistoricalResponse]
            Successful historical WHOIS lookup
        """
        _response = await self._client_wrapper.httpx_client.request(
            "whois/historical",
            method="GET",
            params={
                "apiKey": api_key,
                "domainName": domain_name,
                "whois": whois,
                "page": page,
                "format": format,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetWhoisHistoricalResponse,
                    parse_obj_as(
                        type_=GetWhoisHistoricalResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response.text)
        raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response_json)

    async def perform_a_reverse_whois_lookup_based_on_registrant_information(
        self,
        *,
        api_key: str,
        whois: str,
        keyword: typing.Optional[str] = None,
        email: typing.Optional[str] = None,
        owner: typing.Optional[str] = None,
        company: typing.Optional[str] = None,
        mode: typing.Optional[str] = None,
        exact: typing.Optional[str] = None,
        format: typing.Optional[str] = None,
        includes: typing.Optional[str] = None,
        page: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[GetWhoisReverseResponse]:
        """
        Fetch domains associated with a given registrant

        Parameters
        ----------
        api_key : str
            Your API key

        whois : str
            The type of WHOIS lookup (reverse)

        keyword : typing.Optional[str]
            Keyword to search for in registrant information (optional)

        email : typing.Optional[str]
            Email to search for (optional)

        owner : typing.Optional[str]
            Owner to search for (optional)

        company : typing.Optional[str]
            Company to search for (optional)

        mode : typing.Optional[str]
            Mode of search (optional)

        exact : typing.Optional[str]
            Exact match flag (optional)

        format : typing.Optional[str]
            Format of response (optional)

        includes : typing.Optional[str]
            Include specific details (optional)

        page : typing.Optional[str]
            The page number of the reverse records (optional)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[GetWhoisReverseResponse]
            Successful reverse WHOIS lookup
        """
        _response = await self._client_wrapper.httpx_client.request(
            "whois/reverse",
            method="GET",
            params={
                "apiKey": api_key,
                "whois": whois,
                "keyword": keyword,
                "email": email,
                "owner": owner,
                "company": company,
                "mode": mode,
                "exact": exact,
                "format": format,
                "includes": includes,
                "page": page,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetWhoisReverseResponse,
                    parse_obj_as(
                        type_=GetWhoisReverseResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response.text)
        raise ApiError(headers=dict(_response.headers), status_code=_response.status_code, body=_response_json)
