Skip to content

Commit

Permalink
创建AsyncJWTAuth类支持异步更新鉴权码
Browse files Browse the repository at this point in the history
修复相关的鉴权调用,包括Requester和上层异步调用
修改AsyncCoze对象的auth属性为AsyncAuth类型
Auth和AsyncAuth只与Requester有关,删除非必要类的引用
  • Loading branch information
admin committed Mar 4, 2025
1 parent c6fd892 commit d2fef3d
Show file tree
Hide file tree
Showing 53 changed files with 297 additions and 404 deletions.
6 changes: 6 additions & 0 deletions cozepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
AsyncPKCEOAuthApp,
AsyncWebOAuthApp,
Auth,
AsyncAuth,
DeviceAuthCode,
DeviceOAuthApp,
JWTAuth,
AsyncJWTAuth,
JWTOAuthApp,
OAuthApp,
OAuthToken,
PKCEOAuthApp,
Scope,
TokenAuth,
AsyncTokenAuth,
WebOAuthApp,
load_oauth_app_from_config,
)
Expand Down Expand Up @@ -169,6 +172,9 @@
"AsyncPKCEOAuthApp",
"AsyncWebOAuthApp",
"Auth",
"AsyncAuth",
"AsyncJWTAuth",
"AsyncTokenAuth",
"DeviceAuthCode",
"DeviceOAuthApp",
"JWTAuth",
Expand Down
23 changes: 10 additions & 13 deletions cozepy/audio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import TYPE_CHECKING, Optional

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

Expand All @@ -12,9 +11,8 @@


class AudioClient(object):
def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester

self._rooms: Optional[RoomsClient] = None
Expand All @@ -27,15 +25,15 @@ def rooms(self) -> "RoomsClient":
if self._rooms is None:
from .rooms import RoomsClient

self._rooms = RoomsClient(base_url=self._base_url, auth=self._auth, requester=self._requester)
self._rooms = RoomsClient(base_url=self._base_url, requester=self._requester)
return self._rooms

@property
def speech(self) -> "SpeechClient":
if self._speech is None:
from .speech import SpeechClient

self._speech = SpeechClient(base_url=self._base_url, auth=self._auth, requester=self._requester)
self._speech = SpeechClient(base_url=self._base_url, requester=self._requester)
return self._speech

@property
Expand All @@ -44,7 +42,7 @@ def transcriptions(self) -> "TranscriptionsClient":
from .transcriptions import TranscriptionsClient

self._transcriptions = TranscriptionsClient(
base_url=self._base_url, auth=self._auth, requester=self._requester
base_url=self._base_url, requester=self._requester
)
return self._transcriptions

Expand All @@ -53,14 +51,13 @@ def voices(self) -> "VoicesClient":
if self._voices is None:
from .voices import VoicesClient

self._voices = VoicesClient(base_url=self._base_url, auth=self._auth, requester=self._requester)
self._voices = VoicesClient(base_url=self._base_url, requester=self._requester)
return self._voices


class AsyncAudioClient(object):
def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester

self._rooms: Optional[AsyncRoomsClient] = None
Expand All @@ -73,23 +70,23 @@ def rooms(self) -> "AsyncRoomsClient":
if self._rooms is None:
from .rooms import AsyncRoomsClient

self._rooms = AsyncRoomsClient(base_url=self._base_url, auth=self._auth, requester=self._requester)
self._rooms = AsyncRoomsClient(base_url=self._base_url, requester=self._requester)
return self._rooms

@property
def speech(self) -> "AsyncSpeechClient":
if self._speech is None:
from .speech import AsyncSpeechClient

self._speech = AsyncSpeechClient(base_url=self._base_url, auth=self._auth, requester=self._requester)
self._speech = AsyncSpeechClient(base_url=self._base_url, requester=self._requester)
return self._speech

@property
def voices(self) -> "AsyncVoicesClient":
if self._voices is None:
from .voices import AsyncVoicesClient

self._voices = AsyncVoicesClient(base_url=self._base_url, auth=self._auth, requester=self._requester)
self._voices = AsyncVoicesClient(base_url=self._base_url, requester=self._requester)
return self._voices

@property
Expand All @@ -98,6 +95,6 @@ def transcriptions(self) -> "AsyncTranscriptionsClient":
from .transcriptions import AsyncTranscriptionsClient

self._transcriptions = AsyncTranscriptionsClient(
base_url=self._base_url, auth=self._auth, requester=self._requester
base_url=self._base_url, requester=self._requester
)
return self._transcriptions
7 changes: 2 additions & 5 deletions cozepy/audio/rooms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Optional

from cozepy.auth import Auth
from cozepy.model import CozeModel
from cozepy.request import Requester
from cozepy.util import remove_url_trailing_slash
Expand All @@ -22,9 +21,8 @@ class RoomsClient(object):
Room service client.
"""

def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester

def create(
Expand Down Expand Up @@ -59,9 +57,8 @@ class AsyncRoomsClient(object):
Room service async client.
"""

def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester

async def create(
Expand Down
7 changes: 2 additions & 5 deletions cozepy/audio/speech/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from enum import Enum
from typing import Optional

from cozepy.auth import Auth
from cozepy.model import FileHTTPResponse
from cozepy.request import Requester
from cozepy.util import remove_url_trailing_slash
Expand All @@ -28,9 +27,8 @@ class SpeechClient(object):
speech service client.
"""

def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester

def create(
Expand Down Expand Up @@ -70,9 +68,8 @@ class AsyncSpeechClient(object):
speech service client.
"""

def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester

async def create(
Expand Down
7 changes: 2 additions & 5 deletions cozepy/audio/transcriptions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Optional

from cozepy.auth import Auth
from cozepy.files import FileTypes, _try_fix_file
from cozepy.model import CozeModel
from cozepy.request import Requester
Expand All @@ -13,9 +12,8 @@ class CreateTranscriptionsResp(CozeModel):


class TranscriptionsClient(object):
def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester

def create(
Expand Down Expand Up @@ -43,9 +41,8 @@ class AsyncTranscriptionsClient(object):
Room service async client.
"""

def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester

async def create(
Expand Down
7 changes: 2 additions & 5 deletions cozepy/audio/voices/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import List, Optional

from cozepy import AudioFormat
from cozepy.auth import Auth
from cozepy.files import FileTypes, _try_fix_file
from cozepy.model import AsyncNumberPaged, CozeModel, HTTPRequest, NumberPaged, NumberPagedResponse
from cozepy.request import Requester
Expand Down Expand Up @@ -55,9 +54,8 @@ def get_items(self) -> List[Voice]:


class VoicesClient(object):
def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester

def clone(
Expand Down Expand Up @@ -152,9 +150,8 @@ def request_maker(i_page_num: int, i_page_size: int) -> HTTPRequest:


class AsyncVoicesClient(object):
def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester

async def clone(
Expand Down
16 changes: 14 additions & 2 deletions cozepy/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ class Auth(abc.ABC):
It provides the abstract methods for getting the token type and token.
"""

@property
@abc.abstractmethod
def token_type(self) -> str:
"""
Expand All @@ -690,6 +691,7 @@ def token_type(self) -> str:
:return: token type
"""

@property
@abc.abstractmethod
def token(self) -> str:
"""
Expand All @@ -705,7 +707,7 @@ def authentication(self, headers: dict) -> None:
:param headers: http headers
:return: None
"""
headers["Authorization"] = f"{self.token_type()} {self.token()}"
headers["Authorization"] = f"{self.token_type} {self.token}"


class TokenAuth(Auth):
Expand All @@ -718,9 +720,11 @@ def __init__(self, token: str):
assert len(token) > 0
self._token = token

@property
def token_type(self) -> str:
return "Bearer"

@property
def token(self) -> str:
return self._token

Expand Down Expand Up @@ -755,9 +759,11 @@ def __init__(
client_id, private_key, public_key_id, base_url=remove_url_trailing_slash(base_url)
)

@property
def token_type(self) -> str:
return "Bearer"

@property
def token(self) -> str:
token = self._generate_token()
return token.access_token
Expand All @@ -776,6 +782,7 @@ class AsyncAuth(abc.ABC):
It provides the abstract methods for getting the token type and token.
"""

@property
@abc.abstractmethod
async def token_type(self) -> str:
"""
Expand All @@ -786,6 +793,7 @@ async def token_type(self) -> str:
:return: token type
"""

@property
@abc.abstractmethod
async def token(self) -> str:
"""
Expand All @@ -801,7 +809,7 @@ async def authentication(self, headers: dict) -> None:
:param headers: http headers
:return: None
"""
headers["Authorization"] = f"{await self.token_type()} {await self.token()}"
headers["Authorization"] = f"{await self.token_type} {await self.token}"


class AsyncTokenAuth(AsyncAuth):
Expand All @@ -814,9 +822,11 @@ def __init__(self, token: str):
assert len(token) > 0
self._token = token

@property
async def token_type(self) -> str:
return "Bearer"

@property
async def token(self) -> str:
return self._token

Expand Down Expand Up @@ -851,9 +861,11 @@ def __init__(
client_id, private_key, public_key_id, base_url=remove_url_trailing_slash(base_url)
)

@property
async def token_type(self) -> str:
return "Bearer"

@property
async def token(self) -> str:
token = await self._generate_token()
return token.access_token
Expand Down
7 changes: 2 additions & 5 deletions cozepy/bots/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from enum import IntEnum
from typing import List, Optional

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 @@ -130,9 +129,8 @@ class BotsClient(object):
Bot class.
"""

def __init__(self, base_url: str, auth: Auth, requester: Requester):
def __init__(self, base_url: str, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester

def create(
Expand Down Expand Up @@ -284,9 +282,8 @@ class AsyncBotsClient(object):
Bot class.
"""

def __init__(self, base_url: str, auth: AsyncAuth, requester: Requester):
def __init__(self, base_url: str, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
self._requester = requester

async def create(
Expand Down
Loading

0 comments on commit d2fef3d

Please sign in to comment.