Skip to content

Commit 719168a

Browse files
committed
feat!: impelement routers
1 parent 9e4007d commit 719168a

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

interactions_restful/abc.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
from abc import ABC, abstractmethod
22
from typing import Callable, Coroutine
33

4-
__all__ = ("BaseApi", )
4+
__all__ = ("BaseApi", "BaseRouter")
5+
6+
7+
class BaseRouter(ABC):
8+
@abstractmethod
9+
def add_endpoint_method(self, coro: Callable[..., Coroutine], endpoint: str, method: str, **kwargs):
10+
pass
511

612

713
class BaseApi(ABC):
@@ -10,9 +16,17 @@ def __init__(self, host: str, port: int, **kwargs):
1016
pass
1117

1218
@abstractmethod
13-
def add_route(self, coro: Callable[..., Coroutine], endpoint: str, method: str, **kwargs):
19+
def add_endpoint_method(self, coro: Callable[..., Coroutine], endpoint: str, method: str, **kwargs):
20+
pass
21+
22+
@staticmethod
23+
@abstractmethod
24+
def create_router(**kwargs) -> BaseRouter:
25+
pass
26+
27+
def add_router(self, router: BaseRouter):
1428
pass
1529

1630
@abstractmethod
1731
async def run(self):
18-
pass
32+
pass
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
from typing import Callable, Coroutine
22

3-
from fastapi import FastAPI as _FastAPI
3+
from fastapi import FastAPI as _FastAPI, APIRouter
44
from uvicorn import Config, Server
55

6-
from ..abc import BaseApi
6+
from ..abc import BaseApi, BaseRouter
77

88
__all__ = ("FastAPI", )
99

1010

11+
class FastApiRouter(BaseRouter):
12+
def __init__(self, **kwargs):
13+
self.api_router = APIRouter(**kwargs)
14+
15+
def add_endpoint_method(self, coro: Callable[..., Coroutine], endpoint: str, method: str, **kwargs):
16+
self.api_router.add_api_route(
17+
endpoint,
18+
coro,
19+
methods=[method],
20+
**kwargs
21+
)
22+
23+
1124
class FastAPI(BaseApi):
1225
def __init__(self, host: str, port: int, **kwargs):
1326
self.host = host
@@ -16,14 +29,21 @@ def __init__(self, host: str, port: int, **kwargs):
1629
self.app = _FastAPI(**kwargs)
1730
self._config = Config(self.app, host=self.host, port=self.port)
1831

19-
def add_route(self, coro: Callable[..., Coroutine], endpoint: str, method: str, **kwargs):
32+
def add_endpoint_method(self, coro: Callable[..., Coroutine], endpoint: str, method: str, **kwargs):
2033
self.app.router.add_api_route(
2134
endpoint,
2235
coro,
2336
methods=[method],
2437
**kwargs
2538
)
2639

40+
@staticmethod
41+
def create_router(**kwargs) -> BaseRouter:
42+
return FastApiRouter(**kwargs)
43+
44+
def add_router(self, router: FastApiRouter):
45+
self.app.include_router(router.api_router)
46+
2747
async def run(self):
2848
server = Server(config=self._config)
2949
await server.serve()

interactions_restful/backends/flask_api.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44
from asgiref.wsgi import WsgiToAsgi
55
from hypercorn.config import Config
66
from hypercorn.asyncio import serve
7-
from flask import Flask
7+
from flask import Flask, Blueprint
88
except ImportError as e:
99
raise ImportError("Flask dependencies weren't installed. Please, install they with [flask] option") from e
1010

11-
from ..abc import BaseApi
11+
from ..abc import BaseApi, BaseRouter
1212

13-
__all__ = ("FlaskAPI", )
13+
__all__ = ("FlaskAPI", "FlaskRouter")
14+
15+
16+
class FlaskRouter(BaseRouter):
17+
def __init__(self, name: str, **kwargs):
18+
self.blueprint = Blueprint(name.lower(), name, **kwargs)
19+
20+
def add_endpoint_method(self, coro: Callable[..., Coroutine], endpoint: str, method: str, **kwargs):
21+
self.blueprint.route(endpoint, methods=[method], **kwargs)(coro)
1422

1523

1624
class FlaskAPI(BaseApi):
@@ -22,8 +30,15 @@ def __init__(self, host: str, port: int, **kwargs):
2230
self._config = Config()
2331
self._config.bind = [f"{host}:{port}"]
2432

25-
def add_route(self, coro: Callable[..., Coroutine], endpoint: str, method: str, **kwargs):
33+
def add_endpoint_method(self, coro: Callable[..., Coroutine], endpoint: str, method: str, **kwargs):
2634
self.app.route(endpoint, methods=[method], **kwargs)(coro)
2735

36+
@staticmethod
37+
def create_router(**kwargs):
38+
return FlaskRouter(kwargs.pop("name"), **kwargs)
39+
40+
def add_router(self, router: FlaskRouter):
41+
self.app.register_blueprint(router.blueprint)
42+
2843
async def run(self):
2944
await serve(WsgiToAsgi(self.app), self._config)

interactions_restful/extension.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from interactions import Client
77

8-
from .abc import BaseApi
8+
from .abc import BaseApi, BaseRouter
99

1010
__all__ = ("APIClient", "setup")
1111
API_TYPE = TypeVar("API_TYPE", bound=BaseApi)
@@ -33,11 +33,20 @@ def _run_api_thread_worker(self):
3333

3434
def _register_extension_routes(self):
3535
for extension in self.bot.ext.values():
36+
has_methods = False
3637
for _, coro in getmembers(extension, predicate=asyncio.iscoroutinefunction):
3738
if not hasattr(coro, "__api__"):
3839
continue
40+
41+
has_methods = True
42+
43+
if getattr(extension, "router", None) is None:
44+
extension.router = self.client.create_router(name=extension.name)
3945
data = coro.__api__
40-
self.client.add_route(coro, data["endpoint"], data["method"], **data["kwargs"])
46+
extension.router.add_endpoint_method(coro, data["endpoint"], data["method"], **data["kwargs"])
47+
48+
if has_methods:
49+
self.client.add_router(extension.router)
4150

4251

4352
def setup(client: Client, api: Type[API_TYPE], host: str, port: int, **kwargs) -> APIClient:

0 commit comments

Comments
 (0)