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

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..core.request_options import RequestOptions
from .types.link_response import LinkResponse
from ..core.pydantic_utilities import parse_obj_as
from .errors.link_not_supported import LinkNotSupported
from .errors.link_not_found import LinkNotFound
from .errors.link_error import LinkError
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from .types.site import Site
from ..core.client_wrapper import AsyncClientWrapper

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


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

    def extract(self, *, url: str, request_options: typing.Optional[RequestOptions] = None) -> LinkResponse:
        """
        Parameters
        ----------
        url : str
            URL to extract a downloadable link from. This link must originate from a supported site. You can use the get supported sites endpoint to get a list of supported sites.

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

        Returns
        -------
        LinkResponse

        Examples
        --------
        from soferai import SoferAI

        client = SoferAI(
            api_key="YOUR_API_KEY",
        )
        client.link.extract(
            url="url",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/link/extract",
            method="POST",
            json={
                "url": url,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    LinkResponse,
                    parse_obj_as(
                        type_=LinkResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 400:
                raise LinkNotSupported()
            if _response.status_code == 404:
                raise LinkNotFound()
            if _response.status_code == 500:
                raise LinkError()
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def get_supported_sites(self, *, request_options: typing.Optional[RequestOptions] = None) -> typing.List[Site]:
        """
        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        typing.List[Site]

        Examples
        --------
        from soferai import SoferAI

        client = SoferAI(
            api_key="YOUR_API_KEY",
        )
        client.link.get_supported_sites()
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/link/sites",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[Site],
                    parse_obj_as(
                        type_=typing.List[Site],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


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

    async def extract(self, *, url: str, request_options: typing.Optional[RequestOptions] = None) -> LinkResponse:
        """
        Parameters
        ----------
        url : str
            URL to extract a downloadable link from. This link must originate from a supported site. You can use the get supported sites endpoint to get a list of supported sites.

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

        Returns
        -------
        LinkResponse

        Examples
        --------
        import asyncio

        from soferai import AsyncSoferAI

        client = AsyncSoferAI(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.link.extract(
                url="url",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/link/extract",
            method="POST",
            json={
                "url": url,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    LinkResponse,
                    parse_obj_as(
                        type_=LinkResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 400:
                raise LinkNotSupported()
            if _response.status_code == 404:
                raise LinkNotFound()
            if _response.status_code == 500:
                raise LinkError()
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def get_supported_sites(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.List[Site]:
        """
        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        typing.List[Site]

        Examples
        --------
        import asyncio

        from soferai import AsyncSoferAI

        client = AsyncSoferAI(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.link.get_supported_sites()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/link/sites",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[Site],
                    parse_obj_as(
                        type_=typing.List[Site],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
