

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: blickfeld/flow/services/credentials.proto, blickfeld/flow/services/flow.proto, blickfeld/flow/services/settings.proto
# plugin: python-betterproto
import asyncio
from dataclasses import dataclass
from typing import (
    TYPE_CHECKING,
    AsyncIterator,
    Dict,
    Iterator,
    List,
    Optional,
)

import blickfeld_qb2.betterproto as betterproto
import blickfeld_qb2.betterproto.lib.google.protobuf as betterproto_lib_google_protobuf
import grpclib
from blickfeld_qb2.betterproto.grpc.grpclib_server import ServiceBase

from ...config import data as __config_data__
from .. import config as _config__


if TYPE_CHECKING:
    import grpclib.server
    from blickfeld_qb2.betterproto.grpc.grpclib_client import MetadataLike
    from grpclib.metadata import Deadline


@dataclass(eq=False, repr=False)
class CredentialsSetRequest(betterproto.Message):
    """Credentials set request"""

    credentials: "betterproto_lib_google_protobuf.Struct" = betterproto.message_field(1)
    """
    Credentials which should be stored   Generic JSON format which is defined
    by NodeRed.
    """


@dataclass(eq=False, repr=False)
class CredentialsGetResponse(betterproto.Message):
    """Credentails get response"""

    credentials: "betterproto_lib_google_protobuf.Struct" = betterproto.message_field(1)
    """
    Current credentials   Generic JSON format which is defined by NodeRed.
    """


@dataclass(eq=False, repr=False)
class FlowGetRequest(betterproto.Message):
    """Flow get request"""

    id: str = betterproto.string_field(1)
    """Flow id"""

    as_node_red_json: bool = betterproto.bool_field(2)
    """
    If true, the request returns the NodeRed JSON format which can be directly
    imported.
    """


@dataclass(eq=False, repr=False)
class FlowGetResponse(betterproto.Message):
    """Flow get response"""

    flow: "_config__.Flow" = betterproto.message_field(1, group="kind")
    """Blickfeld Flow config with well-defined high-level attributes"""

    node_red_json: "_config__.NodeRedJson" = betterproto.message_field(2, group="kind")
    """NodeRed JSON format which can be directly imported"""


@dataclass(eq=False, repr=False)
class FlowListResponse(betterproto.Message):
    """Flow list response"""

    ids: List[str] = betterproto.string_field(1)
    """List of Blickfeld Flow identifiers."""


@dataclass(eq=False, repr=False)
class FlowStoreRequest(betterproto.Message):
    """Flow store request"""

    id: str = betterproto.string_field(1)
    """
    ID of the flow   NOTE: For node_red_json, this has to match the ID in the
    NodeRED JSON format.
    """

    flow: "_config__.Flow" = betterproto.message_field(2, group="kind")
    """Blickfeld Flow config with well-defined high-level attributes"""

    node_red_json: "_config__.NodeRedJson" = betterproto.message_field(3, group="kind")
    """
    NodeRed JSON format which can be directly exported from NodeRED Admin UI
    """


@dataclass(eq=False, repr=False)
class FlowDeleteRequest(betterproto.Message):
    """Flow delete request"""

    id: str = betterproto.string_field(1)
    """Id of Blickfeld Flow"""


@dataclass(eq=False, repr=False)
class FlowWatchRequest(betterproto.Message):
    """Flow watch request"""

    as_node_red_json: bool = betterproto.bool_field(1)
    """
    If true, the request returns the NodeRed JSON format which can be directly
    imported.
    """


@dataclass(eq=False, repr=False)
class FlowWatchResponse(betterproto.Message):
    """Per change, a response is emitted"""

    id: str = betterproto.string_field(1)
    """Id of Blickfeld Flow"""

    flow: "_config__.Flow" = betterproto.message_field(2, group="kind")
    """Blickfeld Flow config with well-defined high-level attributes"""

    node_red_json: "_config__.NodeRedJson" = betterproto.message_field(3, group="kind")
    """
    NodeRed JSON format which can be directly exported from NodeRED Admin UI
    """

    type: "__config_data__.WatchEventType" = betterproto.enum_field(4)
    """Type of change"""


@dataclass(eq=False, repr=False)
class FlowStoreGlobalNodesRequest(betterproto.Message):
    """Flow store global nodes request"""

    node_red_json: "_config__.NodeRedJson" = betterproto.message_field(1)
    """NodeRed JSON format of all global configuration nodes"""


@dataclass(eq=False, repr=False)
class FlowGetGlobalNodesResponse(betterproto.Message):
    """Flow get global nodes response"""

    node_red_json: "_config__.NodeRedJson" = betterproto.message_field(1)
    """NodeRed JSON format of all global configuration nodes"""


@dataclass(eq=False, repr=False)
class FlowWatchGlobalNodesResponse(betterproto.Message):
    """Flow watch global nodes response"""

    node_red_json: "_config__.NodeRedJson" = betterproto.message_field(1)
    """NodeRed JSON format of all global configuration nodes"""


@dataclass(eq=False, repr=False)
class SettingsSetRequest(betterproto.Message):
    """Settings set request"""

    settings: "betterproto_lib_google_protobuf.Struct" = betterproto.message_field(1)
    """
    Settings which should be stored   Generic JSON format which is defined by
    NodeRed.
    """


@dataclass(eq=False, repr=False)
class SettingsGetResponse(betterproto.Message):
    """Settings get request"""

    settings: "betterproto_lib_google_protobuf.Struct" = betterproto.message_field(1)
    """Current settings   Generic JSON format which is defined by NodeRed."""


class Credentials(betterproto.ServiceStub):
    """
    Service to store the NodeRed credentials.   It is used to implement the
    custom storage API: https://nodered.org/docs/api/storage/
    """

    async def async_set(
        self,
        *,
        credentials: "betterproto_lib_google_protobuf.Struct" = None,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "betterproto_lib_google_protobuf.Empty":
        """
        Set credentials   Used by https://nodered.org/docs/api/storage/methods/
        #storagesavecredentialscredentials
        """

        request = CredentialsSetRequest()
        if credentials is not None:
            request.credentials = credentials

        return await self._unary_unary(
            "/blickfeld.flow.services.Credentials/Set",
            request,
            betterproto_lib_google_protobuf.Empty,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    def set(
        self,
        *,
        credentials: "betterproto_lib_google_protobuf.Struct" = None,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "betterproto_lib_google_protobuf.Empty":
        """
        Set credentials   Used by https://nodered.org/docs/api/storage/methods/
        #storagesavecredentialscredentials
        """

        loop = asyncio.get_event_loop()
        return loop.run_until_complete(
            self.async_set(
                credentials=credentials,
                timeout=timeout,
                deadline=deadline,
                metadata=metadata,
            )
        )

    async def async_get(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None,
    ) -> "CredentialsGetResponse":
        """
        Get credentials   Used by
        https://nodered.org/docs/api/storage/methods/#storagegetcredentials
        """

        request = betterproto_lib_google_protobuf.Empty()

        return await self._unary_unary(
            "/blickfeld.flow.services.Credentials/Get",
            request,
            CredentialsGetResponse,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    def get(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None,
    ) -> "CredentialsGetResponse":
        """
        Get credentials   Used by
        https://nodered.org/docs/api/storage/methods/#storagegetcredentials
        """

        loop = asyncio.get_event_loop()
        return loop.run_until_complete(
            self.async_get(
                timeout=timeout,
                deadline=deadline,
                metadata=metadata,
            )
        )


class Flow(betterproto.ServiceStub):
    """
    Service to store and control Blickfeld flows.   It is used to implement the
    custom storage API: https://nodered.org/docs/api/storage/
    """

    async def async_get(
        self,
        *,
        id: str = "",
        as_node_red_json: bool = False,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "FlowGetResponse":
        """Get a flow by id"""

        request = FlowGetRequest()
        request.id = id
        request.as_node_red_json = as_node_red_json

        return await self._unary_unary(
            "/blickfeld.flow.services.Flow/Get",
            request,
            FlowGetResponse,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    def get(
        self,
        *,
        id: str = "",
        as_node_red_json: bool = False,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "FlowGetResponse":
        """Get a flow by id"""

        loop = asyncio.get_event_loop()
        return loop.run_until_complete(
            self.async_get(
                id=id,
                as_node_red_json=as_node_red_json,
                timeout=timeout,
                deadline=deadline,
                metadata=metadata,
            )
        )

    async def async_list(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None,
    ) -> "FlowListResponse":
        """Return list of flow ids"""

        request = betterproto_lib_google_protobuf.Empty()

        return await self._unary_unary(
            "/blickfeld.flow.services.Flow/List",
            request,
            FlowListResponse,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    def list(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None,
    ) -> "FlowListResponse":
        """Return list of flow ids"""

        loop = asyncio.get_event_loop()
        return loop.run_until_complete(
            self.async_list(
                timeout=timeout,
                deadline=deadline,
                metadata=metadata,
            )
        )

    async def async_store(
        self,
        *,
        id: str = "",
        flow: "_config__.Flow" = None,
        node_red_json: "_config__.NodeRedJson" = None,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "betterproto_lib_google_protobuf.Empty":
        """Store flow   A "Reload" is required to apply the operation."""

        request = FlowStoreRequest()
        request.id = id
        if flow is not None:
            request.flow = flow
        if node_red_json is not None:
            request.node_red_json = node_red_json

        return await self._unary_unary(
            "/blickfeld.flow.services.Flow/Store",
            request,
            betterproto_lib_google_protobuf.Empty,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    def store(
        self,
        *,
        id: str = "",
        flow: "_config__.Flow" = None,
        node_red_json: "_config__.NodeRedJson" = None,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "betterproto_lib_google_protobuf.Empty":
        """Store flow   A "Reload" is required to apply the operation."""

        loop = asyncio.get_event_loop()
        return loop.run_until_complete(
            self.async_store(
                id=id,
                flow=flow,
                node_red_json=node_red_json,
                timeout=timeout,
                deadline=deadline,
                metadata=metadata,
            )
        )

    async def async_delete(
        self,
        *,
        id: str = "",
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "betterproto_lib_google_protobuf.Empty":
        """
        Delete stored flow   A "Reload" is required to apply the operation.
        """

        request = FlowDeleteRequest()
        request.id = id

        return await self._unary_unary(
            "/blickfeld.flow.services.Flow/Delete",
            request,
            betterproto_lib_google_protobuf.Empty,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    def delete(
        self,
        *,
        id: str = "",
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "betterproto_lib_google_protobuf.Empty":
        """
        Delete stored flow   A "Reload" is required to apply the operation.
        """

        loop = asyncio.get_event_loop()
        return loop.run_until_complete(
            self.async_delete(
                id=id,
                timeout=timeout,
                deadline=deadline,
                metadata=metadata,
            )
        )

    async def async_watch(
        self,
        *,
        as_node_red_json: bool = False,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> AsyncIterator["FlowWatchResponse"]:
        """Watch flow changes"""

        request = FlowWatchRequest()
        request.as_node_red_json = as_node_red_json

        async for response in self._unary_stream(
            "/blickfeld.flow.services.Flow/Watch",
            request,
            FlowWatchResponse,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        ):
            yield response

    def watch(
        self,
        *,
        as_node_red_json: bool = False,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> Iterator["FlowWatchResponse"]:
        """Watch flow changes"""

        loop = asyncio.get_event_loop()
        ait = self.async_watch(
            as_node_red_json=as_node_red_json,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        ).__aiter__()

        async def get_next():
            try:
                obj = await ait.__anext__()
                return False, obj
            except StopAsyncIteration:
                return True, None

        while True:
            done, obj = loop.run_until_complete(get_next())
            if done:
                break
            yield obj

    async def async_store_global_nodes(
        self,
        *,
        node_red_json: "_config__.NodeRedJson" = None,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "betterproto_lib_google_protobuf.Empty":
        """
        Store global nodes   A "Reload" is required to apply the operation.
        """

        request = FlowStoreGlobalNodesRequest()
        if node_red_json is not None:
            request.node_red_json = node_red_json

        return await self._unary_unary(
            "/blickfeld.flow.services.Flow/StoreGlobalNodes",
            request,
            betterproto_lib_google_protobuf.Empty,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    def store_global_nodes(
        self,
        *,
        node_red_json: "_config__.NodeRedJson" = None,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "betterproto_lib_google_protobuf.Empty":
        """
        Store global nodes   A "Reload" is required to apply the operation.
        """

        loop = asyncio.get_event_loop()
        return loop.run_until_complete(
            self.async_store_global_nodes(
                node_red_json=node_red_json,
                timeout=timeout,
                deadline=deadline,
                metadata=metadata,
            )
        )

    async def async_get_global_nodes(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None,
    ) -> "FlowGetGlobalNodesResponse":
        """Get global nodes"""

        request = betterproto_lib_google_protobuf.Empty()

        return await self._unary_unary(
            "/blickfeld.flow.services.Flow/GetGlobalNodes",
            request,
            FlowGetGlobalNodesResponse,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    def get_global_nodes(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None,
    ) -> "FlowGetGlobalNodesResponse":
        """Get global nodes"""

        loop = asyncio.get_event_loop()
        return loop.run_until_complete(
            self.async_get_global_nodes(
                timeout=timeout,
                deadline=deadline,
                metadata=metadata,
            )
        )

    async def async_watch_global_nodes(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None,
    ) -> "FlowWatchGlobalNodesResponse":
        """Watch global nodes"""

        request = betterproto_lib_google_protobuf.Empty()

        return await self._unary_unary(
            "/blickfeld.flow.services.Flow/WatchGlobalNodes",
            request,
            FlowWatchGlobalNodesResponse,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    def watch_global_nodes(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None,
    ) -> "FlowWatchGlobalNodesResponse":
        """Watch global nodes"""

        loop = asyncio.get_event_loop()
        return loop.run_until_complete(
            self.async_watch_global_nodes(
                timeout=timeout,
                deadline=deadline,
                metadata=metadata,
            )
        )

    async def async_reload(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None,
    ) -> "betterproto_lib_google_protobuf.Empty":
        """
        Reload node-red   This reloads all nodes from the config and restart
        all nodes.
        """

        request = betterproto_lib_google_protobuf.Empty()

        return await self._unary_unary(
            "/blickfeld.flow.services.Flow/Reload",
            request,
            betterproto_lib_google_protobuf.Empty,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    def reload(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None,
    ) -> "betterproto_lib_google_protobuf.Empty":
        """
        Reload node-red   This reloads all nodes from the config and restart
        all nodes.
        """

        loop = asyncio.get_event_loop()
        return loop.run_until_complete(
            self.async_reload(
                timeout=timeout,
                deadline=deadline,
                metadata=metadata,
            )
        )


class Settings(betterproto.ServiceStub):
    """
    Configuration service to store the NodeRed Settings.   It is used to
    implement the custom storage API: https://nodered.org/docs/api/storage/
    """

    async def async_set(
        self,
        *,
        settings: "betterproto_lib_google_protobuf.Struct" = None,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "betterproto_lib_google_protobuf.Empty":
        """
        Set settings   Used by https://nodered.org/docs/api/storage/methods/#st
        oragesavesettingssettings
        """

        request = SettingsSetRequest()
        if settings is not None:
            request.settings = settings

        return await self._unary_unary(
            "/blickfeld.flow.services.Settings/Set",
            request,
            betterproto_lib_google_protobuf.Empty,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    def set(
        self,
        *,
        settings: "betterproto_lib_google_protobuf.Struct" = None,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "betterproto_lib_google_protobuf.Empty":
        """
        Set settings   Used by https://nodered.org/docs/api/storage/methods/#st
        oragesavesettingssettings
        """

        loop = asyncio.get_event_loop()
        return loop.run_until_complete(
            self.async_set(
                settings=settings,
                timeout=timeout,
                deadline=deadline,
                metadata=metadata,
            )
        )

    async def async_get(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None,
    ) -> "SettingsGetResponse":
        """
        Get settings   Used by
        https://nodered.org/docs/api/storage/methods/#storagegetsettings
        """

        request = betterproto_lib_google_protobuf.Empty()

        return await self._unary_unary(
            "/blickfeld.flow.services.Settings/Get",
            request,
            SettingsGetResponse,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    def get(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None,
    ) -> "SettingsGetResponse":
        """
        Get settings   Used by
        https://nodered.org/docs/api/storage/methods/#storagegetsettings
        """

        loop = asyncio.get_event_loop()
        return loop.run_until_complete(
            self.async_get(
                timeout=timeout,
                deadline=deadline,
                metadata=metadata,
            )
        )
