TW-Requests/tgbot_app/api/api_client.py

43 lines
1.3 KiB
Python
Raw Permalink Normal View History

2024-07-15 07:15:51 +00:00
import logging
import aiohttp
class APIClient:
headers = headers = {"Content-Type": "application/json"}
async def get_query(self, url, prompt=None):
async with aiohttp.ClientSession() as session:
try:
async with session.get(
url=url,
headers=self.headers,
json=prompt,
ssl=False
) as response:
response.raise_for_status()
data = await response.json()
return data
except aiohttp.ClientError as e:
logging.error(e)
return 'Произошла ошибка'
async def post_query(self, url, prompt):
async with aiohttp.ClientSession() as session:
try:
async with session.post(
url=url,
headers=self.headers,
json=prompt,
ssl=False
) as response:
response.raise_for_status()
data = await response.text()
return data
except aiohttp.ClientError as e:
logging.error(e)
return 'Произошла ошибка'
api_client = APIClient()