r"""
    This code was generated by
   ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
    |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
    |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \

    Twilio - Iam
    This is the public Twilio REST API.

    NOTE: This class is auto generated by OpenAPI Generator.
    https://openapi-generator.tech
    Do not edit the class manually.
"""

from datetime import datetime
from typing import Any, Dict, Optional, Union
from twilio.base import deserialize, serialize, values
from twilio.base.api_response import ApiResponse
from twilio.base.instance_context import InstanceContext
from twilio.base.instance_resource import InstanceResource
from twilio.base.list_resource import ListResource
from twilio.base.version import Version


class ApiKeyInstance(InstanceResource):
    """
    :ivar sid: The unique string that we created to identify the Key resource.
    :ivar friendly_name: The string that you assigned to describe the resource.
    :ivar date_created: The date and time in GMT that the resource was created specified in [RFC 2822](https://www.ietf.org/rfc/rfc2822.txt) format.
    :ivar date_updated: The date and time in GMT that the resource was last updated specified in [RFC 2822](https://www.ietf.org/rfc/rfc2822.txt) format.
    :ivar policy: The \\`Policy\\` object is a collection that specifies the allowed Twilio permissions for the restricted key. For more information on the permissions available with restricted API keys, refer to the [Twilio documentation](https://www.twilio.com/docs/iam/api-keys/restricted-api-keys#permissions-available-with-restricted-api-keys).
    """

    def __init__(
        self, version: Version, payload: Dict[str, Any], sid: Optional[str] = None
    ):
        super().__init__(version)

        self.sid: Optional[str] = payload.get("sid")
        self.friendly_name: Optional[str] = payload.get("friendly_name")
        self.date_created: Optional[datetime] = deserialize.rfc2822_datetime(
            payload.get("date_created")
        )
        self.date_updated: Optional[datetime] = deserialize.rfc2822_datetime(
            payload.get("date_updated")
        )
        self.policy: Optional[Dict[str, object]] = payload.get("policy")

        self._solution = {
            "sid": sid or self.sid,
        }
        self._context: Optional[ApiKeyContext] = None

    @property
    def _proxy(self) -> "ApiKeyContext":
        """
        Generate an instance context for the instance, the context is capable of
        performing various actions. All instance actions are proxied to the context

        :returns: ApiKeyContext for this ApiKeyInstance
        """
        if self._context is None:
            self._context = ApiKeyContext(
                self._version,
                sid=self._solution["sid"],
            )
        return self._context

    def delete(self) -> bool:
        """
        Deletes the ApiKeyInstance


        :returns: True if delete succeeds, False otherwise
        """
        return self._proxy.delete()

    async def delete_async(self) -> bool:
        """
        Asynchronous coroutine that deletes the ApiKeyInstance


        :returns: True if delete succeeds, False otherwise
        """
        return await self._proxy.delete_async()

    def delete_with_http_info(self) -> ApiResponse:
        """
        Deletes the ApiKeyInstance with HTTP info


        :returns: ApiResponse with success boolean, status code, and headers
        """
        return self._proxy.delete_with_http_info()

    async def delete_with_http_info_async(self) -> ApiResponse:
        """
        Asynchronous coroutine that deletes the ApiKeyInstance with HTTP info


        :returns: ApiResponse with success boolean, status code, and headers
        """
        return await self._proxy.delete_with_http_info_async()

    def fetch(self) -> "ApiKeyInstance":
        """
        Fetch the ApiKeyInstance


        :returns: The fetched ApiKeyInstance
        """
        return self._proxy.fetch()

    async def fetch_async(self) -> "ApiKeyInstance":
        """
        Asynchronous coroutine to fetch the ApiKeyInstance


        :returns: The fetched ApiKeyInstance
        """
        return await self._proxy.fetch_async()

    def fetch_with_http_info(self) -> ApiResponse:
        """
        Fetch the ApiKeyInstance with HTTP info


        :returns: ApiResponse with instance, status code, and headers
        """
        return self._proxy.fetch_with_http_info()

    async def fetch_with_http_info_async(self) -> ApiResponse:
        """
        Asynchronous coroutine to fetch the ApiKeyInstance with HTTP info


        :returns: ApiResponse with instance, status code, and headers
        """
        return await self._proxy.fetch_with_http_info_async()

    def update(
        self,
        friendly_name: Union[str, object] = values.unset,
        policy: Union[object, object] = values.unset,
    ) -> "ApiKeyInstance":
        """
        Update the ApiKeyInstance

        :param friendly_name: A descriptive string that you create to describe the resource. It can be up to 64 characters long.
        :param policy: The \\\\`Policy\\\\` object is a collection that specifies the allowed Twilio permissions for the restricted key. For more information on the permissions available with restricted API keys, refer to the [Twilio documentation](https://www.twilio.com/docs/iam/api-keys/restricted-api-keys#permissions-available-with-restricted-api-keys).

        :returns: The updated ApiKeyInstance
        """
        return self._proxy.update(
            friendly_name=friendly_name,
            policy=policy,
        )

    async def update_async(
        self,
        friendly_name: Union[str, object] = values.unset,
        policy: Union[object, object] = values.unset,
    ) -> "ApiKeyInstance":
        """
        Asynchronous coroutine to update the ApiKeyInstance

        :param friendly_name: A descriptive string that you create to describe the resource. It can be up to 64 characters long.
        :param policy: The \\\\`Policy\\\\` object is a collection that specifies the allowed Twilio permissions for the restricted key. For more information on the permissions available with restricted API keys, refer to the [Twilio documentation](https://www.twilio.com/docs/iam/api-keys/restricted-api-keys#permissions-available-with-restricted-api-keys).

        :returns: The updated ApiKeyInstance
        """
        return await self._proxy.update_async(
            friendly_name=friendly_name,
            policy=policy,
        )

    def update_with_http_info(
        self,
        friendly_name: Union[str, object] = values.unset,
        policy: Union[object, object] = values.unset,
    ) -> ApiResponse:
        """
        Update the ApiKeyInstance with HTTP info

        :param friendly_name: A descriptive string that you create to describe the resource. It can be up to 64 characters long.
        :param policy: The \\\\`Policy\\\\` object is a collection that specifies the allowed Twilio permissions for the restricted key. For more information on the permissions available with restricted API keys, refer to the [Twilio documentation](https://www.twilio.com/docs/iam/api-keys/restricted-api-keys#permissions-available-with-restricted-api-keys).

        :returns: ApiResponse with instance, status code, and headers
        """
        return self._proxy.update_with_http_info(
            friendly_name=friendly_name,
            policy=policy,
        )

    async def update_with_http_info_async(
        self,
        friendly_name: Union[str, object] = values.unset,
        policy: Union[object, object] = values.unset,
    ) -> ApiResponse:
        """
        Asynchronous coroutine to update the ApiKeyInstance with HTTP info

        :param friendly_name: A descriptive string that you create to describe the resource. It can be up to 64 characters long.
        :param policy: The \\\\`Policy\\\\` object is a collection that specifies the allowed Twilio permissions for the restricted key. For more information on the permissions available with restricted API keys, refer to the [Twilio documentation](https://www.twilio.com/docs/iam/api-keys/restricted-api-keys#permissions-available-with-restricted-api-keys).

        :returns: ApiResponse with instance, status code, and headers
        """
        return await self._proxy.update_with_http_info_async(
            friendly_name=friendly_name,
            policy=policy,
        )

    def __repr__(self) -> str:
        """
        Provide a friendly representation

        :returns: Machine friendly representation
        """
        context = " ".join("{}={}".format(k, v) for k, v in self._solution.items())
        return "<Twilio.Iam.V1.ApiKeyInstance {}>".format(context)


class ApiKeyContext(InstanceContext):

    def __init__(self, version: Version, sid: str):
        """
        Initialize the ApiKeyContext

        :param version: Version that contains the resource
        :param sid: The Twilio-provided string that uniquely identifies the Key resource to update.
        """
        super().__init__(version)

        # Path Solution
        self._solution = {
            "sid": sid,
        }
        self._uri = "/Keys/{sid}".format(**self._solution)

    def _delete(self) -> tuple:
        """
        Internal helper for delete operation

        Returns:
            tuple: (success_boolean, status_code, headers)
        """

        headers = values.of({})

        return self._version.delete_with_response_info(
            method="DELETE", uri=self._uri, headers=headers
        )

    def delete(self) -> bool:
        """
        Deletes the ApiKeyInstance


        :returns: True if delete succeeds, False otherwise
        """
        success, _, _ = self._delete()
        return success

    def delete_with_http_info(self) -> ApiResponse:
        """
        Deletes the ApiKeyInstance and return response metadata


        :returns: ApiResponse with success boolean, status code, and headers
        """
        success, status_code, headers = self._delete()
        return ApiResponse(data=success, status_code=status_code, headers=headers)

    async def _delete_async(self) -> tuple:
        """
        Internal async helper for delete operation

        Returns:
            tuple: (success_boolean, status_code, headers)
        """

        headers = values.of({})

        return await self._version.delete_with_response_info_async(
            method="DELETE", uri=self._uri, headers=headers
        )

    async def delete_async(self) -> bool:
        """
        Asynchronous coroutine that deletes the ApiKeyInstance


        :returns: True if delete succeeds, False otherwise
        """
        success, _, _ = await self._delete_async()
        return success

    async def delete_with_http_info_async(self) -> ApiResponse:
        """
        Asynchronous coroutine that deletes the ApiKeyInstance and return response metadata


        :returns: ApiResponse with success boolean, status code, and headers
        """
        success, status_code, headers = await self._delete_async()
        return ApiResponse(data=success, status_code=status_code, headers=headers)

    def _fetch(self) -> tuple:
        """
        Internal helper for fetch operation

        Returns:
            tuple: (payload, status_code, headers)
        """

        headers = values.of({})

        headers["Accept"] = "application/json"

        return self._version.fetch_with_response_info(
            method="GET", uri=self._uri, headers=headers
        )

    def fetch(self) -> ApiKeyInstance:
        """
        Fetch the ApiKeyInstance


        :returns: The fetched ApiKeyInstance
        """
        payload, _, _ = self._fetch()
        return ApiKeyInstance(
            self._version,
            payload,
            sid=self._solution["sid"],
        )

    def fetch_with_http_info(self) -> ApiResponse:
        """
        Fetch the ApiKeyInstance and return response metadata


        :returns: ApiResponse with instance, status code, and headers
        """
        payload, status_code, headers = self._fetch()
        instance = ApiKeyInstance(
            self._version,
            payload,
            sid=self._solution["sid"],
        )
        return ApiResponse(data=instance, status_code=status_code, headers=headers)

    async def _fetch_async(self) -> tuple:
        """
        Internal async helper for fetch operation

        Returns:
            tuple: (payload, status_code, headers)
        """

        headers = values.of({})

        headers["Accept"] = "application/json"

        return await self._version.fetch_with_response_info_async(
            method="GET", uri=self._uri, headers=headers
        )

    async def fetch_async(self) -> ApiKeyInstance:
        """
        Asynchronous coroutine to fetch the ApiKeyInstance


        :returns: The fetched ApiKeyInstance
        """
        payload, _, _ = await self._fetch_async()
        return ApiKeyInstance(
            self._version,
            payload,
            sid=self._solution["sid"],
        )

    async def fetch_with_http_info_async(self) -> ApiResponse:
        """
        Asynchronous coroutine to fetch the ApiKeyInstance and return response metadata


        :returns: ApiResponse with instance, status code, and headers
        """
        payload, status_code, headers = await self._fetch_async()
        instance = ApiKeyInstance(
            self._version,
            payload,
            sid=self._solution["sid"],
        )
        return ApiResponse(data=instance, status_code=status_code, headers=headers)

    def _update(
        self,
        friendly_name: Union[str, object] = values.unset,
        policy: Union[object, object] = values.unset,
    ) -> tuple:
        """
        Internal helper for update operation

        Returns:
            tuple: (payload, status_code, headers)
        """

        data = values.of(
            {
                "FriendlyName": friendly_name,
                "Policy": serialize.object(policy),
            }
        )
        headers = values.of({})

        headers["Content-Type"] = "application/x-www-form-urlencoded"

        headers["Accept"] = "application/json"

        return self._version.update_with_response_info(
            method="POST", uri=self._uri, data=data, headers=headers
        )

    def update(
        self,
        friendly_name: Union[str, object] = values.unset,
        policy: Union[object, object] = values.unset,
    ) -> ApiKeyInstance:
        """
        Update the ApiKeyInstance

        :param friendly_name: A descriptive string that you create to describe the resource. It can be up to 64 characters long.
        :param policy: The \\\\`Policy\\\\` object is a collection that specifies the allowed Twilio permissions for the restricted key. For more information on the permissions available with restricted API keys, refer to the [Twilio documentation](https://www.twilio.com/docs/iam/api-keys/restricted-api-keys#permissions-available-with-restricted-api-keys).

        :returns: The updated ApiKeyInstance
        """
        payload, _, _ = self._update(friendly_name=friendly_name, policy=policy)
        return ApiKeyInstance(self._version, payload, sid=self._solution["sid"])

    def update_with_http_info(
        self,
        friendly_name: Union[str, object] = values.unset,
        policy: Union[object, object] = values.unset,
    ) -> ApiResponse:
        """
        Update the ApiKeyInstance and return response metadata

        :param friendly_name: A descriptive string that you create to describe the resource. It can be up to 64 characters long.
        :param policy: The \\\\`Policy\\\\` object is a collection that specifies the allowed Twilio permissions for the restricted key. For more information on the permissions available with restricted API keys, refer to the [Twilio documentation](https://www.twilio.com/docs/iam/api-keys/restricted-api-keys#permissions-available-with-restricted-api-keys).

        :returns: ApiResponse with instance, status code, and headers
        """
        payload, status_code, headers = self._update(
            friendly_name=friendly_name, policy=policy
        )
        instance = ApiKeyInstance(self._version, payload, sid=self._solution["sid"])
        return ApiResponse(data=instance, status_code=status_code, headers=headers)

    async def _update_async(
        self,
        friendly_name: Union[str, object] = values.unset,
        policy: Union[object, object] = values.unset,
    ) -> tuple:
        """
        Internal async helper for update operation

        Returns:
            tuple: (payload, status_code, headers)
        """

        data = values.of(
            {
                "FriendlyName": friendly_name,
                "Policy": serialize.object(policy),
            }
        )
        headers = values.of({})

        headers["Content-Type"] = "application/x-www-form-urlencoded"

        headers["Accept"] = "application/json"

        return await self._version.update_with_response_info_async(
            method="POST", uri=self._uri, data=data, headers=headers
        )

    async def update_async(
        self,
        friendly_name: Union[str, object] = values.unset,
        policy: Union[object, object] = values.unset,
    ) -> ApiKeyInstance:
        """
        Asynchronous coroutine to update the ApiKeyInstance

        :param friendly_name: A descriptive string that you create to describe the resource. It can be up to 64 characters long.
        :param policy: The \\\\`Policy\\\\` object is a collection that specifies the allowed Twilio permissions for the restricted key. For more information on the permissions available with restricted API keys, refer to the [Twilio documentation](https://www.twilio.com/docs/iam/api-keys/restricted-api-keys#permissions-available-with-restricted-api-keys).

        :returns: The updated ApiKeyInstance
        """
        payload, _, _ = await self._update_async(
            friendly_name=friendly_name, policy=policy
        )
        return ApiKeyInstance(self._version, payload, sid=self._solution["sid"])

    async def update_with_http_info_async(
        self,
        friendly_name: Union[str, object] = values.unset,
        policy: Union[object, object] = values.unset,
    ) -> ApiResponse:
        """
        Asynchronous coroutine to update the ApiKeyInstance and return response metadata

        :param friendly_name: A descriptive string that you create to describe the resource. It can be up to 64 characters long.
        :param policy: The \\\\`Policy\\\\` object is a collection that specifies the allowed Twilio permissions for the restricted key. For more information on the permissions available with restricted API keys, refer to the [Twilio documentation](https://www.twilio.com/docs/iam/api-keys/restricted-api-keys#permissions-available-with-restricted-api-keys).

        :returns: ApiResponse with instance, status code, and headers
        """
        payload, status_code, headers = await self._update_async(
            friendly_name=friendly_name, policy=policy
        )
        instance = ApiKeyInstance(self._version, payload, sid=self._solution["sid"])
        return ApiResponse(data=instance, status_code=status_code, headers=headers)

    def __repr__(self) -> str:
        """
        Provide a friendly representation

        :returns: Machine friendly representation
        """
        context = " ".join("{}={}".format(k, v) for k, v in self._solution.items())
        return "<Twilio.Iam.V1.ApiKeyContext {}>".format(context)


class ApiKeyList(ListResource):

    def __init__(self, version: Version):
        """
        Initialize the ApiKeyList

        :param version: Version that contains the resource

        """
        super().__init__(version)

    def get(self, sid: str) -> ApiKeyContext:
        """
        Constructs a ApiKeyContext

        :param sid: The Twilio-provided string that uniquely identifies the Key resource to update.
        """
        return ApiKeyContext(self._version, sid=sid)

    def __call__(self, sid: str) -> ApiKeyContext:
        """
        Constructs a ApiKeyContext

        :param sid: The Twilio-provided string that uniquely identifies the Key resource to update.
        """
        return ApiKeyContext(self._version, sid=sid)

    def __repr__(self) -> str:
        """
        Provide a friendly representation

        :returns: Machine friendly representation
        """
        return "<Twilio.Iam.V1.ApiKeyList>"
