rework api calls
This commit is contained in:
parent
31ef25ead3
commit
37cefe3422
137
lib/reqs.py
137
lib/reqs.py
@ -1,105 +1,84 @@
|
|||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import List, Dict, Any
|
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.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
method: The HTTP method to use (e.g., 'GET', 'POST').
|
||||||
|
base_url: The base URL of the API.
|
||||||
|
auth: The authorization token.
|
||||||
|
endpoint: The API endpoint to call.
|
||||||
|
payload: The data to send with the request.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The JSON response from the API.
|
||||||
|
"""
|
||||||
|
url = f"{base_url}/{endpoint}"
|
||||||
|
headers = {"Authorization": f"Basic {auth}"}
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
def get_urls(base_url: str, auth: str) -> List[Dict[str, Any]]:
|
def get_urls(base_url: str, auth: str) -> List[Dict[str, Any]]:
|
||||||
"""Retrieves user account streams from the API.
|
"""Retrieves user account streams from the API."""
|
||||||
|
response = _make_api_request("GET", base_url, auth, "getUserAccounts/streams")
|
||||||
Args:
|
return response.json() if response else []
|
||||||
base_url: The base URL of the API.
|
|
||||||
auth: The authorization token.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
A list of user account streams.
|
|
||||||
"""
|
|
||||||
url = f"{base_url}/getUserAccounts/streams"
|
|
||||||
headers = {"Authorization": f"Basic {auth}"}
|
|
||||||
response = requests.get(url, headers=headers)
|
|
||||||
response.raise_for_status()
|
|
||||||
return response.json()
|
|
||||||
|
|
||||||
|
|
||||||
def get_user_accounts(base_url: str, auth: str) -> List[Dict[str, Any]]:
|
def get_user_accounts(base_url: str, auth: str) -> List[Dict[str, Any]]:
|
||||||
"""Retrieves user accounts from the API.
|
"""Retrieves user accounts from the API."""
|
||||||
|
response = _make_api_request("GET", base_url, auth, "getUserAccounts")
|
||||||
|
if not response:
|
||||||
|
return []
|
||||||
|
|
||||||
Args:
|
|
||||||
base_url: The base URL of the API.
|
|
||||||
auth: The authorization token.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
A list of user accounts with 'expiaryDate_rendered' added.
|
|
||||||
"""
|
|
||||||
url = f"{base_url}/getUserAccounts"
|
|
||||||
headers = {"Authorization": f"Basic {auth}"}
|
|
||||||
response = requests.get(url, headers=headers)
|
|
||||||
response.raise_for_status()
|
|
||||||
accounts = response.json()
|
accounts = response.json()
|
||||||
|
|
||||||
for account in accounts:
|
for account in accounts:
|
||||||
account["expiaryDate_rendered"] = datetime.utcfromtimestamp(
|
account["expiaryDate_rendered"] = datetime.utcfromtimestamp(
|
||||||
account["expiaryDate"]
|
account["expiaryDate"]
|
||||||
).strftime("%d/%m/%Y")
|
).strftime("%d/%m/%Y")
|
||||||
|
|
||||||
return accounts
|
return accounts
|
||||||
|
|
||||||
|
|
||||||
def delete_user_account(base_url: str, auth: str, stream: str, username: str) -> bool:
|
def delete_user_account(base_url: str, auth: str, stream: str, username: str) -> bool:
|
||||||
"""Deletes a user account via the API.
|
"""Deletes a user account via the API."""
|
||||||
|
|
||||||
Args:
|
|
||||||
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.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
True if the account was deleted successfully, False otherwise.
|
|
||||||
"""
|
|
||||||
url = f"{base_url}/deleteAccount"
|
|
||||||
payload = {"stream": stream, "user": username}
|
payload = {"stream": stream, "user": username}
|
||||||
headers = {"Authorization": f"Basic {auth}"}
|
response = _make_api_request(
|
||||||
response = requests.post(url, headers=headers, data=payload)
|
"POST", base_url, auth, "deleteAccount", payload=payload
|
||||||
response.raise_for_status()
|
)
|
||||||
return "Deleted" in response.text
|
return response and "Deleted" in response.text
|
||||||
|
|
||||||
|
|
||||||
def add_user_account(base_url: str, auth: str, username: str, password: str, stream: str) -> bool:
|
def add_user_account(
|
||||||
"""Adds a user account via the API.
|
base_url: str, auth: str, username: str, password: str, stream: str
|
||||||
|
) -> bool:
|
||||||
Args:
|
"""Adds a user account via the API."""
|
||||||
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.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
True if the account was added successfully, False otherwise.
|
|
||||||
"""
|
|
||||||
url = f"{base_url}/addAccount"
|
|
||||||
payload = {"username": username, "password": password, "stream": stream}
|
payload = {"username": username, "password": password, "stream": stream}
|
||||||
headers = {"Authorization": f"Basic {auth}"}
|
response = _make_api_request(
|
||||||
response = requests.post(url, headers=headers, data=payload)
|
"POST", base_url, auth, "addAccount", payload=payload
|
||||||
return response.status_code == 200
|
)
|
||||||
|
return response and response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
def get_stream_names(base_url: str, auth: str) -> List[str]:
|
def get_stream_names(base_url: str, auth: str) -> List[str]:
|
||||||
"""Retrieves a list of stream names from the API.
|
"""Retrieves a list of stream names from the API."""
|
||||||
|
response = _make_api_request("GET", base_url, auth, "getStreamNames")
|
||||||
Args:
|
return response.json() if response else []
|
||||||
base_url: The base URL of the API.
|
|
||||||
auth: The authorization token.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
A list of stream names, or an empty list if an error occurs.
|
|
||||||
"""
|
|
||||||
url = f"{base_url}/getStreamNames"
|
|
||||||
headers = {"Authorization": f"Basic {auth}"}
|
|
||||||
try:
|
|
||||||
response = requests.get(url, headers=headers)
|
|
||||||
response.raise_for_status()
|
|
||||||
return response.json()
|
|
||||||
except (requests.exceptions.RequestException, json.JSONDecodeError):
|
|
||||||
return []
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user