#
# Copyright (c) 2021 Nitric Technologies Pty Ltd.
#
# This file is part of Nitric Python 3 SDK.
# See https://github.com/nitrictech/python-sdk for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: proto/websocket/v1/websocket.proto
# plugin: python-betterproto
from dataclasses import dataclass
from typing import (
    TYPE_CHECKING,
    Dict,
    Optional,
)

import betterproto
import grpclib
from betterproto.grpc.grpclib_server import ServiceBase


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


@dataclass(eq=False, repr=False)
class WebsocketSendRequest(betterproto.Message):
    socket: str = betterproto.string_field(1)
    """The nitric name of the socket to send on"""

    connection_id: str = betterproto.string_field(2)
    """The connection ID of the client to send to"""

    data: bytes = betterproto.bytes_field(3)
    """The data to send to the socket"""


@dataclass(eq=False, repr=False)
class WebsocketSendResponse(betterproto.Message):
    pass


@dataclass(eq=False, repr=False)
class WebsocketCloseRequest(betterproto.Message):
    socket: str = betterproto.string_field(1)
    """The nitric name of the socket to send on"""

    connection_id: str = betterproto.string_field(2)
    """The connection ID of the client to send to"""


@dataclass(eq=False, repr=False)
class WebsocketCloseResponse(betterproto.Message):
    pass


class WebsocketServiceStub(betterproto.ServiceStub):
    async def send(
        self,
        websocket_send_request: "WebsocketSendRequest",
        *,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "WebsocketSendResponse":
        return await self._unary_unary(
            "/nitric.websocket.v1.WebsocketService/Send",
            websocket_send_request,
            WebsocketSendResponse,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    async def close(
        self,
        websocket_close_request: "WebsocketCloseRequest",
        *,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> "WebsocketCloseResponse":
        return await self._unary_unary(
            "/nitric.websocket.v1.WebsocketService/Close",
            websocket_close_request,
            WebsocketCloseResponse,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )


class WebsocketServiceBase(ServiceBase):
    async def send(
        self, websocket_send_request: "WebsocketSendRequest"
    ) -> "WebsocketSendResponse":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def close(
        self, websocket_close_request: "WebsocketCloseRequest"
    ) -> "WebsocketCloseResponse":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def __rpc_send(
        self,
        stream: "grpclib.server.Stream[WebsocketSendRequest, WebsocketSendResponse]",
    ) -> None:
        request = await stream.recv_message()
        response = await self.send(request)
        await stream.send_message(response)

    async def __rpc_close(
        self,
        stream: "grpclib.server.Stream[WebsocketCloseRequest, WebsocketCloseResponse]",
    ) -> None:
        request = await stream.recv_message()
        response = await self.close(request)
        await stream.send_message(response)

    def __mapping__(self) -> Dict[str, grpclib.const.Handler]:
        return {
            "/nitric.websocket.v1.WebsocketService/Send": grpclib.const.Handler(
                self.__rpc_send,
                grpclib.const.Cardinality.UNARY_UNARY,
                WebsocketSendRequest,
                WebsocketSendResponse,
            ),
            "/nitric.websocket.v1.WebsocketService/Close": grpclib.const.Handler(
                self.__rpc_close,
                grpclib.const.Cardinality.UNARY_UNARY,
                WebsocketCloseRequest,
                WebsocketCloseResponse,
            ),
        }
