@@ -31,30 +31,36 @@ def create_workspace(
3131 f"Automatically create Cozeloop workspace with name { workspace_name } "
3232 )
3333
34- URL = "https://api.coze.cn/v1/workspaces"
34+ try :
35+ workspace_id = self .search_workspace_id (workspace_name = workspace_name )
36+ logger .info (f"Get existing Cozeloop workspace ID: { workspace_id } " )
3537
36- headers = {
37- "Authorization" : f"Bearer { self .api_key } " ,
38- "Content-Type" : "application/json" ,
39- }
40-
41- data = {
42- "name" : workspace_name ,
43- "description" : "Created by Volcengine Agent Development Kit (VeADK)" ,
44- }
45-
46- response = requests .post (URL , headers = headers , json = data )
47-
48- if response .json ().get ("code" ) == 0 :
49- workspace_id = response .json ().get ("data" ).get ("id" )
50- logger .info (f"Cozeloop workspace ID: { workspace_id } " )
5138 return workspace_id
52- else :
53- raise Exception (
54- f"Failed to automatically create workspace: { response .json ()} "
55- )
56-
57- def get_workspace_id (
39+ except Exception as _ :
40+ URL = "https://api.coze.cn/v1/workspaces"
41+
42+ headers = {
43+ "Authorization" : f"Bearer { self .api_key } " ,
44+ "Content-Type" : "application/json" ,
45+ }
46+
47+ data = {
48+ "name" : workspace_name ,
49+ "description" : "Created by Volcengine Agent Development Kit (VeADK)" ,
50+ }
51+
52+ response = requests .post (URL , headers = headers , json = data )
53+
54+ if response .json ().get ("code" ) == 0 :
55+ workspace_id = response .json ().get ("data" ).get ("id" )
56+ logger .info (f"New created Cozeloop workspace ID: { workspace_id } " )
57+ return workspace_id
58+ else :
59+ raise Exception (
60+ f"Failed to automatically create workspace: { response .json ()} "
61+ )
62+
63+ def search_workspace_id (
5864 self , workspace_name : str = DEFAULT_COZELOOP_SPACE_NAME
5965 ) -> str :
6066 logger .info (
@@ -73,7 +79,7 @@ def get_workspace_id(
7379 "page_size" : 50 ,
7480 }
7581
76- response = requests .post (URL , headers = headers , json = data )
82+ response = requests .get (URL , headers = headers , json = data )
7783
7884 if response .json ().get ("code" ) == 0 :
7985 workspaces = response .json ().get ("data" ).get ("workspaces" , [])
0 commit comments