KTVManager_UI/lib/reqs.py

106 lines
3.4 KiB
Python
Raw Normal View History

2025-05-09 16:31:14 +01:00
import requests
import json
from datetime import datetime
from typing import List, Dict, Any
def get_urls(base_url: str, auth: str) -> List[Dict[str, Any]]:
2025-07-15 15:44:19 +01:00
"""Retrieves user account streams from the API.
2025-05-09 16:31:14 +01:00
Args:
2025-07-15 15:44:19 +01:00
base_url: The base URL of the API.
auth: The authorization token.
2025-05-09 16:31:14 +01:00
Returns:
2025-07-15 15:44:19 +01:00
A list of user account streams.
2025-05-09 16:31:14 +01:00
"""
url = f"{base_url}/getUserAccounts/streams"
headers = {"Authorization": f"Basic {auth}"}
2025-07-15 15:44:19 +01:00
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
2025-05-09 16:31:14 +01:00
def get_user_accounts(base_url: str, auth: str) -> List[Dict[str, Any]]:
2025-07-15 15:44:19 +01:00
"""Retrieves user accounts from the API.
2025-05-09 16:31:14 +01:00
Args:
2025-07-15 15:44:19 +01:00
base_url: The base URL of the API.
auth: The authorization token.
2025-05-09 16:31:14 +01:00
Returns:
2025-07-15 15:44:19 +01:00
A list of user accounts with 'expiaryDate_rendered' added.
2025-05-09 16:31:14 +01:00
"""
url = f"{base_url}/getUserAccounts"
headers = {"Authorization": f"Basic {auth}"}
2025-07-15 15:44:19 +01:00
response = requests.get(url, headers=headers)
response.raise_for_status()
accounts = response.json()
2025-05-09 16:31:14 +01:00
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-15 15:44:19 +01:00
"""Deletes a user account via the API.
2025-05-09 16:31:14 +01:00
Args:
2025-07-15 15:44:19 +01:00
base_url: The base URL of the API.
auth: The authorization token.
stream: The stream associated with the account.
username: The username of the account to delete.
2025-05-09 16:31:14 +01:00
Returns:
2025-07-15 15:44:19 +01:00
True if the account was deleted successfully, False otherwise.
2025-05-09 16:31:14 +01:00
"""
url = f"{base_url}/deleteAccount"
payload = {"stream": stream, "user": username}
headers = {"Authorization": f"Basic {auth}"}
2025-07-15 15:44:19 +01:00
response = requests.post(url, headers=headers, data=payload)
response.raise_for_status()
2025-05-09 16:31:14 +01:00
return "Deleted" in response.text
def add_user_account(base_url: str, auth: str, username: str, password: str, stream: str) -> bool:
2025-07-15 15:44:19 +01:00
"""Adds a user account via the API.
2025-05-09 16:31:14 +01:00
Args:
2025-07-15 15:44:19 +01:00
base_url: The base URL of the API.
auth: The authorization token.
username: The username of the new account.
password: The password for the new account.
stream: The stream to associate with the new account.
2025-05-09 16:31:14 +01:00
Returns:
2025-07-15 15:44:19 +01:00
True if the account was added successfully, False otherwise.
2025-05-09 16:31:14 +01:00
"""
url = f"{base_url}/addAccount"
payload = {"username": username, "password": password, "stream": stream}
headers = {"Authorization": f"Basic {auth}"}
2025-07-15 15:44:19 +01:00
response = requests.post(url, headers=headers, data=payload)
2025-07-14 20:09:17 +01:00
return 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-15 15:44:19 +01:00
"""Retrieves a list of stream names from the API.
2025-07-05 10:57:19 +01:00
Args:
2025-07-15 15:44:19 +01:00
base_url: The base URL of the API.
auth: The authorization token.
2025-07-05 10:57:19 +01:00
Returns:
2025-07-15 15:44:19 +01:00
A list of stream names, or an empty list if an error occurs.
2025-07-05 10:57:19 +01:00
"""
url = f"{base_url}/getStreamNames"
headers = {"Authorization": f"Basic {auth}"}
2025-07-15 15:44:19 +01:00
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
except (requests.exceptions.RequestException, json.JSONDecodeError):
return []