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

import typing
from .environment import PinnacleEnvironment
import httpx
from .core.client_wrapper import SyncClientWrapper
from .types.phone_number import PhoneNumber
from .core.request_options import RequestOptions
from .types.check_rcs_capability_response import CheckRcsCapabilityResponse
from .core.pydantic_utilities import parse_obj_as
from .errors.bad_request_error import BadRequestError
from .types.bad_request_error_body import BadRequestErrorBody
from .errors.unauthorized_error import UnauthorizedError
from .types.unauthorized_error_body import UnauthorizedErrorBody
from .errors.internal_server_error import InternalServerError
from .types.internal_server_error_body import InternalServerErrorBody
from json.decoder import JSONDecodeError
from .core.api_error import ApiError
from .types.update_settings_response import UpdateSettingsResponse
from .types.get_account_number_response import GetAccountNumberResponse
from .types.send_request import SendRequest
from .types.send_response import SendResponse
from .core.serialization import convert_and_respect_annotation_metadata
from .core.client_wrapper import AsyncClientWrapper

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


class Pinnacle:
    """
    Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propagate to these functions.

    Parameters
    ----------
    base_url : typing.Optional[str]
        The base url to use for requests from the client.

    environment : PinnacleEnvironment
        The environment to use for requests from the client. from .environment import PinnacleEnvironment



        Defaults to PinnacleEnvironment.DEFAULT



    api_key : str
    timeout : typing.Optional[float]
        The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.

    follow_redirects : typing.Optional[bool]
        Whether the default httpx client follows redirects or not, this is irrelevant if a custom httpx client is passed in.

    httpx_client : typing.Optional[httpx.Client]
        The httpx client to use for making requests, a preconfigured client is used by default, however this is useful should you want to pass in any custom httpx configuration.

    Examples
    --------
    from pinnacle import Pinnacle

    client = Pinnacle(
        api_key="YOUR_API_KEY",
    )
    """

    def __init__(
        self,
        *,
        base_url: typing.Optional[str] = None,
        environment: PinnacleEnvironment = PinnacleEnvironment.DEFAULT,
        api_key: str,
        timeout: typing.Optional[float] = None,
        follow_redirects: typing.Optional[bool] = True,
        httpx_client: typing.Optional[httpx.Client] = None,
    ):
        _defaulted_timeout = timeout if timeout is not None else 60 if httpx_client is None else None
        self._client_wrapper = SyncClientWrapper(
            base_url=_get_base_url(base_url=base_url, environment=environment),
            api_key=api_key,
            httpx_client=httpx_client
            if httpx_client is not None
            else httpx.Client(timeout=_defaulted_timeout, follow_redirects=follow_redirects)
            if follow_redirects is not None
            else httpx.Client(timeout=_defaulted_timeout),
            timeout=_defaulted_timeout,
        )

    def check_rcs_capability(
        self, *, phone_number: PhoneNumber, request_options: typing.Optional[RequestOptions] = None
    ) -> CheckRcsCapabilityResponse:
        """
        Checks if a phone number is able to receive RCS

        Parameters
        ----------
        phone_number : PhoneNumber
            Phone number (E.164 format: [+][country code][subscriber number including area code]) to check for RCS capability. Example: +1234567890

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

        Returns
        -------
        CheckRcsCapabilityResponse
            RCS capability check successful

        Examples
        --------
        from pinnacle import Pinnacle

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.check_rcs_capability(
            phone_number="phone_number",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "check_rcs",
            method="GET",
            params={
                "phone_number": phone_number,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    CheckRcsCapabilityResponse,
                    parse_obj_as(
                        type_=CheckRcsCapabilityResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        BadRequestErrorBody,
                        parse_obj_as(
                            type_=BadRequestErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    typing.cast(
                        UnauthorizedErrorBody,
                        parse_obj_as(
                            type_=UnauthorizedErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        InternalServerErrorBody,
                        parse_obj_as(
                            type_=InternalServerErrorBody,  # 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 update_settings(
        self, *, webhook_url: str, request_options: typing.Optional[RequestOptions] = None
    ) -> UpdateSettingsResponse:
        """
        Initializes settings related to RCS messaging, including webhook registration.

        Parameters
        ----------
        webhook_url : str
            Webhook URL to receive inbound messages

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

        Returns
        -------
        UpdateSettingsResponse
            Settings updated successfully

        Examples
        --------
        from pinnacle import Pinnacle

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.update_settings(
            webhook_url="webhook_url",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "update_settings",
            method="POST",
            json={
                "webhook_url": webhook_url,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    UpdateSettingsResponse,
                    parse_obj_as(
                        type_=UpdateSettingsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        BadRequestErrorBody,
                        parse_obj_as(
                            type_=BadRequestErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    typing.cast(
                        UnauthorizedErrorBody,
                        parse_obj_as(
                            type_=UnauthorizedErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        InternalServerErrorBody,
                        parse_obj_as(
                            type_=InternalServerErrorBody,  # 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 get_account_number(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> GetAccountNumberResponse:
        """
        Retrieve the phone number associated with the account.

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

        Returns
        -------
        GetAccountNumberResponse
            Successfully retrieved supported numbers

        Examples
        --------
        from pinnacle import Pinnacle

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.get_account_number()
        """
        _response = self._client_wrapper.httpx_client.request(
            "get_account_number",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    GetAccountNumberResponse,
                    parse_obj_as(
                        type_=GetAccountNumberResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    typing.cast(
                        UnauthorizedErrorBody,
                        parse_obj_as(
                            type_=UnauthorizedErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        InternalServerErrorBody,
                        parse_obj_as(
                            type_=InternalServerErrorBody,  # 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 send(self, *, request: SendRequest, request_options: typing.Optional[RequestOptions] = None) -> SendResponse:
        """
        Send a SMS or RCS message to a phone number

        Parameters
        ----------
        request : SendRequest

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

        Returns
        -------
        SendResponse
            Message sent successfully

        Examples
        --------
        from pinnacle import Pinnacle, Sms, SmsMessage

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.send(
            request=Sms(
                message=SmsMessage(
                    body="body",
                ),
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "send",
            method="POST",
            json=convert_and_respect_annotation_metadata(object_=request, annotation=SendRequest, direction="write"),
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    SendResponse,
                    parse_obj_as(
                        type_=SendResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        BadRequestErrorBody,
                        parse_obj_as(
                            type_=BadRequestErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    typing.cast(
                        UnauthorizedErrorBody,
                        parse_obj_as(
                            type_=UnauthorizedErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        InternalServerErrorBody,
                        parse_obj_as(
                            type_=InternalServerErrorBody,  # 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 AsyncPinnacle:
    """
    Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propagate to these functions.

    Parameters
    ----------
    base_url : typing.Optional[str]
        The base url to use for requests from the client.

    environment : PinnacleEnvironment
        The environment to use for requests from the client. from .environment import PinnacleEnvironment



        Defaults to PinnacleEnvironment.DEFAULT



    api_key : str
    timeout : typing.Optional[float]
        The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.

    follow_redirects : typing.Optional[bool]
        Whether the default httpx client follows redirects or not, this is irrelevant if a custom httpx client is passed in.

    httpx_client : typing.Optional[httpx.AsyncClient]
        The httpx client to use for making requests, a preconfigured client is used by default, however this is useful should you want to pass in any custom httpx configuration.

    Examples
    --------
    from pinnacle import AsyncPinnacle

    client = AsyncPinnacle(
        api_key="YOUR_API_KEY",
    )
    """

    def __init__(
        self,
        *,
        base_url: typing.Optional[str] = None,
        environment: PinnacleEnvironment = PinnacleEnvironment.DEFAULT,
        api_key: str,
        timeout: typing.Optional[float] = None,
        follow_redirects: typing.Optional[bool] = True,
        httpx_client: typing.Optional[httpx.AsyncClient] = None,
    ):
        _defaulted_timeout = timeout if timeout is not None else 60 if httpx_client is None else None
        self._client_wrapper = AsyncClientWrapper(
            base_url=_get_base_url(base_url=base_url, environment=environment),
            api_key=api_key,
            httpx_client=httpx_client
            if httpx_client is not None
            else httpx.AsyncClient(timeout=_defaulted_timeout, follow_redirects=follow_redirects)
            if follow_redirects is not None
            else httpx.AsyncClient(timeout=_defaulted_timeout),
            timeout=_defaulted_timeout,
        )

    async def check_rcs_capability(
        self, *, phone_number: PhoneNumber, request_options: typing.Optional[RequestOptions] = None
    ) -> CheckRcsCapabilityResponse:
        """
        Checks if a phone number is able to receive RCS

        Parameters
        ----------
        phone_number : PhoneNumber
            Phone number (E.164 format: [+][country code][subscriber number including area code]) to check for RCS capability. Example: +1234567890

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

        Returns
        -------
        CheckRcsCapabilityResponse
            RCS capability check successful

        Examples
        --------
        import asyncio

        from pinnacle import AsyncPinnacle

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.check_rcs_capability(
                phone_number="phone_number",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "check_rcs",
            method="GET",
            params={
                "phone_number": phone_number,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    CheckRcsCapabilityResponse,
                    parse_obj_as(
                        type_=CheckRcsCapabilityResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        BadRequestErrorBody,
                        parse_obj_as(
                            type_=BadRequestErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    typing.cast(
                        UnauthorizedErrorBody,
                        parse_obj_as(
                            type_=UnauthorizedErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        InternalServerErrorBody,
                        parse_obj_as(
                            type_=InternalServerErrorBody,  # 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 update_settings(
        self, *, webhook_url: str, request_options: typing.Optional[RequestOptions] = None
    ) -> UpdateSettingsResponse:
        """
        Initializes settings related to RCS messaging, including webhook registration.

        Parameters
        ----------
        webhook_url : str
            Webhook URL to receive inbound messages

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

        Returns
        -------
        UpdateSettingsResponse
            Settings updated successfully

        Examples
        --------
        import asyncio

        from pinnacle import AsyncPinnacle

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.update_settings(
                webhook_url="webhook_url",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "update_settings",
            method="POST",
            json={
                "webhook_url": webhook_url,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    UpdateSettingsResponse,
                    parse_obj_as(
                        type_=UpdateSettingsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        BadRequestErrorBody,
                        parse_obj_as(
                            type_=BadRequestErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    typing.cast(
                        UnauthorizedErrorBody,
                        parse_obj_as(
                            type_=UnauthorizedErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        InternalServerErrorBody,
                        parse_obj_as(
                            type_=InternalServerErrorBody,  # 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 get_account_number(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> GetAccountNumberResponse:
        """
        Retrieve the phone number associated with the account.

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

        Returns
        -------
        GetAccountNumberResponse
            Successfully retrieved supported numbers

        Examples
        --------
        import asyncio

        from pinnacle import AsyncPinnacle

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


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


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "get_account_number",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    GetAccountNumberResponse,
                    parse_obj_as(
                        type_=GetAccountNumberResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    typing.cast(
                        UnauthorizedErrorBody,
                        parse_obj_as(
                            type_=UnauthorizedErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        InternalServerErrorBody,
                        parse_obj_as(
                            type_=InternalServerErrorBody,  # 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 send(
        self, *, request: SendRequest, request_options: typing.Optional[RequestOptions] = None
    ) -> SendResponse:
        """
        Send a SMS or RCS message to a phone number

        Parameters
        ----------
        request : SendRequest

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

        Returns
        -------
        SendResponse
            Message sent successfully

        Examples
        --------
        import asyncio

        from pinnacle import AsyncPinnacle, Sms, SmsMessage

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.send(
                request=Sms(
                    message=SmsMessage(
                        body="body",
                    ),
                ),
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "send",
            method="POST",
            json=convert_and_respect_annotation_metadata(object_=request, annotation=SendRequest, direction="write"),
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    SendResponse,
                    parse_obj_as(
                        type_=SendResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        BadRequestErrorBody,
                        parse_obj_as(
                            type_=BadRequestErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    typing.cast(
                        UnauthorizedErrorBody,
                        parse_obj_as(
                            type_=UnauthorizedErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    typing.cast(
                        InternalServerErrorBody,
                        parse_obj_as(
                            type_=InternalServerErrorBody,  # 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 _get_base_url(*, base_url: typing.Optional[str] = None, environment: PinnacleEnvironment) -> str:
    if base_url is not None:
        return base_url
    elif environment is not None:
        return environment.value
    else:
        raise Exception("Please pass in either base_url or environment to construct the client")
