reqs.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import requests
  2. import json
  3. from datetime import datetime
  4. from typing import List, Dict, Any
  5. def get_urls(base_url: str, auth: str) -> List[Dict[str, Any]]:
  6. """Retrieve user account streams from the specified base URL.
  7. Args:
  8. base_url (str): The base URL of the API.
  9. auth (str): The authorization token for accessing the API.
  10. Returns:
  11. List[Dict[str, Any]]: A list of user account streams.
  12. """
  13. url = f"{base_url}/getUserAccounts/streams"
  14. payload = {}
  15. headers = {"Authorization": f"Basic {auth}"}
  16. response = requests.request("GET", url, headers=headers, data=payload)
  17. return json.loads(response.text)
  18. def get_user_accounts(base_url: str, auth: str) -> List[Dict[str, Any]]:
  19. """Retrieve user accounts from the specified base URL.
  20. Args:
  21. base_url (str): The base URL of the API.
  22. auth (str): The authorization token for accessing the API.
  23. Returns:
  24. List[Dict[str, Any]]: A list of user accounts with their expiration dates rendered.
  25. """
  26. url = f"{base_url}/getUserAccounts"
  27. payload = {}
  28. headers = {"Authorization": f"Basic {auth}"}
  29. response = requests.request("GET", url, headers=headers, data=payload)
  30. res_json = json.loads(response.text)
  31. for account in res_json:
  32. account["expiaryDate_rendered"] = datetime.utcfromtimestamp(
  33. account["expiaryDate"]
  34. ).strftime("%d/%m/%Y")
  35. return res_json
  36. def delete_user_account(base_url: str, auth: str, stream: str, username: str) -> bool:
  37. """Delete a user account from the specified base URL.
  38. Args:
  39. base_url (str): The base URL of the API.
  40. auth (str): The authorization token for accessing the API.
  41. stream (str): The name of the stream associated with the user account.
  42. username (str): The username of the account to delete.
  43. Returns:
  44. bool: True if the account was deleted successfully, False otherwise.
  45. """
  46. url = f"{base_url}/deleteAccount"
  47. payload = {"stream": stream, "user": username}
  48. headers = {"Authorization": f"Basic {auth}"}
  49. response = requests.request("POST", url, headers=headers, data=payload)
  50. return "Deleted" in response.text
  51. def add_user_account(base_url: str, auth: str, username: str, password: str, stream: str) -> bool:
  52. """Add a user account to the specified base URL.
  53. Args:
  54. base_url (str): The base URL of the API.
  55. auth (str): The authorization token for accessing the API.
  56. username (str): The username of the account to add.
  57. password (str): The password of the account to add.
  58. stream (str): The name of the stream associated with the user account.
  59. Returns:
  60. bool: True if the account was added successfully, False otherwise.
  61. """
  62. url = f"{base_url}/addAccount"
  63. payload = {"username": username, "password": password, "stream": stream}
  64. headers = {"Authorization": f"Basic {auth}"}
  65. response = requests.request("POST", url, headers=headers, data=payload)
  66. return "Added successfully" in response.text
  67. def get_user_accounts_count(base_url: str, auth: str) -> int:
  68. """Get the count of user accounts from the specified base URL.
  69. Args:
  70. base_url (str): The base URL of the API.
  71. auth (str): The authorization token for accessing the API.
  72. Returns:
  73. int: The count of user accounts.
  74. """
  75. url = f"{base_url}/getUserAccounts/count"
  76. payload = {}
  77. headers = {"Authorization": f"Basic {auth}"}
  78. response = requests.request("GET", url, headers=headers, data=payload)
  79. res_json = json.loads(response.text)
  80. return res_json['count']