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

from __future__ import annotations

import os
import typing

import httpx
from .core.api_error import ApiError
from .core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from .environment import LabelStudioEnvironment

if typing.TYPE_CHECKING:
    from .actions.client import ActionsClient, AsyncActionsClient
    from .activity_logs.client import ActivityLogsClient, AsyncActivityLogsClient
    from .annotation_history.client import AnnotationHistoryClient, AsyncAnnotationHistoryClient
    from .annotation_reviews.client import AnnotationReviewsClient, AsyncAnnotationReviewsClient
    from .annotations.client import AnnotationsClient, AsyncAnnotationsClient
    from .billing.client import AsyncBillingClient, BillingClient
    from .comments.client import AsyncCommentsClient, CommentsClient
    from .export_storage.client import AsyncExportStorageClient, ExportStorageClient
    from .files.client import AsyncFilesClient, FilesClient
    from .import_storage.client import AsyncImportStorageClient, ImportStorageClient
    from .jwt_settings.client import AsyncJwtSettingsClient, JwtSettingsClient
    from .ml.client import AsyncMlClient, MlClient
    from .model_providers.client import AsyncModelProvidersClient, ModelProvidersClient
    from .organizations.client import AsyncOrganizationsClient, OrganizationsClient
    from .predictions.client import AsyncPredictionsClient, PredictionsClient
    from .project_templates.client import AsyncProjectTemplatesClient, ProjectTemplatesClient
    from .projects.client import AsyncProjectsClient, ProjectsClient
    from .prompts.client import AsyncPromptsClient, PromptsClient
    from .session_policy.client import AsyncSessionPolicyClient, SessionPolicyClient
    from .sso.client import AsyncSsoClient, SsoClient
    from .tasks.client import AsyncTasksClient, TasksClient
    from .tokens.client import AsyncTokensClient, TokensClient
    from .users.client import AsyncUsersClient, UsersClient
    from .versions.client import AsyncVersionsClient, VersionsClient
    from .views.client import AsyncViewsClient, ViewsClient
    from .webhooks.client import AsyncWebhooksClient, WebhooksClient
    from .workspaces.client import AsyncWorkspacesClient, WorkspacesClient


class LabelStudioBase:
    """
    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 : LabelStudioEnvironment
        The environment to use for requests from the client. from .environment import LabelStudioEnvironment



        Defaults to LabelStudioEnvironment.DEFAULT



    api_key : typing.Optional[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 label_studio_sdk import LabelStudio

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

    def __init__(
        self,
        *,
        base_url: typing.Optional[str] = None,
        environment: LabelStudioEnvironment = LabelStudioEnvironment.DEFAULT,
        api_key: typing.Optional[str] = os.getenv("LABEL_STUDIO_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
        )
        if api_key is None:
            raise ApiError(
                body="The client must be instantiated be either passing in api_key or setting LABEL_STUDIO_API_KEY"
            )
        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._activity_logs: typing.Optional[ActivityLogsClient] = None
        self._annotation_history: typing.Optional[AnnotationHistoryClient] = None
        self._annotation_reviews: typing.Optional[AnnotationReviewsClient] = None
        self._annotations: typing.Optional[AnnotationsClient] = None
        self._billing: typing.Optional[BillingClient] = None
        self._comments: typing.Optional[CommentsClient] = None
        self._users: typing.Optional[UsersClient] = None
        self._actions: typing.Optional[ActionsClient] = None
        self._views: typing.Optional[ViewsClient] = None
        self._organizations: typing.Optional[OrganizationsClient] = None
        self._files: typing.Optional[FilesClient] = None
        self._jwt_settings: typing.Optional[JwtSettingsClient] = None
        self._ml: typing.Optional[MlClient] = None
        self._model_providers: typing.Optional[ModelProvidersClient] = None
        self._prompts: typing.Optional[PromptsClient] = None
        self._predictions: typing.Optional[PredictionsClient] = None
        self._project_templates: typing.Optional[ProjectTemplatesClient] = None
        self._projects: typing.Optional[ProjectsClient] = None
        self._tasks: typing.Optional[TasksClient] = None
        self._session_policy: typing.Optional[SessionPolicyClient] = None
        self._import_storage: typing.Optional[ImportStorageClient] = None
        self._export_storage: typing.Optional[ExportStorageClient] = None
        self._tokens: typing.Optional[TokensClient] = None
        self._versions: typing.Optional[VersionsClient] = None
        self._webhooks: typing.Optional[WebhooksClient] = None
        self._workspaces: typing.Optional[WorkspacesClient] = None
        self._sso: typing.Optional[SsoClient] = None

    @property
    def activity_logs(self):
        if self._activity_logs is None:
            from .activity_logs.client import ActivityLogsClient  # noqa: E402

            self._activity_logs = ActivityLogsClient(client_wrapper=self._client_wrapper)
        return self._activity_logs

    @property
    def annotation_history(self):
        if self._annotation_history is None:
            from .annotation_history.client import AnnotationHistoryClient  # noqa: E402

            self._annotation_history = AnnotationHistoryClient(client_wrapper=self._client_wrapper)
        return self._annotation_history

    @property
    def annotation_reviews(self):
        if self._annotation_reviews is None:
            from .annotation_reviews.client import AnnotationReviewsClient  # noqa: E402

            self._annotation_reviews = AnnotationReviewsClient(client_wrapper=self._client_wrapper)
        return self._annotation_reviews

    @property
    def annotations(self):
        if self._annotations is None:
            from .annotations.client import AnnotationsClient  # noqa: E402

            self._annotations = AnnotationsClient(client_wrapper=self._client_wrapper)
        return self._annotations

    @property
    def billing(self):
        if self._billing is None:
            from .billing.client import BillingClient  # noqa: E402

            self._billing = BillingClient(client_wrapper=self._client_wrapper)
        return self._billing

    @property
    def comments(self):
        if self._comments is None:
            from .comments.client import CommentsClient  # noqa: E402

            self._comments = CommentsClient(client_wrapper=self._client_wrapper)
        return self._comments

    @property
    def users(self):
        if self._users is None:
            from .users.client import UsersClient  # noqa: E402

            self._users = UsersClient(client_wrapper=self._client_wrapper)
        return self._users

    @property
    def actions(self):
        if self._actions is None:
            from .actions.client import ActionsClient  # noqa: E402

            self._actions = ActionsClient(client_wrapper=self._client_wrapper)
        return self._actions

    @property
    def views(self):
        if self._views is None:
            from .views.client import ViewsClient  # noqa: E402

            self._views = ViewsClient(client_wrapper=self._client_wrapper)
        return self._views

    @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 files(self):
        if self._files is None:
            from .files.client import FilesClient  # noqa: E402

            self._files = FilesClient(client_wrapper=self._client_wrapper)
        return self._files

    @property
    def jwt_settings(self):
        if self._jwt_settings is None:
            from .jwt_settings.client import JwtSettingsClient  # noqa: E402

            self._jwt_settings = JwtSettingsClient(client_wrapper=self._client_wrapper)
        return self._jwt_settings

    @property
    def ml(self):
        if self._ml is None:
            from .ml.client import MlClient  # noqa: E402

            self._ml = MlClient(client_wrapper=self._client_wrapper)
        return self._ml

    @property
    def model_providers(self):
        if self._model_providers is None:
            from .model_providers.client import ModelProvidersClient  # noqa: E402

            self._model_providers = ModelProvidersClient(client_wrapper=self._client_wrapper)
        return self._model_providers

    @property
    def prompts(self):
        if self._prompts is None:
            from .prompts.client import PromptsClient  # noqa: E402

            self._prompts = PromptsClient(client_wrapper=self._client_wrapper)
        return self._prompts

    @property
    def predictions(self):
        if self._predictions is None:
            from .predictions.client import PredictionsClient  # noqa: E402

            self._predictions = PredictionsClient(client_wrapper=self._client_wrapper)
        return self._predictions

    @property
    def project_templates(self):
        if self._project_templates is None:
            from .project_templates.client import ProjectTemplatesClient  # noqa: E402

            self._project_templates = ProjectTemplatesClient(client_wrapper=self._client_wrapper)
        return self._project_templates

    @property
    def projects(self):
        if self._projects is None:
            from .projects.client import ProjectsClient  # noqa: E402

            self._projects = ProjectsClient(client_wrapper=self._client_wrapper)
        return self._projects

    @property
    def tasks(self):
        if self._tasks is None:
            from .tasks.client import TasksClient  # noqa: E402

            self._tasks = TasksClient(client_wrapper=self._client_wrapper)
        return self._tasks

    @property
    def session_policy(self):
        if self._session_policy is None:
            from .session_policy.client import SessionPolicyClient  # noqa: E402

            self._session_policy = SessionPolicyClient(client_wrapper=self._client_wrapper)
        return self._session_policy

    @property
    def import_storage(self):
        if self._import_storage is None:
            from .import_storage.client import ImportStorageClient  # noqa: E402

            self._import_storage = ImportStorageClient(client_wrapper=self._client_wrapper)
        return self._import_storage

    @property
    def export_storage(self):
        if self._export_storage is None:
            from .export_storage.client import ExportStorageClient  # noqa: E402

            self._export_storage = ExportStorageClient(client_wrapper=self._client_wrapper)
        return self._export_storage

    @property
    def tokens(self):
        if self._tokens is None:
            from .tokens.client import TokensClient  # noqa: E402

            self._tokens = TokensClient(client_wrapper=self._client_wrapper)
        return self._tokens

    @property
    def versions(self):
        if self._versions is None:
            from .versions.client import VersionsClient  # noqa: E402

            self._versions = VersionsClient(client_wrapper=self._client_wrapper)
        return self._versions

    @property
    def webhooks(self):
        if self._webhooks is None:
            from .webhooks.client import WebhooksClient  # noqa: E402

            self._webhooks = WebhooksClient(client_wrapper=self._client_wrapper)
        return self._webhooks

    @property
    def workspaces(self):
        if self._workspaces is None:
            from .workspaces.client import WorkspacesClient  # noqa: E402

            self._workspaces = WorkspacesClient(client_wrapper=self._client_wrapper)
        return self._workspaces

    @property
    def sso(self):
        if self._sso is None:
            from .sso.client import SsoClient  # noqa: E402

            self._sso = SsoClient(client_wrapper=self._client_wrapper)
        return self._sso


class AsyncLabelStudioBase:
    """
    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 : LabelStudioEnvironment
        The environment to use for requests from the client. from .environment import LabelStudioEnvironment



        Defaults to LabelStudioEnvironment.DEFAULT



    api_key : typing.Optional[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 label_studio_sdk import AsyncLabelStudio

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

    def __init__(
        self,
        *,
        base_url: typing.Optional[str] = None,
        environment: LabelStudioEnvironment = LabelStudioEnvironment.DEFAULT,
        api_key: typing.Optional[str] = os.getenv("LABEL_STUDIO_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
        )
        if api_key is None:
            raise ApiError(
                body="The client must be instantiated be either passing in api_key or setting LABEL_STUDIO_API_KEY"
            )
        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._activity_logs: typing.Optional[AsyncActivityLogsClient] = None
        self._annotation_history: typing.Optional[AsyncAnnotationHistoryClient] = None
        self._annotation_reviews: typing.Optional[AsyncAnnotationReviewsClient] = None
        self._annotations: typing.Optional[AsyncAnnotationsClient] = None
        self._billing: typing.Optional[AsyncBillingClient] = None
        self._comments: typing.Optional[AsyncCommentsClient] = None
        self._users: typing.Optional[AsyncUsersClient] = None
        self._actions: typing.Optional[AsyncActionsClient] = None
        self._views: typing.Optional[AsyncViewsClient] = None
        self._organizations: typing.Optional[AsyncOrganizationsClient] = None
        self._files: typing.Optional[AsyncFilesClient] = None
        self._jwt_settings: typing.Optional[AsyncJwtSettingsClient] = None
        self._ml: typing.Optional[AsyncMlClient] = None
        self._model_providers: typing.Optional[AsyncModelProvidersClient] = None
        self._prompts: typing.Optional[AsyncPromptsClient] = None
        self._predictions: typing.Optional[AsyncPredictionsClient] = None
        self._project_templates: typing.Optional[AsyncProjectTemplatesClient] = None
        self._projects: typing.Optional[AsyncProjectsClient] = None
        self._tasks: typing.Optional[AsyncTasksClient] = None
        self._session_policy: typing.Optional[AsyncSessionPolicyClient] = None
        self._import_storage: typing.Optional[AsyncImportStorageClient] = None
        self._export_storage: typing.Optional[AsyncExportStorageClient] = None
        self._tokens: typing.Optional[AsyncTokensClient] = None
        self._versions: typing.Optional[AsyncVersionsClient] = None
        self._webhooks: typing.Optional[AsyncWebhooksClient] = None
        self._workspaces: typing.Optional[AsyncWorkspacesClient] = None
        self._sso: typing.Optional[AsyncSsoClient] = None

    @property
    def activity_logs(self):
        if self._activity_logs is None:
            from .activity_logs.client import AsyncActivityLogsClient  # noqa: E402

            self._activity_logs = AsyncActivityLogsClient(client_wrapper=self._client_wrapper)
        return self._activity_logs

    @property
    def annotation_history(self):
        if self._annotation_history is None:
            from .annotation_history.client import AsyncAnnotationHistoryClient  # noqa: E402

            self._annotation_history = AsyncAnnotationHistoryClient(client_wrapper=self._client_wrapper)
        return self._annotation_history

    @property
    def annotation_reviews(self):
        if self._annotation_reviews is None:
            from .annotation_reviews.client import AsyncAnnotationReviewsClient  # noqa: E402

            self._annotation_reviews = AsyncAnnotationReviewsClient(client_wrapper=self._client_wrapper)
        return self._annotation_reviews

    @property
    def annotations(self):
        if self._annotations is None:
            from .annotations.client import AsyncAnnotationsClient  # noqa: E402

            self._annotations = AsyncAnnotationsClient(client_wrapper=self._client_wrapper)
        return self._annotations

    @property
    def billing(self):
        if self._billing is None:
            from .billing.client import AsyncBillingClient  # noqa: E402

            self._billing = AsyncBillingClient(client_wrapper=self._client_wrapper)
        return self._billing

    @property
    def comments(self):
        if self._comments is None:
            from .comments.client import AsyncCommentsClient  # noqa: E402

            self._comments = AsyncCommentsClient(client_wrapper=self._client_wrapper)
        return self._comments

    @property
    def users(self):
        if self._users is None:
            from .users.client import AsyncUsersClient  # noqa: E402

            self._users = AsyncUsersClient(client_wrapper=self._client_wrapper)
        return self._users

    @property
    def actions(self):
        if self._actions is None:
            from .actions.client import AsyncActionsClient  # noqa: E402

            self._actions = AsyncActionsClient(client_wrapper=self._client_wrapper)
        return self._actions

    @property
    def views(self):
        if self._views is None:
            from .views.client import AsyncViewsClient  # noqa: E402

            self._views = AsyncViewsClient(client_wrapper=self._client_wrapper)
        return self._views

    @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 files(self):
        if self._files is None:
            from .files.client import AsyncFilesClient  # noqa: E402

            self._files = AsyncFilesClient(client_wrapper=self._client_wrapper)
        return self._files

    @property
    def jwt_settings(self):
        if self._jwt_settings is None:
            from .jwt_settings.client import AsyncJwtSettingsClient  # noqa: E402

            self._jwt_settings = AsyncJwtSettingsClient(client_wrapper=self._client_wrapper)
        return self._jwt_settings

    @property
    def ml(self):
        if self._ml is None:
            from .ml.client import AsyncMlClient  # noqa: E402

            self._ml = AsyncMlClient(client_wrapper=self._client_wrapper)
        return self._ml

    @property
    def model_providers(self):
        if self._model_providers is None:
            from .model_providers.client import AsyncModelProvidersClient  # noqa: E402

            self._model_providers = AsyncModelProvidersClient(client_wrapper=self._client_wrapper)
        return self._model_providers

    @property
    def prompts(self):
        if self._prompts is None:
            from .prompts.client import AsyncPromptsClient  # noqa: E402

            self._prompts = AsyncPromptsClient(client_wrapper=self._client_wrapper)
        return self._prompts

    @property
    def predictions(self):
        if self._predictions is None:
            from .predictions.client import AsyncPredictionsClient  # noqa: E402

            self._predictions = AsyncPredictionsClient(client_wrapper=self._client_wrapper)
        return self._predictions

    @property
    def project_templates(self):
        if self._project_templates is None:
            from .project_templates.client import AsyncProjectTemplatesClient  # noqa: E402

            self._project_templates = AsyncProjectTemplatesClient(client_wrapper=self._client_wrapper)
        return self._project_templates

    @property
    def projects(self):
        if self._projects is None:
            from .projects.client import AsyncProjectsClient  # noqa: E402

            self._projects = AsyncProjectsClient(client_wrapper=self._client_wrapper)
        return self._projects

    @property
    def tasks(self):
        if self._tasks is None:
            from .tasks.client import AsyncTasksClient  # noqa: E402

            self._tasks = AsyncTasksClient(client_wrapper=self._client_wrapper)
        return self._tasks

    @property
    def session_policy(self):
        if self._session_policy is None:
            from .session_policy.client import AsyncSessionPolicyClient  # noqa: E402

            self._session_policy = AsyncSessionPolicyClient(client_wrapper=self._client_wrapper)
        return self._session_policy

    @property
    def import_storage(self):
        if self._import_storage is None:
            from .import_storage.client import AsyncImportStorageClient  # noqa: E402

            self._import_storage = AsyncImportStorageClient(client_wrapper=self._client_wrapper)
        return self._import_storage

    @property
    def export_storage(self):
        if self._export_storage is None:
            from .export_storage.client import AsyncExportStorageClient  # noqa: E402

            self._export_storage = AsyncExportStorageClient(client_wrapper=self._client_wrapper)
        return self._export_storage

    @property
    def tokens(self):
        if self._tokens is None:
            from .tokens.client import AsyncTokensClient  # noqa: E402

            self._tokens = AsyncTokensClient(client_wrapper=self._client_wrapper)
        return self._tokens

    @property
    def versions(self):
        if self._versions is None:
            from .versions.client import AsyncVersionsClient  # noqa: E402

            self._versions = AsyncVersionsClient(client_wrapper=self._client_wrapper)
        return self._versions

    @property
    def webhooks(self):
        if self._webhooks is None:
            from .webhooks.client import AsyncWebhooksClient  # noqa: E402

            self._webhooks = AsyncWebhooksClient(client_wrapper=self._client_wrapper)
        return self._webhooks

    @property
    def workspaces(self):
        if self._workspaces is None:
            from .workspaces.client import AsyncWorkspacesClient  # noqa: E402

            self._workspaces = AsyncWorkspacesClient(client_wrapper=self._client_wrapper)
        return self._workspaces

    @property
    def sso(self):
        if self._sso is None:
            from .sso.client import AsyncSsoClient  # noqa: E402

            self._sso = AsyncSsoClient(client_wrapper=self._client_wrapper)
        return self._sso


def _get_base_url(*, base_url: typing.Optional[str] = None, environment: LabelStudioEnvironment) -> 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")
