> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# 管理 workspace

> 在本节中，你将学习如何使用 Python SDK 管理 workspace。workspace 让你能够组织多个应用，并与团队成员协作。

[Client]: client

workspace 是一种组织应用并与团队成员协作的方式。你可以使用 Client 类来创建、获取、列出和删除 workspace。

## 创建 workspace

`client.create_workspace` 返回一个 `Workspace` 对象。

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    workspace = await client.create_workspace(name='My Workspace')
    
    print(workspace.id)              # Workspace identifier
    print(workspace.name)             # 'My Workspace'
    print(workspace.owner)            # Owner member information
    print(workspace.members)          # List of workspace members
    print(workspace.applications)     # List of applications in the workspace
    print(workspace.createdAt)        # Workspace creation timestamp
```

## 获取 workspace

`client.get_workspace` 返回一个 `Workspace` 对象。

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    workspace = await client.get_workspace(workspace_id='WORKSPACE_ID')
    
    print(workspace.id)          # Workspace identifier
    print(workspace.name)         # Workspace name
    print(workspace.owner)        # Owner member information
    print(workspace.members)      # List of workspace members
    print(workspace.applications) # List of applications in the workspace
```

## 列出所有 workspace

`client.all_workspaces` 返回一个由 `Workspace` 对象组成的列表。

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    workspaces = await client.all_workspaces()
    
    for workspace in workspaces:
        print(f"Workspace: {workspace.name} (ID: {workspace.id})")
        print(f"  Owner: {workspace.owner.name}")
        print(f"  Members: {len(workspace.members)}")
        print(f"  Applications: {len(workspace.applications)}")
```

## 删除 workspace

`client.delete_workspace` 返回一个 `Response` 对象。

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    response = await client.delete_workspace(workspace_id='WORKSPACE_ID')
    print(f"Workspace deleted successfully: {response.status}")
```

## 退出 workspace

如果你是某个 workspace 的成员但不是所有者，可以使用 `client.leave_workspace` 退出该 workspace，它会返回一个 `Response` 对象。

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    response = await client.leave_workspace(workspace_id='WORKSPACE_ID')
    print(f"Left workspace successfully: {response.status}")
```

## workspace 数据结构

`Workspace` 对象包含以下信息：

| 属性             | 类型                  | 描述                        |
| -------------- | ------------------- | ------------------------- |
| `id`           | `str`               | workspace 唯一标识符           |
| `name`         | `str`               | workspace 名称              |
| `owner`        | `Member`            | workspace 所有者信息           |
| `members`      | `list[Member]`      | workspace 成员列表            |
| `applications` | `list[Application]` | 与该 workspace 关联的应用        |
| `createdAt`    | `str`               | workspace 创建时间戳（ISO 8601） |

## 成员数据结构

workspace 中的每个成员具有以下结构：

| 属性      | 类型    | 描述                    |
| ------- | ----- | --------------------- |
| `id`    | `str` | 成员唯一标识符               |
| `name`  | `str` | 成员显示名称                |
| `group` | `str` | 成员在 workspace 中的角色或分组 |
