Skip to content

Commit

Permalink
feat: Add support for asynchronous methods in auth (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbb1234567890 authored Mar 6, 2025
1 parent 464998b commit 0954d5e
Show file tree
Hide file tree
Showing 53 changed files with 414 additions and 367 deletions.
8 changes: 8 additions & 0 deletions cozepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
from .audio.transcriptions import CreateTranscriptionsResp
from .audio.voices import Voice
from .auth import (
AsyncAuth,
AsyncDeviceOAuthApp,
AsyncJWTAuth,
AsyncJWTOAuthApp,
AsyncPKCEOAuthApp,
AsyncTokenAuth,
AsyncWebOAuthApp,
Auth,
DeviceAuthCode,
Expand All @@ -16,6 +19,7 @@
OAuthToken,
PKCEOAuthApp,
Scope,
SyncAuth,
TokenAuth,
WebOAuthApp,
load_oauth_app_from_config,
Expand Down Expand Up @@ -171,6 +175,10 @@
"AsyncPKCEOAuthApp",
"AsyncWebOAuthApp",
"Auth",
"AsyncAuth",
"SyncAuth",
"AsyncJWTAuth",
"AsyncTokenAuth",
"DeviceAuthCode",
"DeviceOAuthApp",
"JWTAuth",
Expand Down
27 changes: 10 additions & 17 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,40 +25,37 @@ 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
def transcriptions(self) -> "TranscriptionsClient":
if self._transcriptions is None:
from .transcriptions import TranscriptionsClient

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

@property
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,31 +68,29 @@ 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
def transcriptions(self) -> "AsyncTranscriptionsClient":
if self._transcriptions is None:
from .transcriptions import AsyncTranscriptionsClient

self._transcriptions = AsyncTranscriptionsClient(
base_url=self._base_url, auth=self._auth, requester=self._requester
)
self._transcriptions = AsyncTranscriptionsClient(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
13 changes: 4 additions & 9 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 @@ -140,7 +138,6 @@ def request_maker(i_page_num: int, i_page_size: int) -> HTTPRequest:
"page_size": i_page_size,
},
cast=_PrivateListVoiceData,
is_async=False,
stream=False,
)

Expand All @@ -153,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 Expand Up @@ -225,8 +221,8 @@ async def list(
url = f"{self._base_url}/v1/audio/voices"
headers: Optional[dict] = kwargs.get("headers")

def request_maker(i_page_num: int, i_page_size: int) -> HTTPRequest:
return self._requester.make_request(
async def request_maker(i_page_num: int, i_page_size: int) -> HTTPRequest:
return await self._requester.amake_request(
"GET",
url,
params={
Expand All @@ -236,7 +232,6 @@ def request_maker(i_page_num: int, i_page_size: int) -> HTTPRequest:
},
headers=headers,
cast=_PrivateListVoiceData,
is_async=False,
stream=False,
)

Expand Down
Loading

0 comments on commit 0954d5e

Please sign in to comment.