跳转至

directory

feishu.contact.directory

OrganizationSnapshot dataclass

Raw organization-directory facts returned by Feishu.

departments contains the descendants returned from the root department; users contains one raw user record for each stable Feishu identity found across the root and descendant departments. The SDK neither filters nor derives account information from email addresses.

源代码位于: feishu/contact/directory.py
Python
@dataclass(frozen=True, slots=True)
class OrganizationSnapshot:
    r"""Raw organization-directory facts returned by Feishu.

    ``departments`` contains the descendants returned from the root department;
    ``users`` contains one raw user record for each stable Feishu identity found
    across the root and descendant departments.  The SDK neither filters nor
    derives account information from email addresses.
    """

    departments: tuple[Mapping[str, Any], ...]
    users: tuple[Mapping[str, Any], ...]

fetch_organization_snapshot async

Python
fetch_organization_snapshot(client: Any, *, root_department_id: str = '0', page_size: int = 50, max_items: int | None = None) -> OrganizationSnapshot

Fetch raw departments and de-duplicated user facts for an organization.

The root department’s descendants are fetched once through client.contact.departments.list. Users are then listed for the root and every discovered department. user_id is requested and used as the canonical stable identity; open_id and union_id are only fallbacks when an API response lacks user_id.

参数:

名称 类型 描述 默认

client

Any

A Feishu client exposing the existing contact namespaces.

必需

root_department_id

str

Root department ID. Defaults to the Feishu root, "0".

'0'

page_size

int

Page size passed to the existing contact APIs.

50

max_items

int | None

Optional maximum passed to the existing contact APIs.

None

返回:

类型 描述
OrganizationSnapshot

An [OrganizationSnapshot][] containing raw Feishu department and user

OrganizationSnapshot

facts. User records are ordered by their stable Feishu identity.

源代码位于: feishu/contact/directory.py
Python
async def fetch_organization_snapshot(
    client: Any,
    *,
    root_department_id: str = "0",
    page_size: int = 50,
    max_items: int | None = None,
) -> OrganizationSnapshot:
    r"""Fetch raw departments and de-duplicated user facts for an organization.

    The root department's descendants are fetched once through
    ``client.contact.departments.list``.  Users are then listed for the root and
    every discovered department.  ``user_id`` is requested and used as the
    canonical stable identity; ``open_id`` and ``union_id`` are only fallbacks
    when an API response lacks ``user_id``.

    Args:
        client: A Feishu client exposing the existing contact namespaces.
        root_department_id: Root department ID. Defaults to the Feishu root,
            ``"0"``.
        page_size: Page size passed to the existing contact APIs.
        max_items: Optional maximum passed to the existing contact APIs.

    Returns:
        An [OrganizationSnapshot][] containing raw Feishu department and user
        facts. User records are ordered by their stable Feishu identity.
    """
    departments = tuple(
        await client.contact.departments.list(
            root_department_id,
            department_id_type="open_department_id",
            fetch_child=True,
            page_size=page_size,
            max_items=max_items,
        )
    )
    department_ids = sorted(
        {department_id for department in departments if (department_id := _department_id(department)) is not None}
    )

    users_by_identity: dict[tuple[str, str], Mapping[str, Any]] = {}
    for department_id in (root_department_id, *department_ids):
        users = await client.contact.users.list(
            department_id,
            user_id_type="user_id",
            department_id_type="open_department_id",
            page_size=page_size,
            max_items=max_items,
        )
        for user in users:
            identity = _user_identity(user)
            if identity is not None:
                users_by_identity.setdefault(identity, user)

    return OrganizationSnapshot(
        departments=departments,
        users=tuple(users_by_identity[identity] for identity in sorted(users_by_identity)),
    )