Skip to content

Commit 06d59f8

Browse files
authored
Merge pull request #1 from interactions-py/ipy-v5
chore: merge `ipy-v5` to `main`
2 parents 452a4b3 + cbb28aa commit 06d59f8

File tree

19 files changed

+1365
-262
lines changed

19 files changed

+1365
-262
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ default_language_version:
22
python: python3.10
33
repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
5-
rev: v4.3.0
5+
rev: v4.4.0
66
hooks:
77
- id: requirements-txt-fixer
88
name: Requirements
@@ -31,23 +31,23 @@ repos:
3131
- id: check-merge-conflict
3232
name: Merge Conflicts
3333
- repo: https://github.com/psf/black
34-
rev: 22.3.0
34+
rev: 23.1.0
3535
hooks:
3636
- id: black
3737
name: Black Formatting
3838
language: python
3939
types: [file, python]
4040
args: [--line-length=100]
4141
- repo: https://github.com/PyCQA/flake8
42-
rev: 4.0.1
42+
rev: 6.0.0
4343
hooks:
4444
- id: flake8
4545
name: flake8 Formatting
4646
language: python
4747
types: [file, python]
4848
args: [--max-line-length=100, --ignore=E203 E301 E302 E501 E402 E704 W503 W504]
4949
- repo: https://github.com/pycqa/isort
50-
rev: 5.10.1
50+
rev: 5.12.0
5151
hooks:
5252
- id: isort
5353
name: isort Formatting

README.md

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,59 @@
1-
# FastAPI wrapper for interactions.py
1+
# interactions restful
22

3-
An Extension library to add Fast API for your interactions.py bot .
3+
A library for interactions.py allowing runtime API structures
44

55
## Installation
66

7-
``pip install interactions-fastapi``
7+
Using pip:
8+
`pip install interactions-restful`
89

10+
Using poetry:
11+
`poetry add interactions-restful`
912

10-
## Examples
13+
Don't forget to specify backend you want to use:
14+
- flask `pip install interactions-restful[flask]`
15+
- fastapi `pip install interactions-restful[fastapi]`
16+
17+
## Simple example
18+
19+
### Main file
20+
21+
```python
22+
import interactions
23+
from interactions_restful import setup
24+
from interactions_restful.backends.fast_api import FastAPI
25+
26+
client = interactions.Client()
27+
28+
setup(client, FastAPI, "127.0.0.1", 5000)
29+
30+
client.load_extension("api")
31+
32+
client.start("token")
33+
```
34+
35+
### Extension file
36+
- `api.py`
1137

1238
```python
1339
import interactions
14-
from interactions.ext.fastapi import setup
40+
from interactions_restful import route
1541

16-
client = interactions.Client(...)
17-
api = setup(client)
1842

19-
@api.get("/")
20-
async def index():
21-
return {"status": "success"}
43+
class MyAPI(interactions.Extension):
44+
@route("GET", "/")
45+
def index(self):
46+
return {"status": "Hello, i.py"}
47+
48+
@interactions.slash_command()
49+
async def test_command(self, ctx):
50+
await ctx.send("Hello, API")
2251

23-
client.start()
2452
```
2553

26-
Server will run on ``127.0.0.1:32512`` (host and port modifiable)
54+
## Backends
2755

28-
More examples in ``examples`` folder.
56+
Currently, library support only flask and fastapi as a backend for building an api, but if you don't want to use them you can create own backend.
2957

3058
## Documentation
3159

examples/bot.py

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,16 @@
1-
from os import getenv
2-
3-
from dotenv import load_dotenv
4-
from fastapi import WebSocket, WebSocketDisconnect
5-
61
import interactions
7-
from interactions.ext.fastapi import setup
8-
9-
load_dotenv()
10-
11-
bot = interactions.Client(getenv("TOKEN"), intents=interactions.Intents.ALL)
12-
api = setup(bot)
13-
14-
15-
@bot.event()
16-
async def on_start():
17-
print("bot started")
18-
19-
20-
@bot.command()
21-
async def test_command(ctx):
22-
await ctx.send("Ok")
23-
24-
25-
@api.route("get", "/")
26-
async def index():
27-
return {"test": "yay"}
2+
from interactions_restful import setup
3+
from interactions_restful.backends.fast_api import FastAPI
284

295

30-
@api.get("/guilds/{guild_id}/members/{member_id}")
31-
async def test(guild_id: int, member_id: int):
32-
for guild in bot.guilds:
33-
if int(guild.id) == guild_id:
34-
break
35-
member = await guild.get_member(member_id)
36-
return member._json
6+
client = interactions.Client()
7+
setup(client, FastAPI, "localhost", 8000)
8+
client.load_extension("exts.my_ext")
379

3810

39-
@api.websocket("/ws/")
40-
async def websocket_control(websocket: WebSocket):
41-
"""
42-
Before using you have to install additional requirement:
43-
`pip install uvicorn[standard]`
44-
"""
45-
await websocket.accept()
46-
await websocket.send_json({"test": "yayayyayayyaya"})
47-
try:
48-
while True:
49-
packet = await websocket.receive_json()
50-
print(packet)
51-
# some stuff with websocket
52-
except WebSocketDisconnect:
53-
pass
11+
@interactions.listen()
12+
async def on_startup():
13+
print(f"Bot `{client.user.username}` started up")
5414

5515

56-
bot.start()
16+
client.start("TOKEN")

examples/exts/cog.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

examples/exts/my_ext.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from interactions import Extension, slash_command
2+
from interactions_restful import route
3+
4+
5+
class MyExtension(Extension):
6+
@route("GET", "/")
7+
async def index(self):
8+
return {"data": "hello interactions.py"}
9+
10+
@slash_command()
11+
async def command(self, ctx):
12+
await ctx.send("hello, api!")

interactions/ext/fastapi/__init__.py

Lines changed: 0 additions & 102 deletions
This file was deleted.

interactions/ext/fastapi/main.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

interactions_restful/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .decorators import *
2+
from .extension import *

interactions_restful/abc.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from abc import ABC, abstractmethod
2+
from typing import Callable, Coroutine
3+
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
11+
12+
13+
class BaseApi(ABC):
14+
@abstractmethod
15+
def __init__(self, host: str, port: int, **kwargs):
16+
pass
17+
18+
@abstractmethod
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):
28+
pass
29+
30+
@abstractmethod
31+
async def run(self):
32+
pass

0 commit comments

Comments
 (0)