2020-07-04 22:51:10 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-07-18 18:18:43 +08:00
|
|
|
import abc
|
2020-07-04 22:51:10 +08:00
|
|
|
from typing import Optional
|
|
|
|
from ipaddress import IPv4Address
|
|
|
|
|
2020-07-11 17:32:03 +08:00
|
|
|
from nonebot.config import Config
|
|
|
|
|
2020-07-04 22:51:10 +08:00
|
|
|
|
2020-07-18 18:18:43 +08:00
|
|
|
class BaseDriver(abc.ABC):
|
2020-07-04 22:51:10 +08:00
|
|
|
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-07-11 17:32:03 +08:00
|
|
|
def __init__(self, config: Config):
|
2020-07-05 20:39:34 +08:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2020-07-04 22:51:10 +08:00
|
|
|
@property
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-07-04 22:51:10 +08:00
|
|
|
def server_app(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@property
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-07-04 22:51:10 +08:00
|
|
|
def asgi(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@property
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-07-04 22:51:10 +08:00
|
|
|
def logger(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-07-04 22:51:10 +08:00
|
|
|
def run(self,
|
|
|
|
host: Optional[IPv4Address] = None,
|
|
|
|
port: Optional[int] = None,
|
|
|
|
*args,
|
|
|
|
**kwargs):
|
|
|
|
raise NotImplementedError
|
2020-07-05 20:39:34 +08:00
|
|
|
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-07-05 20:39:34 +08:00
|
|
|
async def _handle_http(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-07-05 20:39:34 +08:00
|
|
|
async def _handle_ws_reverse(self):
|
|
|
|
raise NotImplementedError
|
2020-07-11 17:32:03 +08:00
|
|
|
|
2020-07-15 20:39:59 +08:00
|
|
|
|
|
|
|
class BaseWebSocket(object):
|
|
|
|
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-07-15 20:39:59 +08:00
|
|
|
def __init__(self, websocket):
|
|
|
|
self._websocket = websocket
|
|
|
|
|
|
|
|
@property
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-07-15 20:39:59 +08:00
|
|
|
def websocket(self):
|
|
|
|
return self._websocket
|
|
|
|
|
|
|
|
@property
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-07-15 20:39:59 +08:00
|
|
|
def closed(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-07-15 20:39:59 +08:00
|
|
|
async def accept(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-08-01 22:03:40 +08:00
|
|
|
async def close(self, code: int):
|
2020-07-15 20:39:59 +08:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-07-15 20:39:59 +08:00
|
|
|
async def receive(self) -> dict:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2020-07-18 18:18:43 +08:00
|
|
|
@abc.abstractmethod
|
2020-07-15 20:39:59 +08:00
|
|
|
async def send(self, data: dict):
|
|
|
|
raise NotImplementedError
|