departments
feishu.contact.departments
¶
DepartmentNamespace
¶
Bases: Namespace
部门接口命名空间。
通过 client.contact.departments 访问,封装飞书通讯录中部门相关的服务端接口,包括部门信息查询、
子部门遍历、上级部门链展开,以及创建、更新与删除部门等能力。读取方法返回飞书原始数据体;如需规整
结构,可由调用方显式套用 feishu.contact.normalize.normalize_department。
通常无需直接实例化,应通过 client.contact.departments 访问。
飞书文档
源代码位于: feishu/contact/departments.py
| Python | |
|---|---|
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
create
async
¶
create(department: dict[str, Any], *, department_id_type: str | None = None, user_id_type: str | None = None) -> NestedDict
创建部门。
将 department 作为请求体发送至创建部门接口。仅在显式传入时附带 department_id_type /
user_id_type 查询参数,未设置时省略。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
dict[str, Any]
|
部门数据,作为请求体发送。 |
必需 |
|
str | None
|
部门 ID 类型,可选 |
None
|
|
str | None
|
用户 ID 类型,可选 |
None
|
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
飞书返回的 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/contact/departments.py
delete
async
¶
delete(department_id: str, *, department_id_type: str | None = None) -> NestedDict
删除部门。
仅在显式传入时附带 department_id_type 查询参数,未设置时省略。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
部门 ID。 |
必需 |
|
str | None
|
部门 ID 类型,可选 |
None
|
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
飞书返回的 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
源代码位于: feishu/contact/departments.py
expand_ids
async
¶
expand_ids(department_ids: list[str], *, department_id_type: str = 'open_department_id') -> list[str]
将一组部门 ID 展开为包含其全部上级部门的去重列表。
对每个传入的部门,先保留自身,再追加其由 feishu.contact.departments.DepartmentNamespace.parent_ids 解析出的上级部门链; 最终按首次出现顺序去重,常用于将部门授权范围向上扩展。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
list[str]
|
起始部门 ID 列表。 |
必需 |
|
str
|
部门 ID 类型,可选 |
'open_department_id'
|
返回:
| 类型 | 描述 |
|---|---|
list[str]
|
包含各起始部门及其所有上级部门的 ID 列表,已按首次出现顺序去重。 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/contact/departments.py
get
async
¶
get(department_id: str, *, department_id_type: str = 'open_department_id') -> NestedDict
获取单个部门信息。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
部门 ID。 |
必需 |
|
str
|
部门 ID 类型,可选 |
'open_department_id'
|
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
飞书返回的 |
NestedDict
|
|
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/contact/departments.py
list
async
¶
list(department_id: str = '0', *, department_id_type: str = 'open_department_id', fetch_child: bool = True, page_size: int = 50, max_items: int | None = None) -> list[NestedDict]
获取子部门列表。
自动翻页并将每一页的原始结果汇总返回。单次请求的 page_size 受飞书限制,超过 50 时会被收敛为 50。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
父部门 ID。默认为 |
'0'
|
|
str
|
部门 ID 类型,可选 |
'open_department_id'
|
|
bool
|
是否递归获取所有下级部门。默认为 |
True
|
|
int
|
每页数量,最大为 50。默认为 50。 |
50
|
|
int | None
|
最多返回的部门数量, |
None
|
返回:
| 类型 | 描述 |
|---|---|
list[NestedDict]
|
飞书原始部门对象列表。如需规整结构,可对每项套用 |
list[NestedDict]
|
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/contact/departments.py
parent_ids
async
¶
parent_ids(department_id: str, *, department_id_type: str = 'open_department_id') -> list[str]
获取指定部门的上级部门 ID 链。
自动翻页并按从近到远的顺序返回各级上级部门的 ID,优先取 open_department_id,缺失时回退到
department_id。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
目标部门 ID。 |
必需 |
|
str
|
部门 ID 类型,可选 |
'open_department_id'
|
返回:
| 类型 | 描述 |
|---|---|
list[str]
|
上级部门 ID 列表,按层级由近及远排列。 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/contact/departments.py
update
async
¶
update(department_id: str, department: dict[str, Any], *, department_id_type: str | None = None, user_id_type: str | None = None) -> NestedDict
更新部门信息。
以 PATCH 方式将 department 作为请求体发送至更新部门接口,仅更新传入的字段。仅在显式传入时附带
department_id_type / user_id_type 查询参数,未设置时省略。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
部门 ID。 |
必需 |
|
dict[str, Any]
|
待更新的部门字段,作为请求体发送。 |
必需 |
|
str | None
|
部门 ID 类型,可选 |
None
|
|
str | None
|
用户 ID 类型,可选 |
None
|
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
飞书返回的 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|