# 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 ...errors.bad_request_error import BadRequestError
from ...errors.not_found_error import NotFoundError
from ...types.order import Order

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 StoreClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def get_inventory(self) -> typing.Dict[str, int]:
        """
        Returns a map of status codes to quantities

        ---
        from Petstore.client import PetstoreApi

        client = PetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.store.get_inventory()
        """
        _response = self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "store/inventory"),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(typing.Dict[str, int], _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 place_order(self, *, request: Order) -> Order:
        """
        Place a new order in the store

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

        client = PetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.store.place_order(
            request=Order(
                id=1,
                pet_id=1,
                quantity=1,
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "store/order"),
            json=jsonable_encoder(request),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(Order, _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 get_order_by_id(self, order_id: int) -> Order:
        """
        For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions.

        Parameters:
            - order_id: int. ID of order that needs to be fetched
        ---
        from Petstore.client import PetstoreApi

        client = PetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.store.get_order_by_id(
            order_id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"store/order/{order_id}"),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(Order, _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 delete_order(self, order_id: int) -> None:
        """
        For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors

        Parameters:
            - order_id: int. ID of the order that needs to be deleted
        ---
        from Petstore.client import PetstoreApi

        client = PetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.store.delete_order(
            order_id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "DELETE",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"store/order/{order_id}"),
            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 AsyncStoreClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def get_inventory(self) -> typing.Dict[str, int]:
        """
        Returns a map of status codes to quantities

        ---
        from Petstore.client import AsyncPetstoreApi

        client = AsyncPetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        await client.store.get_inventory()
        """
        _response = await self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "store/inventory"),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(typing.Dict[str, int], _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 place_order(self, *, request: Order) -> Order:
        """
        Place a new order in the store

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

        client = AsyncPetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        await client.store.place_order(
            request=Order(
                id=1,
                pet_id=1,
                quantity=1,
            ),
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "store/order"),
            json=jsonable_encoder(request),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(Order, _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 get_order_by_id(self, order_id: int) -> Order:
        """
        For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions.

        Parameters:
            - order_id: int. ID of order that needs to be fetched
        ---
        from Petstore.client import AsyncPetstoreApi

        client = AsyncPetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        await client.store.get_order_by_id(
            order_id=1,
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"store/order/{order_id}"),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(Order, _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 delete_order(self, order_id: int) -> None:
        """
        For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors

        Parameters:
            - order_id: int. ID of the order that needs to be deleted
        ---
        from Petstore.client import AsyncPetstoreApi

        client = AsyncPetstoreApi(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        await client.store.delete_order(
            order_id=1,
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "DELETE",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"store/order/{order_id}"),
            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)
