"""
BackendConnectionTag automatically generated by SDKgen please do not edit this file manually
https://sdkgen.app
"""

import requests
import sdkgen
from requests import RequestException
from typing import List
from typing import Dict
from typing import Any
from urllib.parse import parse_qs

from .backend_connection import BackendConnection
from .backend_connection_agent_tag import BackendConnectionAgentTag
from .backend_connection_collection import BackendConnectionCollection
from .backend_connection_create import BackendConnectionCreate
from .backend_connection_database_tag import BackendConnectionDatabaseTag
from .backend_connection_filesystem_tag import BackendConnectionFilesystemTag
from .backend_connection_http_tag import BackendConnectionHttpTag
from .backend_connection_index import BackendConnectionIndex
from .backend_connection_redirect_response import BackendConnectionRedirectResponse
from .backend_connection_sdk_tag import BackendConnectionSdkTag
from .backend_connection_update import BackendConnectionUpdate
from .common_form_container import CommonFormContainer
from .common_message import CommonMessage
from .common_message_exception import CommonMessageException

class BackendConnectionTag(sdkgen.TagAbstract):
    def __init__(self, http_client: requests.Session, parser: sdkgen.Parser):
        super().__init__(http_client, parser)

    def agent(self) -> BackendConnectionAgentTag:
        return BackendConnectionAgentTag(
            self.http_client,
            self.parser
        )

    def database(self) -> BackendConnectionDatabaseTag:
        return BackendConnectionDatabaseTag(
            self.http_client,
            self.parser
        )

    def filesystem(self) -> BackendConnectionFilesystemTag:
        return BackendConnectionFilesystemTag(
            self.http_client,
            self.parser
        )

    def http(self) -> BackendConnectionHttpTag:
        return BackendConnectionHttpTag(
            self.http_client,
            self.parser
        )

    def sdk(self) -> BackendConnectionSdkTag:
        return BackendConnectionSdkTag(
            self.http_client,
            self.parser
        )


    def create(self, payload: BackendConnectionCreate) -> CommonMessage:
        """
        Creates a new connection
        """
        try:
            path_params = {}

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)

            options['json'] = payload.model_dump(by_alias=True)

            options['headers']['Content-Type'] = 'application/json'

            response = self.http_client.request('POST', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = CommonMessage.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def delete(self, connection_id: str) -> CommonMessage:
        """
        Deletes an existing connection
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection/$connection_id<[0-9]+|^~>', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)



            response = self.http_client.request('DELETE', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = CommonMessage.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def get(self, connection_id: str) -> BackendConnection:
        """
        Returns a specific connection
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection/$connection_id<[0-9]+|^~>', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)



            response = self.http_client.request('GET', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = BackendConnection.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def get_all(self, start_index: int, count: int, search: str, class_: str) -> BackendConnectionCollection:
        """
        Returns a paginated list of connections
        """
        try:
            path_params = {}

            query_params = {}
            query_params['startIndex'] = start_index
            query_params['count'] = count
            query_params['search'] = search
            query_params['class'] = class_

            query_struct_names = []

            url = self.parser.url('/backend/connection', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)



            response = self.http_client.request('GET', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = BackendConnectionCollection.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def get_classes(self) -> BackendConnectionIndex:
        """
        Returns all available connection classes
        """
        try:
            path_params = {}

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection/list', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)



            response = self.http_client.request('GET', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = BackendConnectionIndex.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def get_form(self, class_: str) -> CommonFormContainer:
        """
        Returns the connection config form
        """
        try:
            path_params = {}

            query_params = {}
            query_params['class'] = class_

            query_struct_names = []

            url = self.parser.url('/backend/connection/form', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)



            response = self.http_client.request('GET', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = CommonFormContainer.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def get_redirect(self, connection_id: str) -> BackendConnectionRedirectResponse:
        """
        Returns a redirect url to start the OAuth2 authorization flow for the given connection
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection/:connection_id/redirect', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)



            response = self.http_client.request('GET', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = BackendConnectionRedirectResponse.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def update(self, connection_id: str, payload: BackendConnectionUpdate) -> CommonMessage:
        """
        Updates an existing connection
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection/$connection_id<[0-9]+|^~>', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)

            options['json'] = payload.model_dump(by_alias=True)

            options['headers']['Content-Type'] = 'application/json'

            response = self.http_client.request('PUT', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = CommonMessage.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))



