From ce940f764648b75ce71b57b78a52916a15062fad Mon Sep 17 00:00:00 2001 From: burdukov Date: Wed, 22 Nov 2023 20:48:27 +0500 Subject: [PATCH] ADDED: module for work with api --- neuroapi/__init__.py | 17 +++++++++++++ neuroapi/admin.py | 17 +++++++++++++ neuroapi/api_method.py | 5 ++++ neuroapi/enums/__init__.py | 1 + neuroapi/enums/get_all.py | 7 ++++++ neuroapi/image.py | 14 +++++++++++ neuroapi/post.py | 50 ++++++++++++++++++++++++++++++++++++++ neuroapi/user.py | 13 ++++++++++ 8 files changed, 124 insertions(+) create mode 100644 neuroapi/__init__.py create mode 100644 neuroapi/admin.py create mode 100644 neuroapi/api_method.py create mode 100644 neuroapi/enums/__init__.py create mode 100644 neuroapi/enums/get_all.py create mode 100644 neuroapi/image.py create mode 100644 neuroapi/post.py create mode 100644 neuroapi/user.py diff --git a/neuroapi/__init__.py b/neuroapi/__init__.py new file mode 100644 index 0000000..9cf92ac --- /dev/null +++ b/neuroapi/__init__.py @@ -0,0 +1,17 @@ +from .post import Post +from .admin import Admin +from .user import User +from .image import Image +from dotenv import load_dotenv +import os +from os.path import join, dirname + + +load_dotenv(join(dirname(__file__), "..", '.env')) + + +class neuroapi: + post = Post(os.environ.get('API_URL')) + admin = Admin(os.environ.get('API_URL')) + user = User(os.environ.get('API_URL')) + image = Image(os.environ.get('API_URL')) diff --git a/neuroapi/admin.py b/neuroapi/admin.py new file mode 100644 index 0000000..5e5754c --- /dev/null +++ b/neuroapi/admin.py @@ -0,0 +1,17 @@ +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 diff --git a/neuroapi/api_method.py b/neuroapi/api_method.py new file mode 100644 index 0000000..48bad77 --- /dev/null +++ b/neuroapi/api_method.py @@ -0,0 +1,5 @@ +class ApiMethod: + api_url: str + + def __init__(self, api_url: str) -> None: + self.api_url = api_url diff --git a/neuroapi/enums/__init__.py b/neuroapi/enums/__init__.py new file mode 100644 index 0000000..4ab1fb4 --- /dev/null +++ b/neuroapi/enums/__init__.py @@ -0,0 +1 @@ +from .get_all import EGetAll diff --git a/neuroapi/enums/get_all.py b/neuroapi/enums/get_all.py new file mode 100644 index 0000000..9100757 --- /dev/null +++ b/neuroapi/enums/get_all.py @@ -0,0 +1,7 @@ +from enum import Enum + + +class EGetAll(Enum): + all = 'all' + will_post = 'will-post' + posted = 'posted' diff --git a/neuroapi/image.py b/neuroapi/image.py new file mode 100644 index 0000000..223c1ae --- /dev/null +++ b/neuroapi/image.py @@ -0,0 +1,14 @@ +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, message_id: int): + payload = {'post_id': post_id, 'file_id': file_id, + 'has_spoiler': has_spoiler, 'message_id': message_id} + async with ClientSession() as session: + response = await session.post( + self.api_url+'/image/add', data=payload) + data = await response.json() + if 'statusCode' in data: + raise Exception(data['message']) diff --git a/neuroapi/post.py b/neuroapi/post.py new file mode 100644 index 0000000..757a926 --- /dev/null +++ b/neuroapi/post.py @@ -0,0 +1,50 @@ +from aiohttp import ClientSession, ClientResponse +from .enums import EGetAll +from .api_method import ApiMethod + + +class Post(ApiMethod): + + async def new(self, text: str, from_user_id: str, media_group_id: str): + payload = {'text': text, 'from_user_id': from_user_id} + if media_group_id != 'None': + payload['media_group_id'] = media_group_id + async with ClientSession() as session: + response: ClientResponse = await session.post(self.api_url+'/post/new', data=payload) + data = await response.json() + if 'statusCode' in data: + raise Exception(data['message']) + return data['status'] + + 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): + async with ClientSession() as session: + response = await session.get(self.api_url+f'/post/get-by-media-group-id/{media_group_id}') + data = await response.json() + if 'statusCode' in data: + raise Exception(data['message']) + return await response.json() diff --git a/neuroapi/user.py b/neuroapi/user.py new file mode 100644 index 0000000..faf4246 --- /dev/null +++ b/neuroapi/user.py @@ -0,0 +1,13 @@ +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'])