Skip to content

Commit 501dfe7

Browse files
committed
fix: update new api for commands
1 parent 41af09b commit 501dfe7

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
setup(
1818
name="swibots",
19-
version="1.4.55",
19+
version="1.4.56",
2020
packages=find_packages(exclude=["samples", "bots_impl", "docs"]),
2121
long_description=long_description,
2222
long_description_content_type="text/markdown",

swibots/api/bot/controllers/bot_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async def update_bot_info(self, bot_info: BotInfo) -> BotInfo:
4949
data = bot_info.to_json_request()
5050
if "preview" in data:
5151
data["preview"] = json.dumps(data["preview"])
52-
response = await self.client.put(BASE_PATH, data=data)
52+
response = await self.client.put(f"{BASE_PATH}/update", data=data)
5353
return BotInfo.build_from_json(response.data)
5454

5555
async def delete_bot_info(self, bot_id: str) -> bool:

swibots/bot_app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __init__(
6262
is_app: Optional[bool] = False,
6363
home_callback: Optional[str] = None,
6464
preview: Optional[AppPage] = None,
65+
session_name: Optional[str] = None,
6566
**kwargs,
6667
):
6768
"""
@@ -102,7 +103,7 @@ def __init__(
102103
self.user = self.auth_service.get_me_sync(user_type=self._user_type)
103104
self.name = self.user.name
104105

105-
self.rate_limit = AsyncRateLimiter(storage_file=f"{self.user.id}.pkl")
106+
self.rate_limit = AsyncRateLimiter(storage_file=f"{session_name or self.user.id}.pkl")
106107

107108
# Add rate limit functions
108109
self.update_bot_commands = self.rate_limit.limit(

swibots/rate_limiter.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,48 @@
22

33

44
class AsyncRateLimiter:
5-
def __init__(self, storage_file='rate_limit_data.pkl'):
5+
def __init__(self, storage_file="swibots.pkl"):
66
self.storage_file = storage_file
77
self.rate_limit_data = {}
8-
8+
99
async def _load_data(self):
1010
if os.path.exists(self.storage_file):
11-
async with aiofiles.open(self.storage_file, 'rb') as f:
11+
async with aiofiles.open(self.storage_file, "rb") as f:
1212
content = await f.read()
1313
self.rate_limit_data = pickle.loads(content)
14-
14+
1515
async def _save_data(self):
16-
async with aiofiles.open(self.storage_file, 'wb') as f:
16+
async with aiofiles.open(self.storage_file, "wb") as f:
1717
await f.write(pickle.dumps(self.rate_limit_data))
18-
18+
1919
def limit(self, key: str, times: int, seconds: int):
2020
def decorator(func):
2121
async def async_wrapper(*args, **kwargs):
2222
current_time = time.time()
2323

2424
await self._load_data()
25-
25+
2626
if key not in self.rate_limit_data:
2727
self.rate_limit_data[key] = []
28-
29-
self.rate_limit_data[key] = [t for t in self.rate_limit_data[key] if current_time - t < seconds]
30-
28+
29+
self.rate_limit_data[key] = [
30+
t for t in self.rate_limit_data[key] if current_time - t < seconds
31+
]
32+
3133
if len(self.rate_limit_data[key]) >= times:
32-
raise Exception(f"Rate limit exceeded: {times} calls in {seconds} seconds")
33-
34+
raise RateLimitException(
35+
f"Rate limit exceeded: {times} calls in {seconds} seconds"
36+
)
37+
3438
# Record the call
3539
self.rate_limit_data[key].append(current_time)
3640
await self._save_data()
3741
return await func(*args, **kwargs)
38-
42+
3943
return async_wrapper
44+
4045
return decorator
4146

4247

4348
class RateLimitException(Exception):
44-
pass
49+
pass

0 commit comments

Comments
 (0)