Python-Unifi-Client 1.3.3__py3-none-any.whl → 1.3.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2111,10 +2111,16 @@ class Client:
2111
2111
 
2112
2112
  def generate_backup(self, days: int = -1):
2113
2113
  """Generate a controller backup for the past N days."""
2114
- payload = {'cmd': 'backup', 'days': days}
2115
- result = self.fetch_results(f"/api/s/{self._site}/cmd/backup", payload=payload, proxy_type='network', boolean=False)
2114
+ #payload = {'cmd': 'backup', 'days': days}
2115
+ result = self.fetch_results(f"{self._site}/api/backup/download", proxy_type=None, boolean=False)
2116
2116
  print(f"RAW RESULT FROM FETCH_RESULTS(): {result}") if self.debug else None
2117
2117
  return result
2118
+
2119
+ def test_generate_backup(self):
2120
+ sesh = self._session
2121
+
2122
+ result = sesh.get(f"{self._baseurl}/api/backup/download", verify=False)
2123
+ return result.content
2118
2124
 
2119
2125
  def download_backup(self, filepath: str):
2120
2126
  """Download a generated backup file."""
@@ -2808,7 +2814,7 @@ class Client:
2808
2814
  # clear stored cookie/token
2809
2815
  self.cookies = ''
2810
2816
  self.cookies_created_at = 0
2811
- self.is_logged_in = False
2817
+ self._is_loggedin = False
2812
2818
  self._exec_retries += 1
2813
2819
 
2814
2820
  if self._session:
@@ -2950,4 +2956,118 @@ class Client:
2950
2956
  self.curl_headers = filtered
2951
2957
 
2952
2958
  # 8) Append the new header, using only ASCII
2953
- self.curl_headers.append('x-csrf-token: %s' % csrf_token)
2959
+ self.curl_headers.append('x-csrf-token: %s' % csrf_token)
2960
+
2961
+
2962
+
2963
+ def restore(client:Client, filepath:str):
2964
+ sesh = client._session
2965
+ candidates = [
2966
+ ("backup", "/api/system/backup/restore"),
2967
+ ("file", "/api/system/backup/restore"),
2968
+ ("backup", "/api/v2/system/backup/restore"),
2969
+ ("file", "/api/v2/system/backup/restore"),
2970
+ ]
2971
+ for field, path in candidates:
2972
+ with open(filepath, 'rb') as f:
2973
+ r = sesh.post(f"https://47.22.145.45{path}", files={field: f}, verify=False)
2974
+ print(f"{path} [{field}] → {r.status_code}: {r.text[:120]}")
2975
+ return None
2976
+
2977
+
2978
+ def get_users(client:Client):
2979
+ """Grabs all the users that the site manages"""
2980
+ try:
2981
+ client.login()
2982
+ except UnifiAuthError:
2983
+ return {'msg': 'error', 'error': 'Rate limited - try again later', 'code': 429}
2984
+
2985
+ sesh = client._session
2986
+ url = f'{client._baseurl}/proxy/users/api/v2/users'
2987
+ result = sesh.get(url, verify=False, timeout=30)
2988
+ return result.json()
2989
+
2990
+ def create_new_user(client:Client, first_name: str, last_name: str, user_email: str, is_admin: bool = False,
2991
+ local_account: bool = False, username: str = '', password: str = '',
2992
+ network_permission: str = '', user_permission: str = ''):
2993
+ """
2994
+ Creates new users/admins
2995
+
2996
+ ======PARAMS=======
2997
+
2998
+ is_admin: Boolean - determines if an account is an admin
2999
+ local_account: Boolean - an admin account thats only limited to this local site
3000
+ username: String - only set this if you're using a local account, will be used for the user to log in
3001
+ password: String - password of the local account
3002
+
3003
+ permission: String - The permissions of an admin/local account
3004
+ Available permissions
3005
+ Network - 'admin', 'hotspotoperator', 'readonly', 'none'
3006
+ User Management - 'admin', 'readonly', 'none'
3007
+
3008
+ """
3009
+ try:
3010
+ client.login()
3011
+ except UnifiAuthError:
3012
+ return {'msg': 'error', 'error': 'Rate limited - try again later', 'code': 429}
3013
+
3014
+ if local_account and not is_admin:
3015
+ return {'msg': 'error', 'error': 'Local accounts must also be admin accounts'}
3016
+
3017
+ valid_permissions = [
3018
+ 'admin',
3019
+ 'hotspotoperator',
3020
+ 'readonly',
3021
+ 'none'
3022
+ ]
3023
+
3024
+ sesh = client._session
3025
+
3026
+ url = f'{client._baseurl}/proxy/users/api/v2/user'
3027
+
3028
+ payload = {
3029
+ 'first_name': first_name,
3030
+ 'last_name': last_name,
3031
+ }
3032
+
3033
+ if user_email:
3034
+ payload['user_email'] = user_email
3035
+
3036
+ if is_admin:
3037
+ net_perm = network_permission.lower().strip()
3038
+ user_perm = user_permission.lower().strip()
3039
+
3040
+ if net_perm not in valid_permissions:
3041
+ return {'msg': 'error', 'error': 'Invalid network permission. Must be one of: admin, hotspotoperator, readonly, none'}
3042
+
3043
+ if user_perm not in valid_permissions:
3044
+ return {'msg': 'error', 'error': 'Invalid user permission. Must be one of: admin, readonly, none'}
3045
+
3046
+ if user_perm == 'none' and net_perm == 'none':
3047
+ return {'msg': 'error', 'error': 'Admin users must have at least one permission'}
3048
+
3049
+ role_id = ''
3050
+
3051
+ # Grab the role id from the helper script
3052
+ role = client.get_custom_role(net_perm, user_perm)
3053
+ print(role)
3054
+ if role and role.get('msg') != 'error':
3055
+ role_id = role['unique_id']
3056
+ else:
3057
+ return {'msg': 'error', 'error': 'Unable to fetch role data from UniFi'}
3058
+
3059
+ payload.update({
3060
+ 'email': user_email,
3061
+ 'role_id': role_id
3062
+ })
3063
+
3064
+ if local_account:
3065
+ payload.update({
3066
+ 'username': username,
3067
+ 'password': password,
3068
+ 'only_local_account': True
3069
+ })
3070
+
3071
+ results = sesh.post(url, verify=False, json=payload)
3072
+
3073
+ return results.json()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python_unifi_client
3
- Version: 1.3.3
3
+ Version: 1.3.4
4
4
  Home-page: https://github.com/compdat-llc/unifi-client-python
5
5
  Author: Michael Lapinski
6
6
  Author-email: michaellapinski787@gmail.com
@@ -0,0 +1,6 @@
1
+ python_unifi_client/__init__.py,sha256=frcCV1k9oG9oKj3dpUqdJg1PxRT2RSN_XKdLCPjaYaY,2
2
+ python_unifi_client/client.py,sha256=Wt2aDXW6hkDTBI4f05nTrCQ_PQcclMAZjJfaT94ZmZQ,124870
3
+ python_unifi_client-1.3.4.dist-info/METADATA,sha256=qlc8_4X-czhPbsEwXgiBmUcF6Ip2yLjMj2oKuZSg5rk,2483
4
+ python_unifi_client-1.3.4.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
5
+ python_unifi_client-1.3.4.dist-info/top_level.txt,sha256=675fDdb69Hi9X8pSEGl6f03ZpUXNiumV-s1B1gXEeYs,20
6
+ python_unifi_client-1.3.4.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- python_unifi_client/__init__.py,sha256=frcCV1k9oG9oKj3dpUqdJg1PxRT2RSN_XKdLCPjaYaY,2
2
- python_unifi_client/client.py,sha256=CvimiFw5NEVjk6b675rG36H2AY7MWckGYIolljvOX3I,120859
3
- python_unifi_client-1.3.3.dist-info/METADATA,sha256=_HlAfBlg13fy1cWDn-SOFT40Hspuyph7_vlvdH154mw,2483
4
- python_unifi_client-1.3.3.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
5
- python_unifi_client-1.3.3.dist-info/top_level.txt,sha256=675fDdb69Hi9X8pSEGl6f03ZpUXNiumV-s1B1gXEeYs,20
6
- python_unifi_client-1.3.3.dist-info/RECORD,,