From aac0458437a4e3912b1dda1f9810562d4f916488 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 5 Apr 2024 01:50:12 +0700 Subject: [PATCH] fixed: error with data moved: api to init updated: version up to 0.0.2 --- prosto_sms/__init__.py | 79 ++++++++++++++++++++++++++++++++++++++++ prosto_sms/api.py | 79 ---------------------------------------- prosto_sms/exceptions.py | 2 +- setup.py | 2 +- 4 files changed, 81 insertions(+), 81 deletions(-) delete mode 100644 prosto_sms/api.py diff --git a/prosto_sms/__init__.py b/prosto_sms/__init__.py index e69de29..f76b3ff 100644 --- a/prosto_sms/__init__.py +++ b/prosto_sms/__init__.py @@ -0,0 +1,79 @@ +from typing import Optional + +import requests +from loguru import logger + +from . import exceptions +from . import methods + + +class API: + """ + Main api class + """ + + def __init__( + self, + email: str = None, + password: str = None, + key: str = None + ): + """ + Initializing the API + + :param email: account email address + :param password: account password + :param key: api key + """ + + self.email = email + self.password = password + self.key = key + self.api_url = "https://ssl.bs00.ru" + self.methods = methods.Methods(self) + + def request(self, method: str, data: dict = None) -> Optional[dict]: + """ + Method for requesting information + + :param method: required method + :param data: request parameters + :return: response data + """ + + if data is None: + data = {} + + data["format"] = "json" + if not data.get("method"): + data["method"] = method + + if self.key and not (self.email and self.password): + data["key"] = self.key + elif (self.email and self.password) and not self.key: + data["email"] = self.email + data["password"] = self.password + else: + raise exceptions.NoAuthData( + "email and password OR key is required" + ) + + try: + result = requests.get( + url=self.api_url, + params=data, + timeout=30 + ) + result.raise_for_status() + + result = result.json() + except Exception as err: + logger.exception(err) + return + + if result["response"]["msg"].get("type") == "error": + raise exceptions.RequestError( + result["response"]["msg"]["text"] + ) + + return result["response"]["data"] diff --git a/prosto_sms/api.py b/prosto_sms/api.py deleted file mode 100644 index 943a61e..0000000 --- a/prosto_sms/api.py +++ /dev/null @@ -1,79 +0,0 @@ -from typing import Optional - -import requests -from loguru import logger - -from . import exceptions -from . import methods - - -class API: - """ - Main api class - """ - - def __init__( - self, - email: str = None, - password: str = None, - key: str = None - ): - """ - Initializing the API - - :param email: account email address - :param password: account password - :param key: api key - """ - - self.email = email - self.password = password - self.key = key - self.api_url = "https://ssl.bs00.ru" - self.methods = methods.Methods(self) - - def request(self, method: str, data: dict = None) -> Optional[dict]: - """ - Method for requesting information - - :param method: required method - :param data: request parameters - :return: response data - """ - - if data is None: - data = {} - - data["format"] = "json" - if not data.get("method"): - data["method"] = method - - if self.key and not (self.email and self.password): - data["key"] = self.key - elif (self.email and self.password) and not self.key: - data["email"] = self.email - data["password"] = self.password - else: - raise exceptions.NoAuthData( - "email and password OR key is required" - ) - - try: - result = requests.get( - url=self.api_url, - params=data, - timeout=30 - ) - result.raise_for_status() - - result = result.json() - except Exception as err: - logger.exception(err) - return - - if result["response"]["msg"].get("type") == "error": - raise exceptions.ApiError( - result["response"]["msg"]["text"] - ) - - return result["data"] diff --git a/prosto_sms/exceptions.py b/prosto_sms/exceptions.py index 67a136e..11b7c72 100644 --- a/prosto_sms/exceptions.py +++ b/prosto_sms/exceptions.py @@ -10,5 +10,5 @@ class NoAuthData(ApiException): pass -class ApiError(ApiException): +class RequestError(ApiException): pass diff --git a/setup.py b/setup.py index 6f3e319..3cd0a1a 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open('requirements.txt') as f: setup( name='prosto_sms', - version='0.0.1', + version='0.0.2', author='Alex Dennitsev', author_email='me@chydo.dev',