# This file was auto-generated by Fern from our API Definition.

from __future__ import annotations

import os
import typing

import httpx
from .core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from .environment import ScoutEnvironment

if typing.TYPE_CHECKING:
    from .collections.client import AsyncCollectionsClient, CollectionsClient
    from .copilots.client import AsyncCopilotsClient, CopilotsClient
    from .documents.client import AsyncDocumentsClient, DocumentsClient
    from .environments.client import AsyncEnvironmentsClient, EnvironmentsClient
    from .integrations.client import AsyncIntegrationsClient, IntegrationsClient
    from .organizations.client import AsyncOrganizationsClient, OrganizationsClient
    from .revisions.client import AsyncRevisionsClient, RevisionsClient
    from .sources.client import AsyncSourcesClient, SourcesClient
    from .syncs.client import AsyncSyncsClient, SyncsClient
    from .tables.client import AsyncTablesClient, TablesClient
    from .triggers.client import AsyncTriggersClient, TriggersClient
    from .usage.client import AsyncUsageClient, UsageClient
    from .workflow_logs.client import AsyncWorkflowLogsClient, WorkflowLogsClient
    from .workflow_revisions.client import AsyncWorkflowRevisionsClient, WorkflowRevisionsClient
    from .workflows.client import AsyncWorkflowsClient, WorkflowsClient


class Scout:
    """
    Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propagate to these functions.

    Parameters
    ----------
    base_url : typing.Optional[str]
        The base url to use for requests from the client.

    environment : ScoutEnvironment
        The environment to use for requests from the client. from .environment import ScoutEnvironment



        Defaults to ScoutEnvironment.PROD



    api_key : typing.Optional[typing.Union[str, typing.Callable[[], str]]]
    headers : typing.Optional[typing.Dict[str, str]]
        Additional headers to send with every request.

    timeout : typing.Optional[float]
        The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.

    follow_redirects : typing.Optional[bool]
        Whether the default httpx client follows redirects or not, this is irrelevant if a custom httpx client is passed in.

    httpx_client : typing.Optional[httpx.Client]
        The httpx client to use for making requests, a preconfigured client is used by default, however this is useful should you want to pass in any custom httpx configuration.

    Examples
    --------
    from scoutos import Scout

    client = Scout(
        api_key="YOUR_API_KEY",
    )
    """

    def __init__(
        self,
        *,
        base_url: typing.Optional[str] = None,
        environment: ScoutEnvironment = ScoutEnvironment.PROD,
        api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = os.getenv("SCOUT_API_KEY"),
        headers: typing.Optional[typing.Dict[str, str]] = None,
        timeout: typing.Optional[float] = None,
        follow_redirects: typing.Optional[bool] = True,
        httpx_client: typing.Optional[httpx.Client] = None,
    ):
        _defaulted_timeout = (
            timeout if timeout is not None else 60 if httpx_client is None else httpx_client.timeout.read
        )
        self._client_wrapper = SyncClientWrapper(
            base_url=_get_base_url(base_url=base_url, environment=environment),
            api_key=api_key,
            headers=headers,
            httpx_client=httpx_client
            if httpx_client is not None
            else httpx.Client(timeout=_defaulted_timeout, follow_redirects=follow_redirects)
            if follow_redirects is not None
            else httpx.Client(timeout=_defaulted_timeout),
            timeout=_defaulted_timeout,
        )
        self._workflows: typing.Optional[WorkflowsClient] = None
        self._workflow_revisions: typing.Optional[WorkflowRevisionsClient] = None
        self._revisions: typing.Optional[RevisionsClient] = None
        self._environments: typing.Optional[EnvironmentsClient] = None
        self._usage: typing.Optional[UsageClient] = None
        self._workflow_logs: typing.Optional[WorkflowLogsClient] = None
        self._copilots: typing.Optional[CopilotsClient] = None
        self._triggers: typing.Optional[TriggersClient] = None
        self._integrations: typing.Optional[IntegrationsClient] = None
        self._organizations: typing.Optional[OrganizationsClient] = None
        self._collections: typing.Optional[CollectionsClient] = None
        self._tables: typing.Optional[TablesClient] = None
        self._documents: typing.Optional[DocumentsClient] = None
        self._sources: typing.Optional[SourcesClient] = None
        self._syncs: typing.Optional[SyncsClient] = None

    @property
    def workflows(self):
        if self._workflows is None:
            from .workflows.client import WorkflowsClient  # noqa: E402

            self._workflows = WorkflowsClient(client_wrapper=self._client_wrapper)
        return self._workflows

    @property
    def workflow_revisions(self):
        if self._workflow_revisions is None:
            from .workflow_revisions.client import WorkflowRevisionsClient  # noqa: E402

            self._workflow_revisions = WorkflowRevisionsClient(client_wrapper=self._client_wrapper)
        return self._workflow_revisions

    @property
    def revisions(self):
        if self._revisions is None:
            from .revisions.client import RevisionsClient  # noqa: E402

            self._revisions = RevisionsClient(client_wrapper=self._client_wrapper)
        return self._revisions

    @property
    def environments(self):
        if self._environments is None:
            from .environments.client import EnvironmentsClient  # noqa: E402

            self._environments = EnvironmentsClient(client_wrapper=self._client_wrapper)
        return self._environments

    @property
    def usage(self):
        if self._usage is None:
            from .usage.client import UsageClient  # noqa: E402

            self._usage = UsageClient(client_wrapper=self._client_wrapper)
        return self._usage

    @property
    def workflow_logs(self):
        if self._workflow_logs is None:
            from .workflow_logs.client import WorkflowLogsClient  # noqa: E402

            self._workflow_logs = WorkflowLogsClient(client_wrapper=self._client_wrapper)
        return self._workflow_logs

    @property
    def copilots(self):
        if self._copilots is None:
            from .copilots.client import CopilotsClient  # noqa: E402

            self._copilots = CopilotsClient(client_wrapper=self._client_wrapper)
        return self._copilots

    @property
    def triggers(self):
        if self._triggers is None:
            from .triggers.client import TriggersClient  # noqa: E402

            self._triggers = TriggersClient(client_wrapper=self._client_wrapper)
        return self._triggers

    @property
    def integrations(self):
        if self._integrations is None:
            from .integrations.client import IntegrationsClient  # noqa: E402

            self._integrations = IntegrationsClient(client_wrapper=self._client_wrapper)
        return self._integrations

    @property
    def organizations(self):
        if self._organizations is None:
            from .organizations.client import OrganizationsClient  # noqa: E402

            self._organizations = OrganizationsClient(client_wrapper=self._client_wrapper)
        return self._organizations

    @property
    def collections(self):
        if self._collections is None:
            from .collections.client import CollectionsClient  # noqa: E402

            self._collections = CollectionsClient(client_wrapper=self._client_wrapper)
        return self._collections

    @property
    def tables(self):
        if self._tables is None:
            from .tables.client import TablesClient  # noqa: E402

            self._tables = TablesClient(client_wrapper=self._client_wrapper)
        return self._tables

    @property
    def documents(self):
        if self._documents is None:
            from .documents.client import DocumentsClient  # noqa: E402

            self._documents = DocumentsClient(client_wrapper=self._client_wrapper)
        return self._documents

    @property
    def sources(self):
        if self._sources is None:
            from .sources.client import SourcesClient  # noqa: E402

            self._sources = SourcesClient(client_wrapper=self._client_wrapper)
        return self._sources

    @property
    def syncs(self):
        if self._syncs is None:
            from .syncs.client import SyncsClient  # noqa: E402

            self._syncs = SyncsClient(client_wrapper=self._client_wrapper)
        return self._syncs


class AsyncScout:
    """
    Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propagate to these functions.

    Parameters
    ----------
    base_url : typing.Optional[str]
        The base url to use for requests from the client.

    environment : ScoutEnvironment
        The environment to use for requests from the client. from .environment import ScoutEnvironment



        Defaults to ScoutEnvironment.PROD



    api_key : typing.Optional[typing.Union[str, typing.Callable[[], str]]]
    headers : typing.Optional[typing.Dict[str, str]]
        Additional headers to send with every request.

    timeout : typing.Optional[float]
        The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.

    follow_redirects : typing.Optional[bool]
        Whether the default httpx client follows redirects or not, this is irrelevant if a custom httpx client is passed in.

    httpx_client : typing.Optional[httpx.AsyncClient]
        The httpx client to use for making requests, a preconfigured client is used by default, however this is useful should you want to pass in any custom httpx configuration.

    Examples
    --------
    from scoutos import AsyncScout

    client = AsyncScout(
        api_key="YOUR_API_KEY",
    )
    """

    def __init__(
        self,
        *,
        base_url: typing.Optional[str] = None,
        environment: ScoutEnvironment = ScoutEnvironment.PROD,
        api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = os.getenv("SCOUT_API_KEY"),
        headers: typing.Optional[typing.Dict[str, str]] = None,
        timeout: typing.Optional[float] = None,
        follow_redirects: typing.Optional[bool] = True,
        httpx_client: typing.Optional[httpx.AsyncClient] = None,
    ):
        _defaulted_timeout = (
            timeout if timeout is not None else 60 if httpx_client is None else httpx_client.timeout.read
        )
        self._client_wrapper = AsyncClientWrapper(
            base_url=_get_base_url(base_url=base_url, environment=environment),
            api_key=api_key,
            headers=headers,
            httpx_client=httpx_client
            if httpx_client is not None
            else httpx.AsyncClient(timeout=_defaulted_timeout, follow_redirects=follow_redirects)
            if follow_redirects is not None
            else httpx.AsyncClient(timeout=_defaulted_timeout),
            timeout=_defaulted_timeout,
        )
        self._workflows: typing.Optional[AsyncWorkflowsClient] = None
        self._workflow_revisions: typing.Optional[AsyncWorkflowRevisionsClient] = None
        self._revisions: typing.Optional[AsyncRevisionsClient] = None
        self._environments: typing.Optional[AsyncEnvironmentsClient] = None
        self._usage: typing.Optional[AsyncUsageClient] = None
        self._workflow_logs: typing.Optional[AsyncWorkflowLogsClient] = None
        self._copilots: typing.Optional[AsyncCopilotsClient] = None
        self._triggers: typing.Optional[AsyncTriggersClient] = None
        self._integrations: typing.Optional[AsyncIntegrationsClient] = None
        self._organizations: typing.Optional[AsyncOrganizationsClient] = None
        self._collections: typing.Optional[AsyncCollectionsClient] = None
        self._tables: typing.Optional[AsyncTablesClient] = None
        self._documents: typing.Optional[AsyncDocumentsClient] = None
        self._sources: typing.Optional[AsyncSourcesClient] = None
        self._syncs: typing.Optional[AsyncSyncsClient] = None

    @property
    def workflows(self):
        if self._workflows is None:
            from .workflows.client import AsyncWorkflowsClient  # noqa: E402

            self._workflows = AsyncWorkflowsClient(client_wrapper=self._client_wrapper)
        return self._workflows

    @property
    def workflow_revisions(self):
        if self._workflow_revisions is None:
            from .workflow_revisions.client import AsyncWorkflowRevisionsClient  # noqa: E402

            self._workflow_revisions = AsyncWorkflowRevisionsClient(client_wrapper=self._client_wrapper)
        return self._workflow_revisions

    @property
    def revisions(self):
        if self._revisions is None:
            from .revisions.client import AsyncRevisionsClient  # noqa: E402

            self._revisions = AsyncRevisionsClient(client_wrapper=self._client_wrapper)
        return self._revisions

    @property
    def environments(self):
        if self._environments is None:
            from .environments.client import AsyncEnvironmentsClient  # noqa: E402

            self._environments = AsyncEnvironmentsClient(client_wrapper=self._client_wrapper)
        return self._environments

    @property
    def usage(self):
        if self._usage is None:
            from .usage.client import AsyncUsageClient  # noqa: E402

            self._usage = AsyncUsageClient(client_wrapper=self._client_wrapper)
        return self._usage

    @property
    def workflow_logs(self):
        if self._workflow_logs is None:
            from .workflow_logs.client import AsyncWorkflowLogsClient  # noqa: E402

            self._workflow_logs = AsyncWorkflowLogsClient(client_wrapper=self._client_wrapper)
        return self._workflow_logs

    @property
    def copilots(self):
        if self._copilots is None:
            from .copilots.client import AsyncCopilotsClient  # noqa: E402

            self._copilots = AsyncCopilotsClient(client_wrapper=self._client_wrapper)
        return self._copilots

    @property
    def triggers(self):
        if self._triggers is None:
            from .triggers.client import AsyncTriggersClient  # noqa: E402

            self._triggers = AsyncTriggersClient(client_wrapper=self._client_wrapper)
        return self._triggers

    @property
    def integrations(self):
        if self._integrations is None:
            from .integrations.client import AsyncIntegrationsClient  # noqa: E402

            self._integrations = AsyncIntegrationsClient(client_wrapper=self._client_wrapper)
        return self._integrations

    @property
    def organizations(self):
        if self._organizations is None:
            from .organizations.client import AsyncOrganizationsClient  # noqa: E402

            self._organizations = AsyncOrganizationsClient(client_wrapper=self._client_wrapper)
        return self._organizations

    @property
    def collections(self):
        if self._collections is None:
            from .collections.client import AsyncCollectionsClient  # noqa: E402

            self._collections = AsyncCollectionsClient(client_wrapper=self._client_wrapper)
        return self._collections

    @property
    def tables(self):
        if self._tables is None:
            from .tables.client import AsyncTablesClient  # noqa: E402

            self._tables = AsyncTablesClient(client_wrapper=self._client_wrapper)
        return self._tables

    @property
    def documents(self):
        if self._documents is None:
            from .documents.client import AsyncDocumentsClient  # noqa: E402

            self._documents = AsyncDocumentsClient(client_wrapper=self._client_wrapper)
        return self._documents

    @property
    def sources(self):
        if self._sources is None:
            from .sources.client import AsyncSourcesClient  # noqa: E402

            self._sources = AsyncSourcesClient(client_wrapper=self._client_wrapper)
        return self._sources

    @property
    def syncs(self):
        if self._syncs is None:
            from .syncs.client import AsyncSyncsClient  # noqa: E402

            self._syncs = AsyncSyncsClient(client_wrapper=self._client_wrapper)
        return self._syncs


def _get_base_url(*, base_url: typing.Optional[str] = None, environment: ScoutEnvironment) -> str:
    if base_url is not None:
        return base_url
    elif environment is not None:
        return environment.value
    else:
        raise Exception("Please pass in either base_url or environment to construct the client")
