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

import typing
import urllib.parse
from json.decoder import JSONDecodeError

from ...core.api_error import ApiError
from ...core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ...core.jsonable_encoder import jsonable_encoder
from ...core.remove_none_from_dict import remove_none_from_dict
from ...errors.bad_request_error import BadRequestError
from ...errors.not_found_error import NotFoundError
from ...types.user import User

try:
    import pydantic.v1 as pydantic  # type: ignore
except ImportError:
    import pydantic  # type: ignore

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


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

    def create_user(self, *, request: User) -> None:
        """
        This can only be done by the logged in user.

        Parameters:
            - request: User.
        ---
        from Petstore import User
        from Petstore.client import PetstoreApi

        client = PetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.user.create_user(
            request=User(
                id=1,
                username="theUser",
                first_name="John",
                last_name="James",
                email="john@email.com",
                user_status=1,
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "user"),
            json=jsonable_encoder(request),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return
        try:
            _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 create_users_with_list_input(self, *, request: typing.List[User]) -> User:
        """
        Creates list of users with given input array

        Parameters:
            - request: typing.List[User].
        ---
        from Petstore import User
        from Petstore.client import PetstoreApi

        client = PetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.user.create_users_with_list_input(
            request=[
                User(
                    id=1,
                    username="theUser",
                    first_name="John",
                    last_name="James",
                    email="john@email.com",
                    user_status=1,
                )
            ],
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "user/createWithList"),
            json=jsonable_encoder(request),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(User, _response.json())  # type: ignore
        try:
            _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 login_user(self, *, username: typing.Optional[str] = None, password: typing.Optional[str] = None) -> str:
        """


        Parameters:
            - username: typing.Optional[str]. The user name for login

            - password: typing.Optional[str]. The password for login in clear text
        ---
        from Petstore.client import PetstoreApi

        client = PetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.user.login_user()
        """
        _response = self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "user/login"),
            params=remove_none_from_dict({"username": username, "password": password}),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(str, _response.json())  # type: ignore
        if _response.status_code == 400:
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
        try:
            _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 logout_user(self) -> None:
        """


        ---
        from Petstore.client import PetstoreApi

        client = PetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.user.logout_user()
        """
        _response = self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "user/logout"),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return
        try:
            _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_user_by_name(self, username: str) -> User:
        """


        Parameters:
            - username: str. The name that needs to be fetched. Use user1 for testing.
        ---
        from Petstore.client import PetstoreApi

        client = PetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.user.get_user_by_name(
            username="username",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"user/{username}"),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(User, _response.json())  # type: ignore
        if _response.status_code == 400:
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
        if _response.status_code == 404:
            raise NotFoundError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
        try:
            _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_user(self, username: str, *, request: User) -> None:
        """
        This can only be done by the logged in user.

        Parameters:
            - username: str. name that needs to be updated

            - request: User.
        ---
        from Petstore import User
        from Petstore.client import PetstoreApi

        client = PetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.user.update_user(
            username="username",
            request=User(
                id=1,
                username="theUser",
                first_name="John",
                last_name="James",
                email="john@email.com",
                user_status=1,
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "PUT",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"user/{username}"),
            json=jsonable_encoder(request),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return
        try:
            _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 delete_user(self, username: str) -> None:
        """
        This can only be done by the logged in user.

        Parameters:
            - username: str. The name that needs to be deleted
        ---
        from Petstore.client import PetstoreApi

        client = PetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.user.delete_user(
            username="username",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "DELETE",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"user/{username}"),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return
        if _response.status_code == 400:
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
        if _response.status_code == 404:
            raise NotFoundError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
        try:
            _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 AsyncUserClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def create_user(self, *, request: User) -> None:
        """
        This can only be done by the logged in user.

        Parameters:
            - request: User.
        ---
        from Petstore import User
        from Petstore.client import AsyncPetstoreApi

        client = AsyncPetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        await client.user.create_user(
            request=User(
                id=1,
                username="theUser",
                first_name="John",
                last_name="James",
                email="john@email.com",
                user_status=1,
            ),
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "user"),
            json=jsonable_encoder(request),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return
        try:
            _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 create_users_with_list_input(self, *, request: typing.List[User]) -> User:
        """
        Creates list of users with given input array

        Parameters:
            - request: typing.List[User].
        ---
        from Petstore import User
        from Petstore.client import AsyncPetstoreApi

        client = AsyncPetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        await client.user.create_users_with_list_input(
            request=[
                User(
                    id=1,
                    username="theUser",
                    first_name="John",
                    last_name="James",
                    email="john@email.com",
                    user_status=1,
                )
            ],
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "user/createWithList"),
            json=jsonable_encoder(request),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(User, _response.json())  # type: ignore
        try:
            _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 login_user(self, *, username: typing.Optional[str] = None, password: typing.Optional[str] = None) -> str:
        """


        Parameters:
            - username: typing.Optional[str]. The user name for login

            - password: typing.Optional[str]. The password for login in clear text
        ---
        from Petstore.client import AsyncPetstoreApi

        client = AsyncPetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        await client.user.login_user()
        """
        _response = await self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "user/login"),
            params=remove_none_from_dict({"username": username, "password": password}),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(str, _response.json())  # type: ignore
        if _response.status_code == 400:
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
        try:
            _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 logout_user(self) -> None:
        """


        ---
        from Petstore.client import AsyncPetstoreApi

        client = AsyncPetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        await client.user.logout_user()
        """
        _response = await self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "user/logout"),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return
        try:
            _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_user_by_name(self, username: str) -> User:
        """


        Parameters:
            - username: str. The name that needs to be fetched. Use user1 for testing.
        ---
        from Petstore.client import AsyncPetstoreApi

        client = AsyncPetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        await client.user.get_user_by_name(
            username="username",
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"user/{username}"),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(User, _response.json())  # type: ignore
        if _response.status_code == 400:
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
        if _response.status_code == 404:
            raise NotFoundError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
        try:
            _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_user(self, username: str, *, request: User) -> None:
        """
        This can only be done by the logged in user.

        Parameters:
            - username: str. name that needs to be updated

            - request: User.
        ---
        from Petstore import User
        from Petstore.client import AsyncPetstoreApi

        client = AsyncPetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        await client.user.update_user(
            username="username",
            request=User(
                id=1,
                username="theUser",
                first_name="John",
                last_name="James",
                email="john@email.com",
                user_status=1,
            ),
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "PUT",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"user/{username}"),
            json=jsonable_encoder(request),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return
        try:
            _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 delete_user(self, username: str) -> None:
        """
        This can only be done by the logged in user.

        Parameters:
            - username: str. The name that needs to be deleted
        ---
        from Petstore.client import AsyncPetstoreApi

        client = AsyncPetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        await client.user.delete_user(
            username="username",
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "DELETE",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"user/{username}"),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return
        if _response.status_code == 400:
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
        if _response.status_code == 404:
            raise NotFoundError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
        try:
            _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)
