add account switch for reseller

get_list_of_videos
Mark Cotton 2023-08-09 14:01:26 -06:00
parent ebb3e74bed
commit 4df8758d88
1 changed files with 103 additions and 1 deletions

View File

@ -1,5 +1,5 @@
""" Python client for Eagle Eye Networks APIv3 """
version = "0.0.8"
version = "0.0.9"
__version__ = version
@ -31,6 +31,7 @@ class EagleEyev3():
self.client_id = None
self.client_secret = None
self.access_token = None
self.reseller_access_token = None # this is the reseller user they switched from
self.refresh_token = None
self.redirect_uri = None
@ -201,6 +202,60 @@ class EagleEyev3():
"data": response_json
}
def login_from_reseller(self, target_account_id=None, cascade=True):
"""
Obtains acces_token for a end-user account.
Args:
code (str): The authorization code.
target_account_id (str): Account ID that you want to get access to.
cascade (bool): Indicates whether to cascade and get the base URL and current user information.
Returns:
dict: Dictionary containing the success status, response HTTP status code, data, and current user information.
"""
url = "https://auth.eagleeyenetworks.com/api/v3.0/authorizationTokens"
headers = {
"Authorization": f"Bearer {self.access_token}",
"Accept": "application/json"
}
payload = {
"type": "reseller",
"targetType": "account",
"targetId": target_account_id,
"scopes": [
"vms.all"
]
}
# Send a POST request to obtain login tokens
response = requests.post(url, headers=headers, json=payload, timeout=self._get_timeout_values('login'))
response_json = response.json()
logging.info(f"{response.status_code} in login_from_reseller")
if response.status_code == 201: # POST is expected to return a 201
success = True
self.reseller_access_token = self.access_token
self.access_token = response_json['accessToken']
if self.lazy_login:
self._save_access_token()
if cascade:
self.get_base_url()
else:
success = False
return {
"success": success,
"response_http_status": response.status_code,
"data": response_json,
'current_user': self.current_user
}
def get_base_url(self, cascade=True):
"""
Obtains the base URL for the user.
@ -351,6 +406,53 @@ class EagleEyev3():
"data": response_json
}
def get_list_of_accounts(self):
"""
Obtains the list of accounts.
Returns:
dict: Dictionary containing the success status, response HTTP status code, and data.
"""
url = f"https://{self.user_base_url}/api/v3.0/accounts"
headers = {
"Authorization": f"Bearer {self.access_token}",
"Accept": "application/json"
}
try:
response = requests.get(url, headers=headers, timeout=self._get_timeout_values('list_of_accounts'))
response_json = response.json()
logging.info(f"{response.status_code} in get_list_of_accounts")
except requests.exceptions.Timeout:
logging.warn(f"timeout expired for get_list_of_accounts()")
return {
"success": False,
"response_http_status": 0,
"data": None
}
except requests.exceptions.RequestException as e:
logging.warn(e)
return {
"success": False,
"response_http_status": 0,
"data": None
}
if response.status_code == 200:
success = True
self.accounts = [i for i in response_json['results']]
else:
success = False
return {
"success": success,
"response_http_status": response.status_code,
"data": response_json
}
def get_list_of_cameras(self):
"""
Obtains the list of cameras.