Skip to content

Commit

Permalink
创建AsyncJWTAuth类支持异步更新鉴权码
Browse files Browse the repository at this point in the history
修复相关的鉴权调用,包括Requester和上层异步调用
修改AsyncCoze对象的auth属性为AsyncAuth类型
  • Loading branch information
admin committed Mar 4, 2025
1 parent 3919f2b commit c6fd892
Show file tree
Hide file tree
Showing 28 changed files with 100 additions and 81 deletions.
4 changes: 2 additions & 2 deletions cozepy/bots/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import IntEnum
from typing import List, Optional

from cozepy.auth import Auth
from cozepy.auth import Auth, AsyncAuth
from cozepy.model import AsyncNumberPaged, CozeModel, NumberPaged, NumberPagedResponse
from cozepy.request import HTTPRequest, Requester
from cozepy.util import remove_url_trailing_slash
Expand Down Expand Up @@ -284,7 +284,7 @@ class AsyncBotsClient(object):
Bot class.
"""

def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, auth: AsyncAuth, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester
Expand Down
4 changes: 2 additions & 2 deletions cozepy/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import httpx
from typing_extensions import Literal

from cozepy.auth import Auth
from cozepy.auth import Auth, AsyncAuth
from cozepy.model import AsyncIteratorHTTPResponse, AsyncStream, CozeModel, IteratorHTTPResponse, ListResponse, Stream
from cozepy.request import Requester
from cozepy.util import remove_url_trailing_slash
Expand Down Expand Up @@ -763,7 +763,7 @@ def messages(


class AsyncChatClient(object):
def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, auth: AsyncAuth, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester
Expand Down
4 changes: 2 additions & 2 deletions cozepy/chat/message/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from cozepy.auth import Auth
from cozepy.auth import Auth, AsyncAuth
from cozepy.chat import Message
from cozepy.model import ListResponse
from cozepy.request import Requester
Expand Down Expand Up @@ -37,7 +37,7 @@ def list(


class AsyncChatMessagesClient(object):
def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, auth: AsyncAuth, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester
Expand Down
4 changes: 2 additions & 2 deletions cozepy/conversations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any, Dict, List, Optional

from cozepy.auth import Auth
from cozepy.auth import Auth, AsyncAuth
from cozepy.chat import Message
from cozepy.model import AsyncNumberPaged, CozeModel, HTTPRequest, NumberPaged
from cozepy.request import Requester
Expand Down Expand Up @@ -129,7 +129,7 @@ def messages(self):


class AsyncConversationsClient(object):
def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, auth: AsyncAuth, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester
Expand Down
4 changes: 2 additions & 2 deletions cozepy/conversations/message/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict, List, Optional

from cozepy.auth import Auth
from cozepy.auth import Auth, AsyncAuth
from cozepy.chat import Message, MessageContentType, MessageRole
from cozepy.model import AsyncLastIDPaged, CozeModel, HTTPRequest, LastIDPaged, LastIDPagedResponse
from cozepy.request import Requester
Expand Down Expand Up @@ -215,7 +215,7 @@ class AsyncMessagesClient(object):
Message class.
"""

def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, auth: AsyncAuth, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester
Expand Down
2 changes: 1 addition & 1 deletion cozepy/coze.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def __init__(
):
self._auth = auth
self._base_url = remove_url_trailing_slash(base_url)
self._requester = Requester(auth=auth, async_client=http_client)
self._requester = Requester(async_auth=auth, async_client=http_client)

# service client
self._bots: Optional[AsyncBotsClient] = None
Expand Down
4 changes: 2 additions & 2 deletions cozepy/files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from typing import IO, Optional, Tuple, Union

from cozepy.auth import Auth
from cozepy.auth import Auth, AsyncAuth
from cozepy.model import CozeModel
from cozepy.request import Requester
from cozepy.util import remove_url_trailing_slash
Expand Down Expand Up @@ -93,7 +93,7 @@ def retrieve(self, *, file_id: str):


class AsyncFilesClient(object):
def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, auth: AsyncAuth, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester
Expand Down
4 changes: 2 additions & 2 deletions cozepy/knowledge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import warnings
from typing import TYPE_CHECKING, Optional

from cozepy.auth import Auth
from cozepy.auth import Auth, AsyncAuth
from cozepy.request import Requester
from cozepy.util import remove_url_trailing_slash

Expand Down Expand Up @@ -38,7 +38,7 @@ def documents(self) -> "DocumentsClient":


class AsyncKnowledgeClient(object):
def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, auth: AsyncAuth, requester: Requester):
warnings.warn(
"The 'coze.knowledge' module is deprecated and will be removed in a future version. "
"Please use 'coze.datasets' instead.",
Expand Down
4 changes: 2 additions & 2 deletions cozepy/knowledge/documents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import warnings
from typing import List, Optional

from cozepy.auth import Auth
from cozepy.auth import Auth, AsyncAuth
from cozepy.datasets.documents import (
Document, # noqa
DocumentBase, # noqa
Expand Down Expand Up @@ -212,7 +212,7 @@ def request_maker(i_page_num: int, i_page_size: int) -> HTTPRequest:


class AsyncDocumentsClient(object):
def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, auth: AsyncAuth, requester: Requester):
warnings.warn(
"The 'coze.knowledge.documents' module is deprecated and will be removed in a future version. "
"Please use 'coze.datasets.documents' instead.",
Expand Down
3 changes: 2 additions & 1 deletion tests/test_audio_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from cozepy import AsyncCoze, Coze, CreateRoomResp, TokenAuth
from cozepy.auth import AsyncTokenAuth
from cozepy.util import random_hex
from tests.test_util import logid_key

Expand Down Expand Up @@ -45,7 +46,7 @@ def test_sync_rooms_create(self, respx_mock):
@pytest.mark.asyncio
class TestAsyncAudioRooms:
async def test_async_rooms_create(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

bot_id = random_hex(10)
voice_id = random_hex(10)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_audio_speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from cozepy import AsyncCoze, Coze, TokenAuth
from cozepy.auth import AsyncTokenAuth
from cozepy.util import random_hex
from tests.test_util import logid_key

Expand Down Expand Up @@ -39,7 +40,7 @@ def test_sync_speech_create(self, respx_mock):
@pytest.mark.asyncio
class TestAsyncAudioSpeech:
async def test_async_speech_create(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_logid = mock_create_speech(respx_mock)

Expand Down
3 changes: 2 additions & 1 deletion tests/test_audio_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from cozepy import AsyncCoze, Coze, TokenAuth
from cozepy.auth import AsyncTokenAuth
from cozepy.util import random_hex
from tests.test_util import logid_key

Expand Down Expand Up @@ -41,7 +42,7 @@ def test_sync_transcriptions_create(self, respx_mock):
@pytest.mark.asyncio
class TestAsyncAudioTranscriptions:
async def test_async_transcriptions_create(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_logid = mock_create_transcriptions(respx_mock)

Expand Down
5 changes: 3 additions & 2 deletions tests/test_audio_voices.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

from cozepy import AsyncCoze, AudioFormat, Coze, TokenAuth, Voice
from cozepy.auth import AsyncTokenAuth
from cozepy.util import random_hex
from tests.test_util import logid_key

Expand Down Expand Up @@ -88,7 +89,7 @@ def test_clone_voice(self, respx_mock):
@pytest.mark.asyncio
class TestAsyncAudioVoices:
async def test_async_voices_list(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_logid = mock_list_voices(respx_mock)

Expand All @@ -100,7 +101,7 @@ async def test_async_voices_list(self, respx_mock):
assert len(voices) == 1

async def test_async_clone_voice(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_voice = mock_clone_voice(respx_mock)

Expand Down
11 changes: 6 additions & 5 deletions tests/test_bots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from cozepy import AsyncCoze, Bot, Coze, SimpleBot, TokenAuth
from cozepy.auth import AsyncTokenAuth
from cozepy.util import random_hex
from tests.test_util import logid_key

Expand Down Expand Up @@ -189,7 +190,7 @@ def test_sync_bots_list(self, respx_mock):
@pytest.mark.asyncio
class TestAsyncBots:
async def test_async_bots_create(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_bot = mock_create_bot(respx_mock)

Expand All @@ -200,7 +201,7 @@ async def test_async_bots_create(self, respx_mock):
assert bot.bot_id == mock_bot.bot_id

async def test_async_bots_update(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_logid = mock_update_bot(respx_mock)

Expand All @@ -210,7 +211,7 @@ async def test_async_bots_update(self, respx_mock):
assert res.response.logid == mock_logid

async def test_async_bots_publish(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_bot = mock_publish_bot(respx_mock)

Expand All @@ -221,7 +222,7 @@ async def test_async_bots_publish(self, respx_mock):
assert bot.bot_id == mock_bot.bot_id

async def test_async_bots_retrieve(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_bot = mock_retrieve_bot(respx_mock)

Expand All @@ -232,7 +233,7 @@ async def test_async_bots_retrieve(self, respx_mock):
assert bot.bot_id == mock_bot.bot_id

async def test_async_bots_list(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

space_id = random_hex(10)
total = 10
Expand Down
21 changes: 11 additions & 10 deletions tests/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
MessageObjectString,
TokenAuth,
)
from cozepy.auth import AsyncTokenAuth
from cozepy.util import random_hex, write_pcm_to_wav_file
from tests.test_util import logid_key, read_file

Expand Down Expand Up @@ -357,7 +358,7 @@ def test_sync_chat_poll(self, respx_mock):
@pytest.mark.asyncio
class TestAsyncChatConversationMessage:
async def test_async_chat_create(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

conversation_id = "conversation_id"
mock_logid = mock_chat_create(respx_mock, conversation_id, ChatStatus.FAILED)
Expand All @@ -369,7 +370,7 @@ async def test_async_chat_create(self, respx_mock):
assert res.conversation_id == conversation_id

async def test_async_chat_stream(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_chat_stream(respx_mock, read_file("testdata/chat_text_stream_resp.txt"))
stream = coze.chat.stream(bot_id="bot", user_id="user")
Expand Down Expand Up @@ -399,7 +400,7 @@ async def test_async_chat_stream(self, respx_mock):
assert events[len(events) - 1].event == ChatEventType.CONVERSATION_CHAT_COMPLETED

async def test_async_chat_audio_stream(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_logid = mock_chat_stream(respx_mock, read_file("testdata/chat_audio_stream_resp.txt")) # noqa: F841
stream = coze.chat.stream(
Expand All @@ -424,7 +425,7 @@ async def test_async_chat_audio_stream(self, respx_mock):
assert os.path.exists(temp_filename)

async def test_async_chat_stream_error(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_logid = mock_chat_stream(respx_mock, read_file("testdata/chat_error_resp.txt")) # noqa: F841
stream = coze.chat.stream(bot_id="bot", user_id="user")
Expand All @@ -433,7 +434,7 @@ async def test_async_chat_stream_error(self, respx_mock):
[event async for event in stream]

async def test_async_chat_stream_failed(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_logid = mock_chat_stream(respx_mock, read_file("testdata/chat_failed_resp.txt")) # noqa: F841
stream = coze.chat.stream(bot_id="bot", user_id="user")
Expand All @@ -444,7 +445,7 @@ async def test_async_chat_stream_failed(self, respx_mock):
assert events[0].chat.last_error.code == 5000

async def test_async_chat_stream_invalid_event(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_logid = mock_chat_stream(respx_mock, read_file("testdata/chat_invalid_resp.txt")) # noqa: F841

Expand All @@ -454,7 +455,7 @@ async def test_async_chat_stream_invalid_event(self, respx_mock):
[event async for event in stream]

async def test_async_chat_retrieve(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

conversation_id = "conversation_id"
mock_logid = mock_chat_retrieve(respx_mock, conversation_id, ChatStatus.FAILED)
Expand All @@ -466,7 +467,7 @@ async def test_async_chat_retrieve(self, respx_mock):
assert res.conversation_id == conversation_id

async def test_async_submit_tool_outputs_not_stream(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

conversation_id = "conversation_id"
mock_logid = mock_chat_submit_tool_outputs(respx_mock, conversation_id, ChatStatus.FAILED)
Expand All @@ -478,7 +479,7 @@ async def test_async_submit_tool_outputs_not_stream(self, respx_mock):
assert res.conversation_id == conversation_id

async def test_async_submit_tool_outputs_stream(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

mock_chat_submit_tool_outputs_stream(respx_mock, read_file("testdata/chat_text_stream_resp.txt"))
stream = coze.chat.submit_tool_outputs_stream(conversation_id="conversation", chat_id="chat", tool_outputs=[])
Expand Down Expand Up @@ -508,7 +509,7 @@ async def test_async_submit_tool_outputs_stream(self, respx_mock):
assert events[len(events) - 1].event == ChatEventType.CONVERSATION_CHAT_COMPLETED

async def test_async_chat_cancel(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

conversation_id = "conversation_id"
mock_logid = mock_chat_cancel(respx_mock, conversation_id, ChatStatus.FAILED)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_chat_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from cozepy import AsyncCoze, Coze, Message, TokenAuth
from cozepy.auth import AsyncTokenAuth
from cozepy.util import random_hex
from tests.test_util import logid_key

Expand Down Expand Up @@ -39,7 +40,7 @@ def test_sync_chat_messages_list(self, respx_mock):
@pytest.mark.asyncio
class TestAsyncChatMessages:
async def test_async_chat_messages_list(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))
coze = AsyncCoze(auth=AsyncTokenAuth(token="token"))

msg, msg2, logid = mock_chat_messages_list(respx_mock)

Expand Down
Loading

0 comments on commit c6fd892

Please sign in to comment.