From a3042be0319b6a6d98239ed963e0f7cb11a869fc Mon Sep 17 00:00:00 2001 From: chyroc Date: Wed, 8 Jan 2025 22:46:34 +0800 Subject: [PATCH] refactor: Rename 'translations' to 'transcriptions' (#158) --- cozepy/__init__.py | 6 ++-- cozepy/audio/__init__.py | 28 ++++++++++--------- .../__init__.py | 26 ++++++++--------- tests/test_audio_translations.py | 20 ++++++------- 4 files changed, 41 insertions(+), 39 deletions(-) rename cozepy/audio/{translations => transcriptions}/__init__.py (67%) diff --git a/cozepy/__init__.py b/cozepy/__init__.py index 70a0ab8..d19d72b 100644 --- a/cozepy/__init__.py +++ b/cozepy/__init__.py @@ -1,6 +1,6 @@ from .audio.rooms import CreateRoomResp from .audio.speech import AudioFormat -from .audio.translations import CreateTranslationResp +from .audio.transcriptions import CreateTranscriptionsResp from .audio.voices import Voice from .auth import ( AsyncDeviceOAuthApp, @@ -110,8 +110,8 @@ # audio.voices "Voice", "AudioFormat", - # audio.translations - "CreateTranslationResp", + # audio.transcriptions + "CreateTranscriptionsResp", # auth "AsyncDeviceOAuthApp", "AsyncJWTOAuthApp", diff --git a/cozepy/audio/__init__.py b/cozepy/audio/__init__.py index d919615..58cab7c 100644 --- a/cozepy/audio/__init__.py +++ b/cozepy/audio/__init__.py @@ -7,7 +7,7 @@ if TYPE_CHECKING: from .rooms import AsyncRoomsClient, RoomsClient from .speech import AsyncSpeechClient, SpeechClient - from .translations import AsyncTranslationsClient, TranslationsClient + from .transcriptions import AsyncTranscriptionsClient, TranscriptionsClient from .voices import AsyncVoicesClient, VoicesClient @@ -20,7 +20,7 @@ def __init__(self, base_url: str, auth: Auth, requester: Requester): self._rooms: Optional[RoomsClient] = None self._voices: Optional[VoicesClient] = None self._speech: Optional[SpeechClient] = None - self._translations: Optional[TranslationsClient] = None + self._transcriptions: Optional[TranscriptionsClient] = None @property def rooms(self) -> "RoomsClient": @@ -39,12 +39,14 @@ def speech(self) -> "SpeechClient": return self._speech @property - def translations(self) -> "TranslationsClient": - if self._translations is None: - from .translations import TranslationsClient + def transcriptions(self) -> "TranscriptionsClient": + if self._transcriptions is None: + from .transcriptions import TranscriptionsClient - self._translations = TranslationsClient(base_url=self._base_url, auth=self._auth, requester=self._requester) - return self._translations + self._transcriptions = TranscriptionsClient( + base_url=self._base_url, auth=self._auth, requester=self._requester + ) + return self._transcriptions @property def voices(self) -> "VoicesClient": @@ -64,7 +66,7 @@ def __init__(self, base_url: str, auth: Auth, requester: Requester): self._rooms: Optional[AsyncRoomsClient] = None self._voices: Optional[AsyncVoicesClient] = None self._speech: Optional[AsyncSpeechClient] = None - self._translations: Optional[AsyncTranslationsClient] = None + self._transcriptions: Optional[AsyncTranscriptionsClient] = None @property def rooms(self) -> "AsyncRoomsClient": @@ -91,11 +93,11 @@ def voices(self) -> "AsyncVoicesClient": return self._voices @property - def translations(self) -> "AsyncTranslationsClient": - if self._translations is None: - from .translations import AsyncTranslationsClient + def transcriptions(self) -> "AsyncTranscriptionsClient": + if self._transcriptions is None: + from .transcriptions import AsyncTranscriptionsClient - self._translations = AsyncTranslationsClient( + self._transcriptions = AsyncTranscriptionsClient( base_url=self._base_url, auth=self._auth, requester=self._requester ) - return self._translations + return self._transcriptions diff --git a/cozepy/audio/translations/__init__.py b/cozepy/audio/transcriptions/__init__.py similarity index 67% rename from cozepy/audio/translations/__init__.py rename to cozepy/audio/transcriptions/__init__.py index d4c72a9..bac1f82 100644 --- a/cozepy/audio/translations/__init__.py +++ b/cozepy/audio/transcriptions/__init__.py @@ -7,12 +7,12 @@ from cozepy.util import remove_url_trailing_slash -class CreateTranslationResp(CozeModel): +class CreateTranscriptionsResp(CozeModel): # The text of translation text: str -class TranslationsClient(object): +class TranscriptionsClient(object): def __init__(self, base_url: str, auth: Auth, requester: Requester): self._base_url = remove_url_trailing_slash(base_url) self._auth = auth @@ -23,22 +23,22 @@ def create( *, file: FileTypes, **kwargs, - ) -> CreateTranslationResp: + ) -> CreateTranscriptionsResp: """ - create translation + create transcriptions :param file: The file to be translated. - :return: create translation result + :return: create transcriptions result """ - url = f"{self._base_url}/v1/audio/translations" + url = f"{self._base_url}/v1/audio/transcriptions" headers: Optional[dict] = kwargs.get("headers") files = {"file": _try_fix_file(file)} return self._requester.request( - "post", url, stream=False, cast=CreateTranslationResp, headers=headers, files=files + "post", url, stream=False, cast=CreateTranscriptionsResp, headers=headers, files=files ) -class AsyncTranslationsClient(object): +class AsyncTranscriptionsClient(object): """ Room service async client. """ @@ -53,16 +53,16 @@ async def create( *, file: FileTypes, **kwargs, - ) -> CreateTranslationResp: + ) -> CreateTranscriptionsResp: """ - create translation + create transcriptions :param file: The file to be translated. - :return: create translation result + :return: create transcriptions result """ - url = f"{self._base_url}/v1/audio/translations" + url = f"{self._base_url}/v1/audio/transcriptions" files = {"file": _try_fix_file(file)} headers: Optional[dict] = kwargs.get("headers") return await self._requester.arequest( - "post", url, stream=False, cast=CreateTranslationResp, headers=headers, files=files + "post", url, stream=False, cast=CreateTranscriptionsResp, headers=headers, files=files ) diff --git a/tests/test_audio_translations.py b/tests/test_audio_translations.py index 97daff8..b494cb5 100644 --- a/tests/test_audio_translations.py +++ b/tests/test_audio_translations.py @@ -6,7 +6,7 @@ from tests.test_util import logid_key -def mock_create_translation(respx_mock): +def mock_create_transcriptions(respx_mock): logid = random_hex(10) raw_response = httpx.Response( 200, @@ -19,19 +19,19 @@ def mock_create_translation(respx_mock): }, ) - respx_mock.post("/v1/audio/translations").mock(raw_response) + respx_mock.post("/v1/audio/transcriptions").mock(raw_response) return logid @pytest.mark.respx(base_url="https://api.coze.com") -class TestAudioTranslation: - def test_sync_translation_create(self, respx_mock): +class TestSyncAudioTranscriptions: + def test_sync_transcriptions_create(self, respx_mock): coze = Coze(auth=TokenAuth(token="token")) - mock_logid = mock_create_translation(respx_mock) + mock_logid = mock_create_transcriptions(respx_mock) - res = coze.audio.translations.create(file=("filename", "content")) + res = coze.audio.transcriptions.create(file=("filename", "content")) assert res assert res.response.logid is not None assert res.response.logid == mock_logid @@ -39,12 +39,12 @@ def test_sync_translation_create(self, respx_mock): @pytest.mark.respx(base_url="https://api.coze.com") @pytest.mark.asyncio -class TestAsyncAudioTranslation: - async def test_async_translation_create(self, respx_mock): +class TestAsyncAudioTranscriptions: + async def test_async_transcriptions_create(self, respx_mock): coze = AsyncCoze(auth=TokenAuth(token="token")) - mock_logid = mock_create_translation(respx_mock) + mock_logid = mock_create_transcriptions(respx_mock) - res = await coze.audio.translations.create(file=("filename", "content")) + res = await coze.audio.transcriptions.create(file=("filename", "content")) assert res assert res.response.logid == mock_logid