Skip to content

Commit

Permalink
Added async services and code cleanup
Browse files Browse the repository at this point in the history
...
  • Loading branch information
YouKnow-sys committed Oct 2, 2024
1 parent 2c79b1c commit 7d4f2ae
Show file tree
Hide file tree
Showing 9 changed files with 684 additions and 366 deletions.
12 changes: 6 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

setuptools.setup(
name="smsir-python",
version="1.0.7",
version="1.1.7",
author="Mojtaba Akbari",
author_email="[email protected]",
packages=["sms_ir"],
packages=["sms_ir", "sms_ir/async_services", "sms_ir/sync_services"],
description="Python Package of SMS.ir Panel ",
long_description=description,
long_description_content_type="text/markdown",
url="https://github.com/IPeCompany/SmsPanelV2.Python",
license='MIT',
python_requires='>=3.7',
install_requires=[],
)
license="MIT",
python_requires=">=3.7",
install_requires=["aiohttp"],
)
3 changes: 2 additions & 1 deletion sms_ir/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .services import SmsIr
from .sync_services import SmsIr
from .async_services import AsyncSmsIr
289 changes: 289 additions & 0 deletions sms_ir/async_services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
from typing import List, Optional

from aiohttp import ClientResponse

from .requester import Requestser

class AsyncSmsIr:
def __init__(
self,
api_key: str,
linenumber: Optional[int] = None,
) -> None:
headers = {
"X-API-KEY": api_key,
"ACCEPT": "application/json",
"Content-Type": "application/json",
}

self._linenumber = linenumber
self._requester = Requestser(headers)

async def close(self):
"""
Close the session connection
"""
await self._requester.close()

async def send_sms(
self,
number: str,
message: str,
linenumber: Optional[int] = None,
) -> ClientResponse:
"""
Send message to specific mobile number
"""

return await self.send_bulk_sms(
numbers=[number],
message=message,
linenumber=linenumber,
)

async def send_bulk_sms(
self,
numbers: List[str],
message: str,
linenumber: Optional[int] = None,
) -> ClientResponse:
"""
Send message to multiple mobile numbers
"""

url = "/v1/send/bulk/"

data = {
"lineNumber": linenumber or self._linenumber,
"MessageText": message,
"Mobiles": numbers,
}

return await self._requester.post(
url,
data,
)

async def send_like_to_like(
self,
numbers: List[str],
messages: List[str],
linenumber: Optional[int] = None,
send_date_time: Optional[str] = None,
) -> ClientResponse:
"""
Send multiple messages to multiple mobile numbers pair to pair
"""

url = "/v1/send/liketolike/"

data = {
"lineNumber": linenumber or self._linenumber,
"MessageTexts": messages,
"Mobiles": numbers,
"SendDateTime": send_date_time,
}

return await self._requester.post(
url,
data,
)

async def delete_scheduled(
self,
pack_id: int,
) -> ClientResponse:
"""
Delete scheduled message pack
"""

url = f"/v1/send/scheduled/{pack_id}/"

return await self._requester.delete(
url,
)

async def send_verify_code(
self,
number: int,
template_id: int,
**parameters: dict[str, str],
) -> ClientResponse:
"""
Send verification code with preasync defined template
"""

url = "/v1/send/verify/"

data = {
"Mobile": number,
"TemplateId": template_id,
"Parameters": [dict(name=k, value=v) for k, v in parameters.items()],
}

return await self._requester.post(
url,
data,
)

async def report_message(
self,
message_id: int,
) -> ClientResponse:
"""
get report of sent message
"""

url = f"/v1/send/{message_id}/"

return await self._requester.get(
url,
)

async def report_pack(
self,
pack_id: int,
) -> ClientResponse:
"""
get report of sent message pack
"""

url = f"/v1/send/pack/{pack_id}/"

return await self._requester.get(
url,
)

async def report_today(
self,
page_size: int = 10,
page_number: int = 1,
) -> ClientResponse:
"""
get report of Today sent Messages
"""

url = "/v1/send/live/"

params = {
"pageSize": page_size,
"pageNumber": page_number,
}

return await self._requester.get(
url,
params,
)

async def report_archived(
self,
from_date: Optional[int] = None,
to_date: Optional[int] = None,
page_size: int = 10,
page_number: int = 1,
) -> ClientResponse:
"""
get report of Archived Messages
"""

url = "/v1/send/archive/"

params = {
"fromDate": from_date,
"toDate": to_date,
"pageSize": page_size,
"pageNumber": page_number,
}

return await self._requester.get(
url,
params,
)

async def report_latest_received(
self,
count: int,
) -> ClientResponse:
"""
get report of latest received messages
"""

url = "/v1/receive/latest/"

params = {
"count": count,
}

return await self._requester.get(
url,
params,
)

async def report_today_received(
self,
page_size: int = 10,
page_number: int = 1,
) -> ClientResponse:
"""
get report of today received messages
"""

url = "/v1/receive/live/"

params = {
"pageSize": page_size,
"pageNumber": page_number,
}

return await self._requester.get(
url,
params,
)

async def report_archived_received(
self,
from_date: Optional[int] = None,
to_date: Optional[int] = None,
page_size: int = 10,
page_number: int = 1,
) -> ClientResponse:
"""
get report of today received messages
"""

url = "/v1/receive/archive/"

params = {
"fromDate": from_date,
"toDate": to_date,
"pageSize": page_size,
"pageNumber": page_number,
}

return await self._requester.get(
url,
params,
)

async def get_credit(self) -> ClientResponse:
"""
get account credit
"""

url = "/v1/credit/"

return await self._requester.get(
url,
)

async def get_line_numbers(self) -> ClientResponse:
"""
get account line numbers
"""

url = "/v1/line/"

return await self._requester.get(
url,
)

32 changes: 32 additions & 0 deletions sms_ir/async_services/requester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from aiohttp import ClientSession, ClientResponse

from ..base_requester import BaseRequester


class Requestser(BaseRequester):
def __init__(self, headers: dict[str, str]) -> None:
super().__init__()
self._session = ClientSession(base_url=self.endpoint, headers=headers)

async def close(self):
await self._session.close()

async def post(self, url: str, json) -> ClientResponse:
self.logger.info(f"send request to {url}")
return await self._session.post(
url,
json=json,
)

async def delete(self, url: str) -> ClientResponse:
self.logger.info(f"send request to {url}")
return await self._session.delete(
url,
)

async def get(self, url: str, params=None) -> ClientResponse:
self.logger.info(f"send request to {url}")
return await self._session.get(
url,
params=params,
)
21 changes: 21 additions & 0 deletions sms_ir/base_requester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import logging
import sys

ENDPOINT = "https://api.sms.ir"

class BaseRequester:
def __init__(self) -> None:
self.endpoint = ENDPOINT

# setup logging
self.log_level = logging.INFO

log_format = logging.Formatter("[%(asctime)s] [%(levelname)s] - %(message)s")
self.logger = logging.getLogger(__name__)
self.logger.setLevel(self.log_level)

# writing to stdout
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(self.log_level)
handler.setFormatter(log_format)
self.logger.addHandler(handler)
Loading

0 comments on commit 7d4f2ae

Please sign in to comment.