KTVManager_UI/lib/reqs.py

85 lines
2.8 KiB
Python
Raw Permalink Normal View History

2025-05-09 16:31:14 +01:00
import requests
import json
from datetime import datetime
2025-07-16 09:19:48 +01:00
from typing import List, Dict, Any, Optional
# Create a session object to reuse TCP connections
session = requests.Session()
def _make_api_request(
method: str,
base_url: str,
auth: str,
endpoint: str,
payload: Optional[Dict[str, Any]] = None,
) -> Any:
"""
A helper function to make API requests.
2025-05-09 16:31:14 +01:00
Args:
2025-07-16 09:19:48 +01:00
method: The HTTP method to use (e.g., 'GET', 'POST').
2025-07-15 15:44:19 +01:00
base_url: The base URL of the API.
auth: The authorization token.
2025-07-16 09:19:48 +01:00
endpoint: The API endpoint to call.
payload: The data to send with the request.
2025-05-09 16:31:14 +01:00
Returns:
2025-07-16 09:19:48 +01:00
The JSON response from the API.
2025-05-09 16:31:14 +01:00
"""
2025-07-16 09:19:48 +01:00
url = f"{base_url}/{endpoint}"
2025-05-09 16:31:14 +01:00
headers = {"Authorization": f"Basic {auth}"}
2025-07-16 09:19:48 +01:00
try:
response = session.request(method, url, headers=headers, data=payload)
response.raise_for_status()
return response
except (requests.exceptions.RequestException, json.JSONDecodeError) as e:
# Log the error for debugging purposes
print(f"API request failed: {e}")
return None
2025-05-09 16:31:14 +01:00
2025-07-16 09:19:48 +01:00
def get_urls(base_url: str, auth: str) -> List[Dict[str, Any]]:
"""Retrieves user account streams from the API."""
response = _make_api_request("GET", base_url, auth, "getUserAccounts/streams")
return response.json() if response else []
2025-05-09 16:31:14 +01:00
2025-07-16 09:19:48 +01:00
def get_user_accounts(base_url: str, auth: str) -> List[Dict[str, Any]]:
"""Retrieves user accounts from the API."""
response = _make_api_request("GET", base_url, auth, "getUserAccounts")
if not response:
return []
2025-05-09 16:31:14 +01:00
2025-07-16 09:19:48 +01:00
accounts = response.json()
2025-07-15 15:44:19 +01:00
for account in accounts:
2025-05-09 16:31:14 +01:00
account["expiaryDate_rendered"] = datetime.utcfromtimestamp(
account["expiaryDate"]
).strftime("%d/%m/%Y")
2025-07-15 15:44:19 +01:00
return accounts
2025-05-09 16:31:14 +01:00
def delete_user_account(base_url: str, auth: str, stream: str, username: str) -> bool:
2025-07-16 09:19:48 +01:00
"""Deletes a user account via the API."""
2025-05-09 16:31:14 +01:00
payload = {"stream": stream, "user": username}
2025-07-16 09:19:48 +01:00
response = _make_api_request(
"POST", base_url, auth, "deleteAccount", payload=payload
)
return response and "Deleted" in response.text
2025-05-09 16:31:14 +01:00
2025-07-16 09:19:48 +01:00
def add_user_account(
base_url: str, auth: str, username: str, password: str, stream: str
) -> bool:
"""Adds a user account via the API."""
2025-05-09 16:31:14 +01:00
payload = {"username": username, "password": password, "stream": stream}
2025-07-16 09:19:48 +01:00
response = _make_api_request(
"POST", base_url, auth, "addAccount", payload=payload
)
return response and response.status_code == 200
2025-05-09 16:31:14 +01:00
2025-07-05 10:57:19 +01:00
def get_stream_names(base_url: str, auth: str) -> List[str]:
2025-07-16 09:19:48 +01:00
"""Retrieves a list of stream names from the API."""
response = _make_api_request("GET", base_url, auth, "getStreamNames")
return response.json() if response else []