added main files

This commit is contained in:
2024-04-05 00:38:00 +07:00
commit ba6c68db7f
10 changed files with 708 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
from . import api
+79
View File
@@ -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.ApiError(
result["response"]["msg"]["text"]
)
return result["data"]
+14
View File
@@ -0,0 +1,14 @@
class ApiException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class NoAuthData(ApiException):
pass
class ApiError(ApiException):
pass
+125
View File
@@ -0,0 +1,125 @@
from . import models
class Methods:
def __init__(self, data):
self.request = data.request
def push_message(
self,
*,
text: str,
phone: str,
sender_name: str,
priority: int = 2,
external_id: str = None,
route: str = None,
call_protection: int = None
) -> models.PushMessageData:
"""
Implementation of push_msg
:param text: message text
:param phone: receiver phone number
:param sender_name: sender name
:param priority: message priority
:param external_id: ID in your platform
:param route: message delivery route
:param call_protection: call waiting time
:return: result of calling the method
"""
return models.PushMessageData(
**self.request(
method="push_msg",
data={
"text": text,
"phone": phone,
"sender_name": sender_name,
"priority": priority,
"external_id": external_id,
"route": route,
"call_protection": call_protection
}
)
)
def get_message_report(
self,
*,
id: int
) -> models.GetMessageReportData:
"""
Implementation of
:param id:
:return: result of calling the method
"""
return models.GetMessageReportData(
**self.request(
method="get_msg_report",
data={
"id": id
}
)
)
def get_profile(self) -> models.GetProfileData:
"""
Implementation of
:return: result of calling the method
"""
return models.GetProfileData(
**self.request(
method="get_profile"
)
)
def get_prices(
self,
*,
group_directions: int = None
) -> models.GetPricesData:
"""
Implementation of
:param group_directions:
:return: result of calling the method
"""
return models.GetPricesData(
**self.request(
method="get_prices",
data={
"group_directions": group_directions
}
)
)
def wait_call(
self,
*,
phone: str,
call_protection: int
) -> models.WaitCallData:
"""
Implementation of
:param phone:
:param call_protection:
:return: result of calling the method
"""
return models.WaitCallData(
**self.request(
method="wait_call",
data={
"phone": phone,
"call_protection": call_protection
}
)
)
+69
View File
@@ -0,0 +1,69 @@
from typing import Optional
from pydantic import BaseModel
class GetPricesData(BaseModel):
group_directions: str
class GetMessageReportData(BaseModel):
id: int
sender_name: str
text: str
phone: str
type: int
n_raw_sms: int
start_time: str # TODO: в будущем datetime pls
last_update: str # TODO: в будущем datetime pls
id_tarifs_group: int
state: int
state_text: str
credits: float
class WaitCallData(BaseModel):
call_to_number: str
id_call: str
waiting_call_from: str
class GetProfileData(BaseModel):
id: int
email: str
first_name: str
last_name: str
credits: float
credits_used: float
credits_name: str
currency: str
sender_name: str
referral_id: Optional[int]
class PushMessageData(BaseModel):
id: int
credits: float
n_raw_sms: int
sender_name: str
call_to_number: Optional[str]
class Message(BaseModel):
err_code: str
text: str
type: str
class GetProfileAnswer(BaseModel):
msg: Optional[Message]
data: Optional[GetProfileData]
class Answer(BaseModel):
msg: Optional[Message]
data: Optional[dict]
class BaseAnswer(BaseModel):
response: Answer