KTVManager_UI/lib/reqs.py

108 lines
3.6 KiB
Python
Raw Normal View History

2024-11-02 20:04:56 +00:00
import requests
import json
from datetime import datetime
from typing import List, Dict, Any
2024-11-02 20:04:56 +00:00
def get_urls(base_url: str, auth: str) -> List[Dict[str, Any]]:
"""Retrieve user account streams from the specified base URL.
Args:
base_url (str): The base URL of the API.
auth (str): The authorization token for accessing the API.
2024-11-02 20:04:56 +00:00
Returns:
List[Dict[str, Any]]: A list of user account streams.
"""
url = f"{base_url}/getUserAccounts/streams"
2024-11-02 20:04:56 +00:00
payload = {}
headers = {"Authorization": f"Basic {auth}"}
response = requests.request("GET", url, headers=headers, data=payload)
return json.loads(response.text)
def get_user_accounts(base_url: str, auth: str) -> List[Dict[str, Any]]:
"""Retrieve user accounts from the specified base URL.
Args:
base_url (str): The base URL of the API.
auth (str): The authorization token for accessing the API.
2024-11-02 20:04:56 +00:00
Returns:
List[Dict[str, Any]]: A list of user accounts with their expiration dates rendered.
"""
url = f"{base_url}/getUserAccounts"
2024-11-02 20:04:56 +00:00
payload = {}
headers = {"Authorization": f"Basic {auth}"}
response = requests.request("GET", url, headers=headers, data=payload)
res_json = json.loads(response.text)
2024-11-02 20:04:56 +00:00
for account in res_json:
2024-11-03 15:34:37 +00:00
account["expiaryDate_rendered"] = datetime.utcfromtimestamp(
account["expiaryDate"]
).strftime("%d/%m/%Y")
2024-11-03 15:34:37 +00:00
return res_json
def delete_user_account(base_url: str, auth: str, stream: str, username: str) -> bool:
"""Delete a user account from the specified base URL.
2024-11-03 15:34:37 +00:00
Args:
base_url (str): The base URL of the API.
auth (str): The authorization token for accessing the API.
stream (str): The name of the stream associated with the user account.
username (str): The username of the account to delete.
2024-11-03 15:34:37 +00:00
Returns:
bool: True if the account was deleted successfully, False otherwise.
"""
2024-11-04 16:39:02 +00:00
url = f"{base_url}/deleteAccount"
2024-11-03 15:34:37 +00:00
payload = {"stream": stream, "user": username}
headers = {"Authorization": f"Basic {auth}"}
response = requests.request("POST", url, headers=headers, data=payload)
return "Deleted" in response.text
2024-11-04 16:39:02 +00:00
def add_user_account(base_url: str, auth: str, username: str, password: str, stream: str) -> bool:
"""Add a user account to the specified base URL.
2024-11-04 16:39:02 +00:00
Args:
base_url (str): The base URL of the API.
auth (str): The authorization token for accessing the API.
username (str): The username of the account to add.
password (str): The password of the account to add.
stream (str): The name of the stream associated with the user account.
2024-11-04 16:39:02 +00:00
Returns:
bool: True if the account was added successfully, False otherwise.
"""
2024-11-04 16:39:02 +00:00
url = f"{base_url}/addAccount"
payload = {"username": username, "password": password, "stream": stream}
headers = {"Authorization": f"Basic {auth}"}
response = requests.request("POST", url, headers=headers, data=payload)
return "Added successfully" in response.text
def get_user_accounts_count(base_url: str, auth: str) -> int:
"""Get the count of user accounts from the specified base URL.
Args:
base_url (str): The base URL of the API.
auth (str): The authorization token for accessing the API.
Returns:
int: The count of user accounts.
"""
url = f"{base_url}/getUserAccounts/count"
payload = {}
headers = {"Authorization": f"Basic {auth}"}
response = requests.request("GET", url, headers=headers, data=payload)
res_json = json.loads(response.text)
return res_json['count']