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

from __future__ import annotations

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from .._client import AsyncWorkOSClient, WorkOSClient

from .._types import NOT_GIVEN, NotGiven, RequestOptions
from .models import (
    DataIntegrationConfigurationListResponse,
    DataIntegrationConfigurationResponse,
)


class PipesProvider:
    """Pipes Provider API resources."""

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

    def list_organization_data_integration_configurations(
        self,
        organization_id: str,
        *,
        request_options: RequestOptions | None = None,
    ) -> DataIntegrationConfigurationListResponse:
        """List providers for an organization

        Returns a list of all providers available to the specified organization, along with any configured custom OAuth scopes, enabled state, and organization-managed credentials where applicable.

        Args:
            organization_id: An [Organization](https://workos.com/docs/reference/organization) identifier to list provider configurations for.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            DataIntegrationConfigurationListResponse

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

    def update_organization_data_integration_configuration(
        self,
        organization_id: str,
        slug: str,
        *,
        enabled: bool | None = None,
        scopes: list[str] | None | NotGiven = NOT_GIVEN,
        client_id: str | None = None,
        client_secret: str | None = None,
        config: dict[str, str] | None = None,
        request_options: RequestOptions | None = None,
    ) -> DataIntegrationConfigurationResponse:
        """Configure a provider for an organization

        Creates or updates an organization's provider configuration. Use this endpoint to enable or disable a provider, set custom OAuth scopes, or supply organization-managed OAuth credentials.

        Args:
            organization_id: An [Organization](https://workos.com/docs/reference/organization) identifier to configure the provider for.
            slug: The slug identifier of the provider to configure (e.g., `github`, `slack`, `notion`).
            enabled: Whether the provider is enabled for the organization.
            scopes: The OAuth scopes to request for the organization. Pass `null` to inherit the provider scopes.
            client_id: The OAuth client ID of the organization's own application. Must be provided together with `client_secret`, and only for providers whose credentials are supplied by the organization.
            client_secret: The OAuth client secret of the organization's own application. Must be provided together with `client_id`.
            config: Provider-specific config values to set for the organization, keyed by config field. Only fields the provider declares are accepted, and each value must match that field's pattern. Accepted only for providers whose credentials are organization-managed; for shared or custom credential providers, config belongs on the integration itself (via the data-integrations API) and supplying it here is rejected.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            DataIntegrationConfigurationResponse

        Raises:
            BadRequestError: If the request is malformed (400).
            AuthenticationError: If the API key is invalid (401).
            NotFoundError: If the resource is not found (404).
            UnprocessableEntityError: If the request data is unprocessable (422).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        body: dict[str, Any] = {
            k: v
            for k, v in {
                "enabled": enabled,
                "client_id": client_id,
                "client_secret": client_secret,
                "config": config,
            }.items()
            if v is not None
        }
        if not isinstance(scopes, NotGiven):
            body["scopes"] = scopes
        return self._client.request(
            method="put",
            path=(
                "organizations",
                str(organization_id),
                "data_integration_configurations",
                str(slug),
            ),
            body=body,
            model=DataIntegrationConfigurationResponse,
            request_options=request_options,
        )


class AsyncPipesProvider:
    """Pipes Provider API resources (async)."""

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

    async def list_organization_data_integration_configurations(
        self,
        organization_id: str,
        *,
        request_options: RequestOptions | None = None,
    ) -> DataIntegrationConfigurationListResponse:
        """List providers for an organization

        Returns a list of all providers available to the specified organization, along with any configured custom OAuth scopes, enabled state, and organization-managed credentials where applicable.

        Args:
            organization_id: An [Organization](https://workos.com/docs/reference/organization) identifier to list provider configurations for.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            DataIntegrationConfigurationListResponse

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

    async def update_organization_data_integration_configuration(
        self,
        organization_id: str,
        slug: str,
        *,
        enabled: bool | None = None,
        scopes: list[str] | None | NotGiven = NOT_GIVEN,
        client_id: str | None = None,
        client_secret: str | None = None,
        config: dict[str, str] | None = None,
        request_options: RequestOptions | None = None,
    ) -> DataIntegrationConfigurationResponse:
        """Configure a provider for an organization

        Creates or updates an organization's provider configuration. Use this endpoint to enable or disable a provider, set custom OAuth scopes, or supply organization-managed OAuth credentials.

        Args:
            organization_id: An [Organization](https://workos.com/docs/reference/organization) identifier to configure the provider for.
            slug: The slug identifier of the provider to configure (e.g., `github`, `slack`, `notion`).
            enabled: Whether the provider is enabled for the organization.
            scopes: The OAuth scopes to request for the organization. Pass `null` to inherit the provider scopes.
            client_id: The OAuth client ID of the organization's own application. Must be provided together with `client_secret`, and only for providers whose credentials are supplied by the organization.
            client_secret: The OAuth client secret of the organization's own application. Must be provided together with `client_id`.
            config: Provider-specific config values to set for the organization, keyed by config field. Only fields the provider declares are accepted, and each value must match that field's pattern. Accepted only for providers whose credentials are organization-managed; for shared or custom credential providers, config belongs on the integration itself (via the data-integrations API) and supplying it here is rejected.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            DataIntegrationConfigurationResponse

        Raises:
            BadRequestError: If the request is malformed (400).
            AuthenticationError: If the API key is invalid (401).
            NotFoundError: If the resource is not found (404).
            UnprocessableEntityError: If the request data is unprocessable (422).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        body: dict[str, Any] = {
            k: v
            for k, v in {
                "enabled": enabled,
                "client_id": client_id,
                "client_secret": client_secret,
                "config": config,
            }.items()
            if v is not None
        }
        if not isinstance(scopes, NotGiven):
            body["scopes"] = scopes
        return await self._client.request(
            method="put",
            path=(
                "organizations",
                str(organization_id),
                "data_integration_configurations",
                str(slug),
            ),
            body=body,
            model=DataIntegrationConfigurationResponse,
            request_options=request_options,
        )
