Skip to content

Commit

Permalink
feat: add get workspace list api
Browse files Browse the repository at this point in the history
  • Loading branch information
chyroc committed Sep 23, 2024
1 parent 5f63af6 commit 62c9a4d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
12 changes: 11 additions & 1 deletion cozepy/coze.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from cozepy.request import Requester

if TYPE_CHECKING:
from cozepy.bot import BotClient
from .bot import BotClient
from .workspace import WorkspaceClient


class Coze(object):
Expand All @@ -20,6 +21,7 @@ def __init__(

# service client
self._bot = None
self._workspace = None

@property
def bot(self) -> "BotClient":
Expand All @@ -28,3 +30,11 @@ def bot(self) -> "BotClient":

self._bot = BotClient(self._base_url, self._auth, self._requester)
return self._bot

@property
def workspace(self) -> "WorkspaceClient":
if not self._workspace:
from cozepy.workspace import WorkspaceClient

self._workspace = WorkspaceClient(self._base_url, self._auth, self._requester)
return self._workspace
2 changes: 1 addition & 1 deletion cozepy/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __parse_requests_code_msg(self, r: Response) -> Tuple[Optional[int], str, Op
return

if "code" in json and "msg" in json and int(json["code"]) > 0:
return int(json["code"]), json["msg"], json["data"]
return int(json["code"]), json["msg"], json.get("data") or None
if "error_message" in json and json["error_message"] != "":
return None, json["error_message"], None
if "data" in json:
Expand Down
59 changes: 59 additions & 0 deletions cozepy/workspace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from enum import StrEnum
from typing import List

from cozepy import Auth, NumberPaged
from cozepy.model import CozeModel
from cozepy.request import Requester


class WorkspaceRoleType(StrEnum):
owner = "owner"
admin = "admin"
member = "member"


class WorkspaceType(StrEnum):
personal = "personal"
team = "team"


class Workspace(CozeModel):
# workspace id
id: str
# workspace name
name: str
# workspace icon url
icon_url: str
# user in workspace role type
role_type: WorkspaceRoleType
# workspace type
workspace_type: WorkspaceType


class WorkspaceClient(object):
"""
Bot class.
"""

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

def list_workspace_v1(self, *, page_num: int = 1, page_size: int = 20, headers=None) -> NumberPaged[Workspace]:
url = f"{self._base_url}/v1/workspaces"
params = {
"page_size": page_size,
"page_num": page_num,
}
data = self._requester.request("get", url, self._PrivateListPublishedBotsV1Data, headers=headers, params=params)
return NumberPaged(
items=data.workspaces,
page_num=page_num,
page_size=page_size,
total=data.total_count,
)

class _PrivateListPublishedBotsV1Data(CozeModel):
total_count: int
workspaces: List[Workspace]

0 comments on commit 62c9a4d

Please sign in to comment.