Added singleton config and changed module structure

This commit is contained in:
2023-11-23 22:13:10 +03:00
parent 6389c4f29e
commit 8c2d706401
12 changed files with 41 additions and 29 deletions

View File

@@ -0,0 +1,18 @@
from aiohttp import ClientSession
from .api_method import ApiMethod
class Admin(ApiMethod):
async def get(self):
async with ClientSession() as session:
response = await session.get(self.api_url+'/admin/get')
return await response.json()
async def is_admin(self, id: str):
async with ClientSession() as session:
response = await session.get(self.api_url+f'/admin/is-admin/{id}')
if await response.text() == 'false':
return False
return True

View File

@@ -0,0 +1,8 @@
from ..config import Config
class ApiMethod:
api_url: str
def __init__(self) -> None:
self.api_url = Config().api_url

View File

@@ -0,0 +1 @@
from .get_all import EGetAll

View File

@@ -0,0 +1,7 @@
from enum import Enum
class EGetAll(Enum):
all = 'all'
will_post = 'will-post'
posted = 'posted'

View File

@@ -0,0 +1,20 @@
import json
from aiohttp import ClientSession
from .api_method import ApiMethod
class Image(ApiMethod):
async def add(self, post_id: str, file_id: str, has_spoiler: bool | None, message_id: int):
payload = {'post_id': post_id, 'file_id': file_id,
'has_spoiler': has_spoiler, 'message_id': message_id}
if has_spoiler is None:
payload.pop('has_spoiler')
payload = json.dumps(payload)
async with ClientSession() as session:
response = await session.post(
self.api_url+'/image/add', data=payload, headers={'Content-Type': 'application/json'})
data = await response.json()
if 'statusCode' in data:
raise Exception(data['message'])

57
neuroapi/_methods/post.py Normal file
View File

@@ -0,0 +1,57 @@
import requests
from aiohttp import ClientSession
from .api_method import ApiMethod
from .enums import EGetAll
class Post(ApiMethod):
async def new(self, text: str, from_user_id: str, media_group_id: str = "None"):
payload = {'text': text, 'from_user_id': from_user_id}
if media_group_id != 'None':
payload['media_group_id'] = media_group_id
response = requests.post(self.api_url+'/post/new', data=payload)
data = response.json()
if 'statusCode' in data:
raise Exception(data['message'])
return data
async def __get_all(self, status: EGetAll):
async with ClientSession() as session:
response = await session.get(self.api_url+f'/post/get-all/{status.value}')
return response
async def get_all(self):
result = await self.__get_all(EGetAll.all)
return await result.json()
async def get_will_post(self):
result = await self.__get_all(EGetAll.will_post)
return await result.json()
async def get_posted(self):
result = await self.__get_all(EGetAll.posted)
return await result.json()
async def get(self, post_id: str):
async with ClientSession() as session:
response = await session.get(self.api_url+f'/post/get/{post_id}')
data = await response.json()
if 'statusCode' in data:
raise Exception(data['message'])
return data
async def get_by_media_group_id(self, media_group_id: str):
response = requests.get(self.api_url+f'/post/get-by-media-group-id/{media_group_id}')
data = response.json()
if 'statusCode' in data:
raise Exception(data['message'])
return data
async def edit_text(self, post_id: str, text: str):
response = requests.post(self.api_url+f"/post/edit/{post_id}", data={"text": text})
data = response.json()
if 'statusCode' in data:
raise Exception(data['message'])
return data

14
neuroapi/_methods/user.py Normal file
View File

@@ -0,0 +1,14 @@
from aiohttp import ClientSession
from .api_method import ApiMethod
class User(ApiMethod):
async def get(self, id: str, username: str):
payload = {'id': id, 'username': username}
async with ClientSession() as session:
response = await session.post(
self.api_url+'/user/get', data=payload)
data = await response.json()
if 'statusCode' in data:
raise Exception(data['message'])