From 536ac4227f35a0117956d2a3bff2169dc2de8c5f Mon Sep 17 00:00:00 2001 From: Mark Cotton Date: Wed, 9 Aug 2023 14:01:26 -0600 Subject: [PATCH] add account switch for reseller, published as 0.0.9 --- src/EagleEyev3/__init__.py | 104 ++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/src/EagleEyev3/__init__.py b/src/EagleEyev3/__init__.py index 293670b..ff8dc05 100644 --- a/src/EagleEyev3/__init__.py +++ b/src/EagleEyev3/__init__.py @@ -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.