# This file is auto-generated by oagen. Do not edit.

from __future__ import annotations

from typing import TYPE_CHECKING, Optional, Union

if TYPE_CHECKING:
    from .._client import AsyncWorkOSClient, WorkOSClient

from .._types import RequestOptions, enum_value
from .models import Directory, DirectoryUserWithGroups
from workos.common.models.directory_group import DirectoryGroup
from workos.common.models.pagination_order import PaginationOrder
from .._pagination import AsyncPage, SyncPage


class DirectorySync:
    """Directory Sync API resources."""

    def __init__(self, client: "WorkOSClient") -> None:
        self._client = client

    def list_directories(
        self,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        organization_id: Optional[str] = None,
        search: Optional[str] = None,
        domain: Optional[str] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> SyncPage[Directory]:
        """List Directories

        Get a list of all of your existing directories matching the criteria specified.

        Args:
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            order: Order the results by the creation time. Defaults to `desc`.
            organization_id: Filter Directories by their associated organization.
            search: Searchable text to match against Directory names.
            domain: (deprecated) Filter Directories by their associated domain.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            SyncPage[Directory]

        Raises:
            AuthorizationError: If the request is forbidden (403).
            UnprocessableEntityError: If the request data is unprocessable (422).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
                "organization_id": organization_id,
                "search": search,
                "domain": domain,
            }.items()
            if v is not None
        }
        return self._client.request_page(
            method="get",
            path=("directories",),
            model=Directory,
            params=params,
            request_options=request_options,
        )

    def get_directory(
        self,
        id: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> Directory:
        """Get a Directory

        Get the details of an existing directory.

        Args:
            id: Unique identifier for the Directory.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            Directory

        Raises:
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        return self._client.request(
            method="get",
            path=("directories", str(id)),
            model=Directory,
            request_options=request_options,
        )

    def delete_directory(
        self,
        id: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> None:
        """Delete a Directory

        Permanently deletes an existing directory. It cannot be undone.

        Args:
            id: Unique identifier for the Directory.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Raises:
            AuthorizationError: If the request is forbidden (403).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        self._client.request(
            method="delete",
            path=("directories", str(id)),
            request_options=request_options,
        )

    def list_groups(
        self,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        directory: Optional[str] = None,
        user: Optional[str] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> SyncPage[DirectoryGroup]:
        """List Directory Groups

        Get a list of all of existing directory groups matching the criteria specified.

        Args:
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `before="obj_123"` to fetch a new batch of objects before `"obj_123"`.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `after="obj_123"` to fetch a new batch of objects after `"obj_123"`.
            order: Order the results by the creation time. Supported values are `"asc"` (ascending), `"desc"` (descending), and `"normal"` (descending with reversed cursor semantics where `before` fetches older records and `after` fetches newer records). Defaults to `desc`.
            directory: Unique identifier of the WorkOS Directory. This value can be obtained from the WorkOS dashboard or from the WorkOS API.
            user: Unique identifier of the WorkOS Directory User. This value can be obtained from the WorkOS API.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            SyncPage[DirectoryGroup]

        Raises:
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            UnprocessableEntityError: If the request data is unprocessable (422).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
                "directory": directory,
                "user": user,
            }.items()
            if v is not None
        }
        return self._client.request_page(
            method="get",
            path=("directory_groups",),
            model=DirectoryGroup,
            params=params,
            request_options=request_options,
        )

    def get_group(
        self,
        id: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> DirectoryGroup:
        """Get a Directory Group

        Get the details of an existing Directory Group.

        Args:
            id: Unique identifier for the Directory Group.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            DirectoryGroup

        Raises:
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        return self._client.request(
            method="get",
            path=("directory_groups", str(id)),
            model=DirectoryGroup,
            request_options=request_options,
        )

    def list_users(
        self,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        directory: Optional[str] = None,
        group: Optional[str] = None,
        idp_id: Optional[str] = None,
        email: Optional[str] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> SyncPage[DirectoryUserWithGroups]:
        """List Directory Users

        Get a list of all of existing Directory Users matching the criteria specified.

        Args:
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `before="obj_123"` to fetch a new batch of objects before `"obj_123"`.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `after="obj_123"` to fetch a new batch of objects after `"obj_123"`.
            order: Order the results by the creation time. Supported values are `"asc"` (ascending), `"desc"` (descending), and `"normal"` (descending with reversed cursor semantics where `before` fetches older records and `after` fetches newer records). Defaults to `desc`.
            directory: Unique identifier of the WorkOS Directory. This value can be obtained from the WorkOS dashboard or from the WorkOS API.
            group: Unique identifier of the WorkOS Directory Group. This value can be obtained from the WorkOS API.
            idp_id: Filter Directory Users by the identity provider's unique identifier (`idp_id`). Requires the `directory` parameter to also be provided.
            email: Filter Directory Users by their primary email address. Requires the `directory` parameter to also be provided.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            SyncPage[DirectoryUserWithGroups]

        Raises:
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            UnprocessableEntityError: If the request data is unprocessable (422).
            RateLimitExceededError: If rate limited (429).
            AuthenticationError: If the API key is invalid (401).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
                "directory": directory,
                "group": group,
                "idp_id": idp_id,
                "email": email,
            }.items()
            if v is not None
        }
        return self._client.request_page(
            method="get",
            path=("directory_users",),
            model=DirectoryUserWithGroups,
            params=params,
            request_options=request_options,
        )

    def get_user(
        self,
        id: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> DirectoryUserWithGroups:
        """Get a Directory User

        Get the details of an existing Directory User.

        Args:
            id: Unique identifier for the Directory User.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            DirectoryUserWithGroups

        Raises:
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        return self._client.request(
            method="get",
            path=("directory_users", str(id)),
            model=DirectoryUserWithGroups,
            request_options=request_options,
        )


class AsyncDirectorySync:
    """Directory Sync API resources (async)."""

    def __init__(self, client: "AsyncWorkOSClient") -> None:
        self._client = client

    async def list_directories(
        self,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        organization_id: Optional[str] = None,
        search: Optional[str] = None,
        domain: Optional[str] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> AsyncPage[Directory]:
        """List Directories

        Get a list of all of your existing directories matching the criteria specified.

        Args:
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
            order: Order the results by the creation time. Defaults to `desc`.
            organization_id: Filter Directories by their associated organization.
            search: Searchable text to match against Directory names.
            domain: (deprecated) Filter Directories by their associated domain.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            AsyncPage[Directory]

        Raises:
            AuthorizationError: If the request is forbidden (403).
            UnprocessableEntityError: If the request data is unprocessable (422).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
                "organization_id": organization_id,
                "search": search,
                "domain": domain,
            }.items()
            if v is not None
        }
        return await self._client.request_page(
            method="get",
            path=("directories",),
            model=Directory,
            params=params,
            request_options=request_options,
        )

    async def get_directory(
        self,
        id: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> Directory:
        """Get a Directory

        Get the details of an existing directory.

        Args:
            id: Unique identifier for the Directory.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            Directory

        Raises:
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        return await self._client.request(
            method="get",
            path=("directories", str(id)),
            model=Directory,
            request_options=request_options,
        )

    async def delete_directory(
        self,
        id: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> None:
        """Delete a Directory

        Permanently deletes an existing directory. It cannot be undone.

        Args:
            id: Unique identifier for the Directory.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Raises:
            AuthorizationError: If the request is forbidden (403).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        await self._client.request(
            method="delete",
            path=("directories", str(id)),
            request_options=request_options,
        )

    async def list_groups(
        self,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        directory: Optional[str] = None,
        user: Optional[str] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> AsyncPage[DirectoryGroup]:
        """List Directory Groups

        Get a list of all of existing directory groups matching the criteria specified.

        Args:
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `before="obj_123"` to fetch a new batch of objects before `"obj_123"`.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `after="obj_123"` to fetch a new batch of objects after `"obj_123"`.
            order: Order the results by the creation time. Supported values are `"asc"` (ascending), `"desc"` (descending), and `"normal"` (descending with reversed cursor semantics where `before` fetches older records and `after` fetches newer records). Defaults to `desc`.
            directory: Unique identifier of the WorkOS Directory. This value can be obtained from the WorkOS dashboard or from the WorkOS API.
            user: Unique identifier of the WorkOS Directory User. This value can be obtained from the WorkOS API.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            AsyncPage[DirectoryGroup]

        Raises:
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            UnprocessableEntityError: If the request data is unprocessable (422).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
                "directory": directory,
                "user": user,
            }.items()
            if v is not None
        }
        return await self._client.request_page(
            method="get",
            path=("directory_groups",),
            model=DirectoryGroup,
            params=params,
            request_options=request_options,
        )

    async def get_group(
        self,
        id: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> DirectoryGroup:
        """Get a Directory Group

        Get the details of an existing Directory Group.

        Args:
            id: Unique identifier for the Directory Group.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            DirectoryGroup

        Raises:
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        return await self._client.request(
            method="get",
            path=("directory_groups", str(id)),
            model=DirectoryGroup,
            request_options=request_options,
        )

    async def list_users(
        self,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        directory: Optional[str] = None,
        group: Optional[str] = None,
        idp_id: Optional[str] = None,
        email: Optional[str] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> AsyncPage[DirectoryUserWithGroups]:
        """List Directory Users

        Get a list of all of existing Directory Users matching the criteria specified.

        Args:
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `before="obj_123"` to fetch a new batch of objects before `"obj_123"`.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `after="obj_123"` to fetch a new batch of objects after `"obj_123"`.
            order: Order the results by the creation time. Supported values are `"asc"` (ascending), `"desc"` (descending), and `"normal"` (descending with reversed cursor semantics where `before` fetches older records and `after` fetches newer records). Defaults to `desc`.
            directory: Unique identifier of the WorkOS Directory. This value can be obtained from the WorkOS dashboard or from the WorkOS API.
            group: Unique identifier of the WorkOS Directory Group. This value can be obtained from the WorkOS API.
            idp_id: Filter Directory Users by the identity provider's unique identifier (`idp_id`). Requires the `directory` parameter to also be provided.
            email: Filter Directory Users by their primary email address. Requires the `directory` parameter to also be provided.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            AsyncPage[DirectoryUserWithGroups]

        Raises:
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            UnprocessableEntityError: If the request data is unprocessable (422).
            RateLimitExceededError: If rate limited (429).
            AuthenticationError: If the API key is invalid (401).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
                "directory": directory,
                "group": group,
                "idp_id": idp_id,
                "email": email,
            }.items()
            if v is not None
        }
        return await self._client.request_page(
            method="get",
            path=("directory_users",),
            model=DirectoryUserWithGroups,
            params=params,
            request_options=request_options,
        )

    async def get_user(
        self,
        id: str,
        *,
        request_options: Optional[RequestOptions] = None,
    ) -> DirectoryUserWithGroups:
        """Get a Directory User

        Get the details of an existing Directory User.

        Args:
            id: Unique identifier for the Directory User.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            DirectoryUserWithGroups

        Raises:
            AuthorizationError: If the request is forbidden (403).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        return await self._client.request(
            method="get",
            path=("directory_users", str(id)),
            model=DirectoryUserWithGroups,
            request_options=request_options,
        )
