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

import datetime as dt
import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.pydantic_utilities import pydantic_v1
from ..core.request_options import RequestOptions
from ..types.model_provider_connection import ModelProviderConnection
from ..types.model_provider_connection_created_by import ModelProviderConnectionCreatedBy
from ..types.model_provider_connection_organization import ModelProviderConnectionOrganization
from ..types.model_provider_connection_provider import ModelProviderConnectionProvider
from ..types.model_provider_connection_scope import ModelProviderConnectionScope

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class ModelProvidersClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def create(
        self,
        *,
        provider: ModelProviderConnectionProvider,
        api_key: typing.Optional[str] = OMIT,
        deployment_name: typing.Optional[str] = OMIT,
        endpoint: typing.Optional[str] = OMIT,
        scope: typing.Optional[ModelProviderConnectionScope] = OMIT,
        organization: typing.Optional[ModelProviderConnectionOrganization] = OMIT,
        created_by: typing.Optional[ModelProviderConnectionCreatedBy] = OMIT,
        created_at: typing.Optional[dt.datetime] = OMIT,
        updated_at: typing.Optional[dt.datetime] = OMIT,
        request_options: typing.Optional[RequestOptions] = None
    ) -> ModelProviderConnection:
        """
        Create a new model provider connection.

        Parameters
        ----------
        provider : ModelProviderConnectionProvider

        api_key : typing.Optional[str]

        deployment_name : typing.Optional[str]

        endpoint : typing.Optional[str]

        scope : typing.Optional[ModelProviderConnectionScope]

        organization : typing.Optional[ModelProviderConnectionOrganization]

        created_by : typing.Optional[ModelProviderConnectionCreatedBy]

        created_at : typing.Optional[dt.datetime]

        updated_at : typing.Optional[dt.datetime]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ModelProviderConnection


        Examples
        --------
        from label_studio_sdk.client import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.model_providers.create(
            provider="OpenAI",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/model-provider-connections/",
            method="POST",
            json={
                "provider": provider,
                "api_key": api_key,
                "deployment_name": deployment_name,
                "endpoint": endpoint,
                "scope": scope,
                "organization": organization,
                "created_by": created_by,
                "created_at": created_at,
                "updated_at": updated_at,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return pydantic_v1.parse_obj_as(ModelProviderConnection, _response.json())  # type: ignore
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


class AsyncModelProvidersClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def create(
        self,
        *,
        provider: ModelProviderConnectionProvider,
        api_key: typing.Optional[str] = OMIT,
        deployment_name: typing.Optional[str] = OMIT,
        endpoint: typing.Optional[str] = OMIT,
        scope: typing.Optional[ModelProviderConnectionScope] = OMIT,
        organization: typing.Optional[ModelProviderConnectionOrganization] = OMIT,
        created_by: typing.Optional[ModelProviderConnectionCreatedBy] = OMIT,
        created_at: typing.Optional[dt.datetime] = OMIT,
        updated_at: typing.Optional[dt.datetime] = OMIT,
        request_options: typing.Optional[RequestOptions] = None
    ) -> ModelProviderConnection:
        """
        Create a new model provider connection.

        Parameters
        ----------
        provider : ModelProviderConnectionProvider

        api_key : typing.Optional[str]

        deployment_name : typing.Optional[str]

        endpoint : typing.Optional[str]

        scope : typing.Optional[ModelProviderConnectionScope]

        organization : typing.Optional[ModelProviderConnectionOrganization]

        created_by : typing.Optional[ModelProviderConnectionCreatedBy]

        created_at : typing.Optional[dt.datetime]

        updated_at : typing.Optional[dt.datetime]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ModelProviderConnection


        Examples
        --------
        from label_studio_sdk.client import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )
        await client.model_providers.create(
            provider="OpenAI",
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/model-provider-connections/",
            method="POST",
            json={
                "provider": provider,
                "api_key": api_key,
                "deployment_name": deployment_name,
                "endpoint": endpoint,
                "scope": scope,
                "organization": organization,
                "created_by": created_by,
                "created_at": created_at,
                "updated_at": updated_at,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return pydantic_v1.parse_obj_as(ModelProviderConnection, _response.json())  # type: ignore
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
