mirror of
https://github.com/catspace-dev/unicheckbot.git
synced 2024-11-21 21:46:32 +03:00
moved api request to helpers
This commit is contained in:
parent
49662e4ff1
commit
6023e35742
|
@ -4,6 +4,7 @@ from .start import start_cmd
|
|||
from .web import web_cmd
|
||||
from .whois import whois_cmd
|
||||
from .icmp import icmp_cmd
|
||||
from .inline import inline_processor
|
||||
|
||||
|
||||
def setup(dp: Dispatcher):
|
||||
|
@ -11,3 +12,4 @@ def setup(dp: Dispatcher):
|
|||
dp.register_message_handler(web_cmd, commands=['web', 'http'])
|
||||
dp.register_message_handler(whois_cmd, commands=['whois'])
|
||||
dp.register_message_handler(icmp_cmd, commands=['icmp'])
|
||||
dp.register_inline_handler(inline_processor)
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
from aiogram.types import Message
|
||||
from tgbot.nodes import nodes
|
||||
from tgbot.nodes import nodes as all_nodes
|
||||
from httpx import AsyncClient, Response
|
||||
from datetime import datetime
|
||||
from core.coretypes import ErrorCodes, ErrorPayload, ICMPCheckerResponse, ResponseStatus, APINodeInfo
|
||||
from ..helpers import send_api_requests
|
||||
|
||||
icmp_help_message = """
|
||||
❓ Производит проверку хоста по протоколу ICMP.
|
||||
|
@ -33,18 +34,6 @@ async def prepare_icmp_check_result(res: Response):
|
|||
return message
|
||||
|
||||
|
||||
async def send_icmp_check_request(target: str):
|
||||
for node in nodes:
|
||||
async with AsyncClient() as client:
|
||||
result = await client.get(
|
||||
f"{node.address}/icmp", params=dict(
|
||||
target=target,
|
||||
token=node.token
|
||||
)
|
||||
)
|
||||
yield result
|
||||
|
||||
|
||||
async def check_icmp(msg: Message, target: str):
|
||||
rsp_msg = await msg.answer(f"Отчет о проверке хоста:"
|
||||
f"\n\n— Хост: {target}"
|
||||
|
@ -52,7 +41,7 @@ async def check_icmp(msg: Message, target: str):
|
|||
)
|
||||
iter_keys = 1 # because I can't use enumerate
|
||||
# using generators for magic...
|
||||
async for res in send_icmp_check_request(target):
|
||||
async for res in send_api_requests("icmp", dict(target=target), all_nodes):
|
||||
await msg.bot.send_chat_action(msg.chat.id, 'typing')
|
||||
node_formatted_response = await prepare_icmp_check_result(res)
|
||||
rsp_msg = await rsp_msg.edit_text(rsp_msg.text + f"\n\n{iter_keys}. {node_formatted_response}")
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
from aiogram.types import Message
|
||||
from typing import Optional
|
||||
from tgbot.handlers.helpers import check_int
|
||||
from tgbot.nodes import nodes
|
||||
from tgbot.nodes import nodes as all_nodes
|
||||
from httpx import AsyncClient, Response
|
||||
from core.coretypes import ResponseStatus, HTTP_EMOJI
|
||||
from datetime import datetime
|
||||
from ..helpers import send_api_requests
|
||||
|
||||
web_help_message = """
|
||||
❓ Производит проверку хоста по протоколу HTTP.
|
||||
|
@ -35,19 +36,6 @@ async def prepare_webcheck_message(response: Response) -> str:
|
|||
return message
|
||||
|
||||
|
||||
async def send_check_requests(host: str, port: int):
|
||||
for node in nodes:
|
||||
async with AsyncClient() as client:
|
||||
result = await client.get(
|
||||
f"{node.address}/http", params=dict(
|
||||
target=host,
|
||||
port=port,
|
||||
token=node.token
|
||||
)
|
||||
)
|
||||
yield result
|
||||
|
||||
|
||||
async def check_web(message: Message, host: str, port: Optional[int]):
|
||||
if port is None:
|
||||
port = 80
|
||||
|
@ -57,7 +45,7 @@ async def check_web(message: Message, host: str, port: Optional[int]):
|
|||
)
|
||||
iter_keys = 1 # because I can't use enumerate
|
||||
# using generators for magic...
|
||||
async for res in send_check_requests(host, port):
|
||||
async for res in send_api_requests("http", dict(target=host, port=port), all_nodes):
|
||||
# set typing status...
|
||||
await message.bot.send_chat_action(message.chat.id, 'typing')
|
||||
|
||||
|
|
|
@ -1,3 +1,8 @@
|
|||
from httpx import AsyncClient
|
||||
from typing import List
|
||||
from core.coretypes import APINode
|
||||
|
||||
|
||||
def check_int(value) -> bool:
|
||||
try:
|
||||
int(value)
|
||||
|
@ -5,3 +10,13 @@ def check_int(value) -> bool:
|
|||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
async def send_api_requests(endpoint: str, data: dict, nodes: List[APINode]):
|
||||
for node in nodes:
|
||||
data.update(dict(token=node.token))
|
||||
async with AsyncClient() as client:
|
||||
result = await client.get(
|
||||
f"{node.address}/{endpoint}", params=data
|
||||
)
|
||||
yield result
|
||||
|
|
Loading…
Reference in New Issue
Block a user