| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package plane_v2
- import (
- "encoding/json"
- "fmt"
- "go.uber.org/zap"
- )
- // ==================== Workspace API ====================
- // ListWorkspaces 列出当前用户的工作空间
- func ListWorkspaces() ([]byte, error) {
- resp, err := planeWebGet("/api/workspaces/")
- if err != nil {
- zap.L().Error("[PlaneWeb] ListWorkspaces 请求失败", zap.Error(err))
- return nil, err
- }
- if !isWebSuccess(resp.StatusCode) {
- zap.L().Error("[PlaneWeb] ListWorkspaces 返回错误", zap.String("status", resp.Status))
- return nil, fmt.Errorf("ListWorkspaces 失败: %s", resp.Status)
- }
- return resp.Body, nil
- }
- // CreateWorkspace 创建工作空间
- func CreateWorkspace(name, slug string) ([]byte, error) {
- payload := map[string]interface{}{
- "name": name,
- "slug": slug,
- }
- resp, err := planeWebPost("/api/workspaces/", payload)
- if err != nil {
- zap.L().Error("[PlaneWeb] CreateWorkspace 请求失败", zap.Error(err))
- return nil, err
- }
- if resp.StatusCode != 201 {
- zap.L().Error("[PlaneWeb] CreateWorkspace 返回错误",
- zap.String("status", resp.Status),
- zap.String("body", string(resp.Body)),
- )
- return nil, fmt.Errorf("CreateWorkspace 失败: %s", resp.Status)
- }
- return resp.Body, nil
- }
- // GetWorkspace 获取工作空间详情
- func GetWorkspace(slug string) ([]byte, error) {
- url := fmt.Sprintf("/api/workspaces/%s/", slug)
- resp, err := planeWebGet(url)
- if err != nil {
- zap.L().Error("[PlaneWeb] GetWorkspace 请求失败", zap.Error(err))
- return nil, err
- }
- if !isWebSuccess(resp.StatusCode) {
- zap.L().Error("[PlaneWeb] GetWorkspace 返回错误", zap.String("status", resp.Status))
- return nil, fmt.Errorf("GetWorkspace 失败: %s", resp.Status)
- }
- return resp.Body, nil
- }
- // UpdateWorkspace 更新工作空间
- func UpdateWorkspace(slug string, data map[string]interface{}) ([]byte, error) {
- url := fmt.Sprintf("/api/workspaces/%s/", slug)
- resp, err := planeWebPatch(url, data)
- if err != nil {
- zap.L().Error("[PlaneWeb] UpdateWorkspace 请求失败", zap.Error(err))
- return nil, err
- }
- if !isWebSuccess(resp.StatusCode) {
- zap.L().Error("[PlaneWeb] UpdateWorkspace 返回错误",
- zap.String("status", resp.Status),
- zap.String("body", string(resp.Body)),
- )
- return nil, fmt.Errorf("UpdateWorkspace 失败: %s", resp.Status)
- }
- return resp.Body, nil
- }
- // DeleteWorkspace 删除工作空间
- func DeleteWorkspace(slug string) error {
- url := fmt.Sprintf("/api/workspaces/%s/", slug)
- resp, err := planeWebDelete(url)
- if err != nil {
- zap.L().Error("[PlaneWeb] DeleteWorkspace 请求失败", zap.Error(err))
- return err
- }
- if resp.StatusCode != 204 {
- zap.L().Error("[PlaneWeb] DeleteWorkspace 返回错误", zap.String("status", resp.Status))
- return fmt.Errorf("DeleteWorkspace 失败: %s", resp.Status)
- }
- return nil
- }
- // GetCurrentWorkspace 获取当前配置的工作空间详情(使用配置中的slug)
- func GetCurrentWorkspace() ([]byte, error) {
- return GetWorkspace(webWorkspaceSlug)
- }
- // WorkspaceInfo 工作空间基础信息
- type WorkspaceInfo struct {
- ID string `json:"id"`
- Name string `json:"name"`
- Slug string `json:"slug"`
- }
- // ParseWorkspaceList 解析工作空间列表
- func ParseWorkspaceList(data []byte) ([]WorkspaceInfo, error) {
- var workspaces []WorkspaceInfo
- if err := json.Unmarshal(data, &workspaces); err != nil {
- return nil, fmt.Errorf("解析工作空间列表失败: %w", err)
- }
- return workspaces, nil
- }
|