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

import typing
from ..core.client_wrapper import SyncClientWrapper
import datetime as dt
from ..core.request_options import RequestOptions
from .types.tools_shorten_url_response import ToolsShortenUrlResponse
from ..core.pydantic_utilities import parse_obj_as
from ..errors.bad_request_error import BadRequestError
from ..errors.unauthorized_error import UnauthorizedError
from ..errors.internal_server_error import InternalServerError
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from .types.tools_upload_url_response import ToolsUploadUrlResponse
from ..core.client_wrapper import AsyncClientWrapper

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


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

    def shorten_url(
        self,
        *,
        url: str,
        expires_at: typing.Optional[dt.datetime] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ToolsShortenUrlResponse:
        """
        Create a shortened URL with an optional expiration date (default and max expiration is 90 days). The shortened URL will redirect to the original URL and will have the following format https://urls.p1n.io/ABCD5678.

        Parameters
        ----------
        url : str
            The URL to be shortened. Must be a valid URL with either http or https protocol.

        expires_at : typing.Optional[dt.datetime]
            Optional expiration date for the shortened URL in ISO 8601 format. Default and max expiration is 90 days.

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

        Returns
        -------
        ToolsShortenUrlResponse
            Successfully created shortened URL

        Examples
        --------
        from rcs import Pinnacle

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.tools.shorten_url(
            url="https://example.com",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "tools/urls/shorten",
            method="POST",
            json={
                "url": url,
                "expiresAt": expires_at,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ToolsShortenUrlResponse,
                    parse_obj_as(
                        type_=ToolsShortenUrlResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            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 == 401:
                raise UnauthorizedError(
                    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(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def upload_url(
        self, *, content_type: str, size: int, name: str, request_options: typing.Optional[RequestOptions] = None
    ) -> ToolsUploadUrlResponse:
        """
        Generate signed upload (expires in 2 hours) and download URLs for a file (expires in 1 hour).

        Parameters
        ----------
        content_type : str
            The MIME type of the file.

            Supported types are audio/basic, audio/L24, audio/mp4, audio/mpeg, audio/mpg, audio/mp3, audio/ogg, audio/aac, audio/vndrn-realaudio, audio/vndwave, audio/3gpp, audio/3gpp2, audio/ac3, audio/webm, audio/amrnb, audio/amr, video/mpeg, video/mp4, video/quicktime, video/webm, video/3gpp, video/3gpp2, video/3gpptt, video/H261, video/H263, video/H2631998, video/H2632000, video/H264, video/m4v, video/mpeg4, video/webm, image/jpeg, image/gif, image/png, image/gif, image/bmp, image/tiff, image/webp, text/vcard, text/xvcard, text/csv, text/rtf, text/richtext, text/calendar, text/directory, application/ogg, application/pdf, application/vcard, application/vndapple.pkpass.


        size : int
            The size of the file in bytes. Should be less than 100 MB.

        name : str
            The name of the file.

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

        Returns
        -------
        ToolsUploadUrlResponse
            Successfully generated upload and download URLs

        Examples
        --------
        from rcs import Pinnacle

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.tools.upload_url(
            content_type="image/png",
            size=1024,
            name="example.png",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "tools/uploadUrl",
            method="POST",
            json={
                "contentType": content_type,
                "size": size,
                "name": name,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ToolsUploadUrlResponse,
                    parse_obj_as(
                        type_=ToolsUploadUrlResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            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 == 401:
                raise UnauthorizedError(
                    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(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


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

    async def shorten_url(
        self,
        *,
        url: str,
        expires_at: typing.Optional[dt.datetime] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ToolsShortenUrlResponse:
        """
        Create a shortened URL with an optional expiration date (default and max expiration is 90 days). The shortened URL will redirect to the original URL and will have the following format https://urls.p1n.io/ABCD5678.

        Parameters
        ----------
        url : str
            The URL to be shortened. Must be a valid URL with either http or https protocol.

        expires_at : typing.Optional[dt.datetime]
            Optional expiration date for the shortened URL in ISO 8601 format. Default and max expiration is 90 days.

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

        Returns
        -------
        ToolsShortenUrlResponse
            Successfully created shortened URL

        Examples
        --------
        import asyncio

        from rcs import AsyncPinnacle

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.tools.shorten_url(
                url="https://example.com",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "tools/urls/shorten",
            method="POST",
            json={
                "url": url,
                "expiresAt": expires_at,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ToolsShortenUrlResponse,
                    parse_obj_as(
                        type_=ToolsShortenUrlResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            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 == 401:
                raise UnauthorizedError(
                    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(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def upload_url(
        self, *, content_type: str, size: int, name: str, request_options: typing.Optional[RequestOptions] = None
    ) -> ToolsUploadUrlResponse:
        """
        Generate signed upload (expires in 2 hours) and download URLs for a file (expires in 1 hour).

        Parameters
        ----------
        content_type : str
            The MIME type of the file.

            Supported types are audio/basic, audio/L24, audio/mp4, audio/mpeg, audio/mpg, audio/mp3, audio/ogg, audio/aac, audio/vndrn-realaudio, audio/vndwave, audio/3gpp, audio/3gpp2, audio/ac3, audio/webm, audio/amrnb, audio/amr, video/mpeg, video/mp4, video/quicktime, video/webm, video/3gpp, video/3gpp2, video/3gpptt, video/H261, video/H263, video/H2631998, video/H2632000, video/H264, video/m4v, video/mpeg4, video/webm, image/jpeg, image/gif, image/png, image/gif, image/bmp, image/tiff, image/webp, text/vcard, text/xvcard, text/csv, text/rtf, text/richtext, text/calendar, text/directory, application/ogg, application/pdf, application/vcard, application/vndapple.pkpass.


        size : int
            The size of the file in bytes. Should be less than 100 MB.

        name : str
            The name of the file.

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

        Returns
        -------
        ToolsUploadUrlResponse
            Successfully generated upload and download URLs

        Examples
        --------
        import asyncio

        from rcs import AsyncPinnacle

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.tools.upload_url(
                content_type="image/png",
                size=1024,
                name="example.png",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "tools/uploadUrl",
            method="POST",
            json={
                "contentType": content_type,
                "size": size,
                "name": name,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ToolsUploadUrlResponse,
                    parse_obj_as(
                        type_=ToolsUploadUrlResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            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 == 401:
                raise UnauthorizedError(
                    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(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
