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

import typing
from json.decoder import JSONDecodeError

from ... import core
from ...core.api_error import ApiError
from ...core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ...core.http_response import AsyncHttpResponse, HttpResponse
from ...core.jsonable_encoder import jsonable_encoder
from ...core.parse_error import ParsingError
from ...core.request_options import RequestOptions
from ...core.unchecked_base_model import construct_type
from ...errors.forbidden_error import ForbiddenError
from ...errors.not_found_error import NotFoundError
from ...errors.unprocessable_entity_error import UnprocessableEntityError
from ...types.finetune_created_by import FinetuneCreatedBy
from ...types.finetune_visibility import FinetuneVisibility
from ...types.music_finetune_page_response_model import MusicFinetunePageResponseModel
from ...types.music_finetune_response_model import MusicFinetuneResponseModel
from .types.finetunes_create_request_model_id import FinetunesCreateRequestModelId
from .types.finetunes_create_request_visibility import FinetunesCreateRequestVisibility
from .types.finetunes_list_request_sort import FinetunesListRequestSort
from .types.finetunes_list_request_sort_direction import FinetunesListRequestSortDirection
from .types.update_music_finetune_request_model_visibility import UpdateMusicFinetuneRequestModelVisibility
from pydantic import ValidationError

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


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

    def list(
        self,
        *,
        cursor: typing.Optional[str] = None,
        page_size: typing.Optional[int] = None,
        visibility: typing.Optional[FinetuneVisibility] = None,
        created_by: typing.Optional[FinetuneCreatedBy] = None,
        sort: typing.Optional[FinetunesListRequestSort] = None,
        sort_direction: typing.Optional[FinetunesListRequestSortDirection] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[MusicFinetunePageResponseModel]:
        """
        List music finetunes accessible to you (your own, workspace-shared, and ElevenLabs-curated), with optional filtering, sorting, and cursor pagination.

        Parameters
        ----------
        cursor : typing.Optional[str]
            Used for fetching the next page. Cursor is returned in the response.

        page_size : typing.Optional[int]
            How many finetunes to return. Max 100, default 50.

        visibility : typing.Optional[FinetuneVisibility]
            Filter by visibility. 'private' returns private finetunes; 'workspace' returns workspace-shared finetunes; 'public' returns public finetunes, which are currently ElevenLabs curated finetunes. Omit to return all accessible finetunes.

        created_by : typing.Optional[FinetuneCreatedBy]
            Filter by creator. 'self' returns finetunes you created; 'workspace' returns finetunes created by workspace teammates; 'elevenlabs' returns ElevenLabs curated finetunes. Omit to return finetunes from all creators.

        sort : typing.Optional[FinetunesListRequestSort]
            Sort by field (created_at or name)

        sort_direction : typing.Optional[FinetunesListRequestSortDirection]
            Sort direction (asc or desc)

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

        Returns
        -------
        HttpResponse[MusicFinetunePageResponseModel]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/music/finetunes",
            method="GET",
            params={
                "cursor": cursor,
                "page_size": page_size,
                "visibility": visibility,
                "created_by": created_by,
                "sort": sort,
                "sort_direction": sort_direction,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MusicFinetunePageResponseModel,
                    construct_type(
                        type_=MusicFinetunePageResponseModel,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def create(
        self,
        *,
        name: str,
        primary_genre: str,
        files: typing.Optional[typing.List[core.File]] = OMIT,
        tags: typing.Optional[typing.List[str]] = OMIT,
        visibility: typing.Optional[FinetunesCreateRequestVisibility] = OMIT,
        model_id: typing.Optional[FinetunesCreateRequestModelId] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[MusicFinetuneResponseModel]:
        """
        Create a new music finetune

        Parameters
        ----------
        name : str
            Name for the finetune (5-200 characters).

        primary_genre : str
            Primary musical genre of the finetune.

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

        tags : typing.Optional[typing.List[str]]
            Tags to associate with the finetune.

        visibility : typing.Optional[FinetunesCreateRequestVisibility]
            Finetune visibility. Only 'private' and 'workspace' can be set.

        model_id : typing.Optional[FinetunesCreateRequestModelId]
            The model to create a finetune for.

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

        Returns
        -------
        HttpResponse[MusicFinetuneResponseModel]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/music/finetunes",
            method="POST",
            data={
                "name": name,
                "primary_genre": primary_genre,
                "tags": tags,
                "visibility": visibility,
                "model_id": model_id,
            },
            files={
                **({"files": files} if files is not None else {}),
            },
            request_options=request_options,
            omit=OMIT,
            force_multipart=True,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MusicFinetuneResponseModel,
                    construct_type(
                        type_=MusicFinetuneResponseModel,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def get(
        self, finetune_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[MusicFinetuneResponseModel]:
        """
        Get a music finetune.

        Parameters
        ----------
        finetune_id : str

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

        Returns
        -------
        HttpResponse[MusicFinetuneResponseModel]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/music/finetunes/{jsonable_encoder(finetune_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MusicFinetuneResponseModel,
                    construct_type(
                        type_=MusicFinetuneResponseModel,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def delete(
        self, finetune_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[MusicFinetuneResponseModel]:
        """
        Delete a music finetune

        Parameters
        ----------
        finetune_id : str

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

        Returns
        -------
        HttpResponse[MusicFinetuneResponseModel]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/music/finetunes/{jsonable_encoder(finetune_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MusicFinetuneResponseModel,
                    construct_type(
                        type_=MusicFinetuneResponseModel,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def update(
        self,
        finetune_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        tags: typing.Optional[typing.Sequence[str]] = OMIT,
        primary_genre: typing.Optional[str] = OMIT,
        visibility: typing.Optional[UpdateMusicFinetuneRequestModelVisibility] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[MusicFinetuneResponseModel]:
        """
        Update a music finetune.

        Parameters
        ----------
        finetune_id : str

        name : typing.Optional[str]
            Updated name for the finetune.

        tags : typing.Optional[typing.Sequence[str]]
            Replacement set of tags.

        primary_genre : typing.Optional[str]
            Updated primary musical genre.

        visibility : typing.Optional[UpdateMusicFinetuneRequestModelVisibility]
            Finetune visibility. Only 'private' and 'workspace' can be set.

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

        Returns
        -------
        HttpResponse[MusicFinetuneResponseModel]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/music/finetunes/{jsonable_encoder(finetune_id)}",
            method="PATCH",
            json={
                "name": name,
                "tags": tags,
                "primary_genre": primary_genre,
                "visibility": visibility,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MusicFinetuneResponseModel,
                    construct_type(
                        type_=MusicFinetuneResponseModel,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


class AsyncRawFinetunesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self,
        *,
        cursor: typing.Optional[str] = None,
        page_size: typing.Optional[int] = None,
        visibility: typing.Optional[FinetuneVisibility] = None,
        created_by: typing.Optional[FinetuneCreatedBy] = None,
        sort: typing.Optional[FinetunesListRequestSort] = None,
        sort_direction: typing.Optional[FinetunesListRequestSortDirection] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[MusicFinetunePageResponseModel]:
        """
        List music finetunes accessible to you (your own, workspace-shared, and ElevenLabs-curated), with optional filtering, sorting, and cursor pagination.

        Parameters
        ----------
        cursor : typing.Optional[str]
            Used for fetching the next page. Cursor is returned in the response.

        page_size : typing.Optional[int]
            How many finetunes to return. Max 100, default 50.

        visibility : typing.Optional[FinetuneVisibility]
            Filter by visibility. 'private' returns private finetunes; 'workspace' returns workspace-shared finetunes; 'public' returns public finetunes, which are currently ElevenLabs curated finetunes. Omit to return all accessible finetunes.

        created_by : typing.Optional[FinetuneCreatedBy]
            Filter by creator. 'self' returns finetunes you created; 'workspace' returns finetunes created by workspace teammates; 'elevenlabs' returns ElevenLabs curated finetunes. Omit to return finetunes from all creators.

        sort : typing.Optional[FinetunesListRequestSort]
            Sort by field (created_at or name)

        sort_direction : typing.Optional[FinetunesListRequestSortDirection]
            Sort direction (asc or desc)

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

        Returns
        -------
        AsyncHttpResponse[MusicFinetunePageResponseModel]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/music/finetunes",
            method="GET",
            params={
                "cursor": cursor,
                "page_size": page_size,
                "visibility": visibility,
                "created_by": created_by,
                "sort": sort,
                "sort_direction": sort_direction,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MusicFinetunePageResponseModel,
                    construct_type(
                        type_=MusicFinetunePageResponseModel,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def create(
        self,
        *,
        name: str,
        primary_genre: str,
        files: typing.Optional[typing.List[core.File]] = OMIT,
        tags: typing.Optional[typing.List[str]] = OMIT,
        visibility: typing.Optional[FinetunesCreateRequestVisibility] = OMIT,
        model_id: typing.Optional[FinetunesCreateRequestModelId] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[MusicFinetuneResponseModel]:
        """
        Create a new music finetune

        Parameters
        ----------
        name : str
            Name for the finetune (5-200 characters).

        primary_genre : str
            Primary musical genre of the finetune.

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

        tags : typing.Optional[typing.List[str]]
            Tags to associate with the finetune.

        visibility : typing.Optional[FinetunesCreateRequestVisibility]
            Finetune visibility. Only 'private' and 'workspace' can be set.

        model_id : typing.Optional[FinetunesCreateRequestModelId]
            The model to create a finetune for.

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

        Returns
        -------
        AsyncHttpResponse[MusicFinetuneResponseModel]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/music/finetunes",
            method="POST",
            data={
                "name": name,
                "primary_genre": primary_genre,
                "tags": tags,
                "visibility": visibility,
                "model_id": model_id,
            },
            files={
                **({"files": files} if files is not None else {}),
            },
            request_options=request_options,
            omit=OMIT,
            force_multipart=True,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MusicFinetuneResponseModel,
                    construct_type(
                        type_=MusicFinetuneResponseModel,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def get(
        self, finetune_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[MusicFinetuneResponseModel]:
        """
        Get a music finetune.

        Parameters
        ----------
        finetune_id : str

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

        Returns
        -------
        AsyncHttpResponse[MusicFinetuneResponseModel]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/music/finetunes/{jsonable_encoder(finetune_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MusicFinetuneResponseModel,
                    construct_type(
                        type_=MusicFinetuneResponseModel,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def delete(
        self, finetune_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[MusicFinetuneResponseModel]:
        """
        Delete a music finetune

        Parameters
        ----------
        finetune_id : str

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

        Returns
        -------
        AsyncHttpResponse[MusicFinetuneResponseModel]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/music/finetunes/{jsonable_encoder(finetune_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MusicFinetuneResponseModel,
                    construct_type(
                        type_=MusicFinetuneResponseModel,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def update(
        self,
        finetune_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        tags: typing.Optional[typing.Sequence[str]] = OMIT,
        primary_genre: typing.Optional[str] = OMIT,
        visibility: typing.Optional[UpdateMusicFinetuneRequestModelVisibility] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[MusicFinetuneResponseModel]:
        """
        Update a music finetune.

        Parameters
        ----------
        finetune_id : str

        name : typing.Optional[str]
            Updated name for the finetune.

        tags : typing.Optional[typing.Sequence[str]]
            Replacement set of tags.

        primary_genre : typing.Optional[str]
            Updated primary musical genre.

        visibility : typing.Optional[UpdateMusicFinetuneRequestModelVisibility]
            Finetune visibility. Only 'private' and 'workspace' can be set.

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

        Returns
        -------
        AsyncHttpResponse[MusicFinetuneResponseModel]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/music/finetunes/{jsonable_encoder(finetune_id)}",
            method="PATCH",
            json={
                "name": name,
                "tags": tags,
                "primary_genre": primary_genre,
                "visibility": visibility,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MusicFinetuneResponseModel,
                    construct_type(
                        type_=MusicFinetuneResponseModel,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
