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

import typing
from ...core.client_wrapper import SyncClientWrapper
from ...requests.create_catalog_image_request import CreateCatalogImageRequestParams
from ... import core
from ...core.request_options import RequestOptions
from ...core.http_response import HttpResponse
from ...types.create_catalog_image_response import CreateCatalogImageResponse
import json
from ...core.jsonable_encoder import jsonable_encoder
from ...core.unchecked_base_model import construct_type
from json.decoder import JSONDecodeError
from ...core.api_error import ApiError
from ...requests.update_catalog_image_request import UpdateCatalogImageRequestParams
from ...types.update_catalog_image_response import UpdateCatalogImageResponse
from ...core.client_wrapper import AsyncClientWrapper
from ...core.http_response import AsyncHttpResponse

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


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

    def create(
        self,
        *,
        request: typing.Optional[CreateCatalogImageRequestParams] = OMIT,
        image_file: typing.Optional[core.File] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[CreateCatalogImageResponse]:
        """
        Uploads an image file to be represented by a [CatalogImage](entity:CatalogImage) object that can be linked to an existing
        [CatalogObject](entity:CatalogObject) instance. The resulting `CatalogImage` is unattached to any `CatalogObject` if the `object_id`
        is not specified.

        This `CreateCatalogImage` endpoint accepts HTTP multipart/form-data requests with a JSON part and an image file part in
        JPEG, PJPEG, PNG, or GIF format. The maximum file size is 15MB.

        Parameters
        ----------
        request : typing.Optional[CreateCatalogImageRequestParams]

        image_file : typing.Optional[core.File]
            See core.File for more documentation

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

        Returns
        -------
        HttpResponse[CreateCatalogImageResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/catalog/images",
            method="POST",
            data={},
            files={
                **(
                    {"request": (None, json.dumps(jsonable_encoder(request)), "application/json; charset=utf-8")}
                    if request is not OMIT
                    else {}
                ),
                **(
                    {"image_file": core.with_content_type(file=image_file, default_content_type="image/jpeg")}
                    if image_file is not None
                    else {}
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateCatalogImageResponse,
                    construct_type(
                        type_=CreateCatalogImageResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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(
        self,
        image_id: str,
        *,
        request: typing.Optional[UpdateCatalogImageRequestParams] = OMIT,
        image_file: typing.Optional[core.File] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[UpdateCatalogImageResponse]:
        """
        Uploads a new image file to replace the existing one in the specified [CatalogImage](entity:CatalogImage) object.

        This `UpdateCatalogImage` endpoint accepts HTTP multipart/form-data requests with a JSON part and an image file part in
        JPEG, PJPEG, PNG, or GIF format. The maximum file size is 15MB.

        Parameters
        ----------
        image_id : str
            The ID of the `CatalogImage` object to update the encapsulated image file.

        request : typing.Optional[UpdateCatalogImageRequestParams]

        image_file : typing.Optional[core.File]
            See core.File for more documentation

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

        Returns
        -------
        HttpResponse[UpdateCatalogImageResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/catalog/images/{jsonable_encoder(image_id)}",
            method="PUT",
            data={},
            files={
                **(
                    {"request": (None, json.dumps(jsonable_encoder(request)), "application/json; charset=utf-8")}
                    if request is not OMIT
                    else {}
                ),
                **(
                    {"image_file": core.with_content_type(file=image_file, default_content_type="image/jpeg")}
                    if image_file is not None
                    else {}
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateCatalogImageResponse,
                    construct_type(
                        type_=UpdateCatalogImageResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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 AsyncRawImagesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def create(
        self,
        *,
        request: typing.Optional[CreateCatalogImageRequestParams] = OMIT,
        image_file: typing.Optional[core.File] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[CreateCatalogImageResponse]:
        """
        Uploads an image file to be represented by a [CatalogImage](entity:CatalogImage) object that can be linked to an existing
        [CatalogObject](entity:CatalogObject) instance. The resulting `CatalogImage` is unattached to any `CatalogObject` if the `object_id`
        is not specified.

        This `CreateCatalogImage` endpoint accepts HTTP multipart/form-data requests with a JSON part and an image file part in
        JPEG, PJPEG, PNG, or GIF format. The maximum file size is 15MB.

        Parameters
        ----------
        request : typing.Optional[CreateCatalogImageRequestParams]

        image_file : typing.Optional[core.File]
            See core.File for more documentation

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

        Returns
        -------
        AsyncHttpResponse[CreateCatalogImageResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/catalog/images",
            method="POST",
            data={},
            files={
                **(
                    {"request": (None, json.dumps(jsonable_encoder(request)), "application/json; charset=utf-8")}
                    if request is not OMIT
                    else {}
                ),
                **(
                    {"image_file": core.with_content_type(file=image_file, default_content_type="image/jpeg")}
                    if image_file is not None
                    else {}
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateCatalogImageResponse,
                    construct_type(
                        type_=CreateCatalogImageResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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(
        self,
        image_id: str,
        *,
        request: typing.Optional[UpdateCatalogImageRequestParams] = OMIT,
        image_file: typing.Optional[core.File] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[UpdateCatalogImageResponse]:
        """
        Uploads a new image file to replace the existing one in the specified [CatalogImage](entity:CatalogImage) object.

        This `UpdateCatalogImage` endpoint accepts HTTP multipart/form-data requests with a JSON part and an image file part in
        JPEG, PJPEG, PNG, or GIF format. The maximum file size is 15MB.

        Parameters
        ----------
        image_id : str
            The ID of the `CatalogImage` object to update the encapsulated image file.

        request : typing.Optional[UpdateCatalogImageRequestParams]

        image_file : typing.Optional[core.File]
            See core.File for more documentation

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

        Returns
        -------
        AsyncHttpResponse[UpdateCatalogImageResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/catalog/images/{jsonable_encoder(image_id)}",
            method="PUT",
            data={},
            files={
                **(
                    {"request": (None, json.dumps(jsonable_encoder(request)), "application/json; charset=utf-8")}
                    if request is not OMIT
                    else {}
                ),
                **(
                    {"image_file": core.with_content_type(file=image_file, default_content_type="image/jpeg")}
                    if image_file is not None
                    else {}
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateCatalogImageResponse,
                    construct_type(
                        type_=UpdateCatalogImageResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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)
