restore composer.json, add mysqli extension
Some checks failed
Build and Push to Docker Hub / build-test-push (push) Failing after 1m55s

This commit is contained in:
2026-04-15 15:38:41 +05:00
parent 24f6fe42cc
commit 477e79ed80
5 changed files with 92 additions and 67 deletions

View File

@@ -1,22 +1,29 @@
import requests
import logging
from django.core.cache import cache
from common.env import env
logger = logging.getLogger(__name__)
class SendService:
GET = "GET"
POST = "POST"
PATCH = "PATCH"
CONTACT = "contact"
CACHE_KEY = "eskiz_token"
TOKEN_LIFETIME = 86400 * 20 # Eskiz tokens usually last 30 days, we cache for 20
def __init__(
self, api_url=None, email=None, password=None, callback_url=None
):
self.api_url = api_url or env("SMS_API_URL")
self.api_url = (api_url or env("SMS_API_URL")).rstrip("/")
self.email = email or env("SMS_LOGIN")
self.password = password or env("SMS_PASSWORD")
self.callback_url = callback_url
self.headers = {}
self.headers = {
"Content-Type": "application/json",
"Accept": "application/json",
}
self.methods = {
"auth_user": "auth/user",
@@ -26,66 +33,89 @@ class SendService:
}
def request(self, api_path, data=None, method=None, headers=None):
incoming_data = {"status": "error"}
incoming_data = {"status": "error", "message": "Unknown error"}
url = f"{self.api_url}/{api_path}"
final_headers = self.headers.copy()
if headers:
final_headers.update(headers)
try:
# For Eskiz, data should be JSON for Notify API
response = requests.request(
method,
f"{self.api_url}/{api_path}",
data=data,
headers=headers,
url,
json=data if method != self.GET else None,
params=data if method == self.GET else None,
headers=final_headers,
timeout=10
)
if api_path == self.methods["auth_refresh"]:
if response.status_code == 200:
incoming_data["status"] = "success"
else:
if response.status_code == 401:
# Token might be expired, clear cache
cache.delete(self.CACHE_KEY)
try:
incoming_data = response.json()
except ValueError:
incoming_data = {"status": "error", "message": response.text}
if response.status_code >= 400:
logger.error(f"Eskiz API Error ({response.status_code}): {incoming_data}")
except requests.RequestException as error:
logger.error(f"Eskiz Request Exception: {str(error)}")
raise Exception(str(error))
return incoming_data
def get_token(self):
token = cache.get(self.CACHE_KEY)
if not token:
logger.info("Eskiz token not found in cache, authenticating...")
auth_res = self.auth()
if auth_res.get("status") == "success" or "data" in auth_res:
token = auth_res.get("data", {}).get("token")
if token:
cache.set(self.CACHE_KEY, token, self.TOKEN_LIFETIME)
else:
logger.error(f"Eskiz Auth Failed: {auth_res}")
return token
def auth(self):
data = {"email": self.email, "password": self.password}
# Eskiz login expects JSON
return self.request(
self.methods["auth_login"], data=data, method=self.POST
)
def refresh_token(self):
token = self.auth()["data"]["token"]
self.headers["Authorization"] = "Bearer " + token
token = self.get_token()
headers = {"Authorization": f"Bearer {token}"}
context = {
"headers": self.headers,
"method": self.PATCH,
"api_path": self.methods["auth_refresh"],
}
return self.request(
context["api_path"],
method=context["method"],
headers=context["headers"],
res = self.request(
self.methods["auth_refresh"],
method=self.PATCH,
headers=headers,
)
if res.get("status") == "success":
# Some Eskiz versions return a new token on refresh
new_token = res.get("data", {}).get("token")
if new_token:
cache.set(self.CACHE_KEY, new_token, self.TOKEN_LIFETIME)
return res
def get_my_user_info(self):
token = self.auth()["data"]["token"]
self.headers["Authorization"] = "Bearer " + token
data = {
"headers": self.headers,
"method": self.GET,
"api_path": self.methods["auth_user"],
}
token = self.get_token()
headers = {"Authorization": f"Bearer {token}"}
return self.request(
data["api_path"], method=data["method"], headers=data["headers"]
self.methods["auth_user"], method=self.GET, headers=headers
)
def add_sms_contact(self, first_name, phone_number, group):
token = self.auth()["data"]["token"]
self.headers["Authorization"] = "Bearer " + token
token = self.get_token()
headers = {"Authorization": f"Bearer {token}"}
data = {
"name": first_name,
@@ -94,42 +124,32 @@ class SendService:
"mobile_phone": phone_number,
}
context = {
"headers": self.headers,
"method": self.POST,
"api_path": self.CONTACT,
"data": data,
}
return self.request(
context["api_path"],
data=context["data"],
method=context["method"],
headers=context["headers"],
self.CONTACT,
data=data,
method=self.POST,
headers=headers,
)
def send_sms(self, phone_number, message):
token = self.auth()["data"]["token"]
self.headers["Authorization"] = "Bearer " + token
token = self.get_token()
if not token:
return {"status": "error", "message": "Failed to obtain auth token"}
headers = {"Authorization": f"Bearer {token}"}
# Use string "4546" for sender name
data = {
"from": 4546,
"from": "4546",
"mobile_phone": phone_number,
"callback_url": self.callback_url,
"message": message,
}
context = {
"headers": self.headers,
"method": self.POST,
"api_path": self.methods["send_message"],
"data": data,
}
res = self.request(
context["api_path"],
data=context["data"],
method=context["method"],
headers=context["headers"],
self.methods["send_message"],
data=data,
method=self.POST,
headers=headers,
)
return res