API開放平臺場景示例:獲取所有部門
更新時間:
調(diào)用“根據(jù)部門ID獲取部門的基本信息”接口,從返回值中獲取根部門ID,傳入“獲取指定部門ID下的子部門”接口,遞歸獲取部門分頁數(shù)據(jù)。
相關(guān)接口
基本流程
Python示例代碼
# -*- coding: utf-8 -*-
import requests
# 導入獲取訪問令牌的函數(shù),路徑根據(jù)實際情況進行修改,或直接給access_token賦值用于測試
from api_demo.get_access_token import access_token
def get_department_id(v_depart_id):
"""
根據(jù)部門id獲取部門的基本信息
https://mailbestwisewords.com/openapi/index.html#/operations/alimailpb_ud_DepartmentService_GetDepartment
:param v_depart_id: 部門ID,用于獲取特定部門的信息
:return 返回包含部門信息的JSON響應(yīng)
"""
url = f"https://alimail-cn.aliyuncs.com/v2/departments/{v_depart_id}"
querystring = {}
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + access_token
}
response = requests.request("GET", url, headers=headers, params=querystring)
print('##########################################################################')
print('請求參數(shù):', querystring)
print('返回參數(shù):', response.status_code, response.text)
print('##########################################################################')
return response.json()
def get_department_list(offset, limit, v_depart_id):
"""
獲取指定部門 id 下的子部門
https://mailbestwisewords.com/openapi/index.html#/operations/alimailpb_ud_DepartmentService_ListSubDepartments
:param offset: 分頁查詢的起始位置
:param limit: 每頁查詢的數(shù)量
:param v_depart_id: 部門ID,用于獲取該部門下的子部門
:return: 返回包含子部門列表和總子部門數(shù)量的列表
"""
url = "https://alimail-cn.aliyuncs.com/v2/departments/" + v_depart_id + "/departments"
querystring = {"offset": offset, "limit": limit}
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + access_token
}
response = requests.request("GET", url, headers=headers, params=querystring)
v_departments = response.json()
departments_items = v_departments["departments"]
v_total_sub = v_departments["total"]
return departments_items, v_total_sub
def get_department_list_sub_items(department_id):
"""
遞歸調(diào)用獲取指定部門 id 下的子部門
:param department_id: 部門ID,用于查詢該部門下的所有子部門
:return: 返回一個包含所有子部門的列表
"""
# 初始化分頁查詢的起始位置和每頁數(shù)量
offset = 0
limit = 100
# 初始化用于存儲所有子部門的列表
total_departments_items = []
# 打印開始查詢的部門ID
print('##########################################################################')
print(f'開始查詢:{department_id}')
while True:
# 調(diào)用get_department_list函數(shù)獲取當前頁的子部門列表和總子部門數(shù)量
departments_items, v_total_sub = get_department_list(str(offset), str(limit), department_id)
# 打印當前頁碼、子部門列表和數(shù)量
print(f'子部門:第{(offset // limit) + 1}頁:{departments_items}')
# 將當前頁的子部門列表添加到總列表中
total_departments_items.extend(departments_items)
# 如果已獲取的子部門數(shù)量達到或超過總數(shù)量,則結(jié)束循環(huán)
if len(total_departments_items) >= v_total_sub:
break
# 更新分頁查詢的起始位置
offset += limit
print(f'合計:{v_total_sub}個子部門')
# 遞歸獲取子部門的子部門
for item in total_departments_items:
# 遞歸調(diào)用自身獲取每個子部門下的所有子部門
sub_items = get_department_list_sub_items(item['id'])
# 將子部門的子部門添加到總列表中
total_departments_items.extend(sub_items)
# 返回包含所有子部門的列表
return total_departments_items
# 獲取主部門ID
root_department = get_department_id('$root')
root_department_id = root_department['id']
root_department_name = root_department['name']
print(f'根部門ID:{root_department_id}')
print(f'根部門名稱:{root_department_name}')
# 獲取所有子部門
total_departments = get_department_list_sub_items(root_department_id)
print(total_departments)
運行結(jié)果
文檔內(nèi)容是否對您有幫助?