added robot generated comments

full_example
Mark Cotton 2023-05-18 10:43:13 -05:00
parent 2141731fe5
commit a091b93e1f
1 changed files with 31 additions and 11 deletions

View File

@ -1,4 +1,3 @@
import json import json
import logging import logging
import requests import requests
@ -8,7 +7,6 @@ from .settings import *
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
class EagleEyev3(): class EagleEyev3():
def __init__(self): def __init__(self):
self.client_id = None self.client_id = None
self.client_secret = None self.client_secret = None
@ -28,24 +26,32 @@ class EagleEyev3():
self.base_url = None self.base_url = None
def _load_vars_from_settings(self): def _load_vars_from_settings(self):
"""Load variables from the settings module."""
self.client_id = settings.client_id self.client_id = settings.client_id
self.client_secret = settings.client_secret self.client_secret = settings.client_secret
self.server_host = settings.server_host self.server_host = settings.server_host
self.server_port = settings.server_port self.server_port = settings.server_port
# combine server_protocol, server_host, and server_port to make the redirect_uri # Combine server_protocol, server_host, and server_port to make the redirect_uri
# please see note in settings.py about trailing slashes and modify this line if needed # Note: Please see the note in settings.py about trailing slashes and modify this line if needed
self.redirect_uri = f"{settings.server_protocol}://{settings.server_host}:{settings.server_port}" self.redirect_uri = f"{settings.server_protocol}://{settings.server_host}:{settings.server_port}"
def login_tokens(self, code=None, cascade=True): def login_tokens(self, code=None, cascade=True):
"""Obtains login tokens using the authorization code.
Args:
code (str): The authorization code.
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.
"""
baseUrl = "https://auth.eagleeyenetworks.com/oauth2/token" baseUrl = "https://auth.eagleeyenetworks.com/oauth2/token"
pathUrl = f"?grant_type=authorization_code&scope=vms.all&code={code}&redirect_uri={self.redirect_uri}" # note the trailing slash, make sure it matches the whitelist pathUrl = f"?grant_type=authorization_code&scope=vms.all&code={code}&redirect_uri={self.redirect_uri}" # Note the trailing slash, make sure it matches the whitelist
url = baseUrl + pathUrl url = baseUrl + pathUrl
# Send a POST request to obtain login tokens
response = requests.post(url, auth=(self.client_id, self.client_secret)) response = requests.post(url, auth=(self.client_id, self.client_secret))
response_json = response.json() response_json = response.json()
@ -68,12 +74,21 @@ class EagleEyev3():
} }
def get_base_url(self, cascade=True): def get_base_url(self, cascade=True):
"""Obtains the base URL for the user.
Args:
cascade (bool): Indicates whether to cascade and get the current user information.
Returns:
dict: Dictionary containing the success status, response HTTP status code, and data.
"""
url = "https://api.eagleeyenetworks.com/api/v3.0/clientSettings" url = "https://api.eagleeyenetworks.com/api/v3.0/clientSettings"
headers = { headers = {
"Authorization": f"Bearer {self.access_token}", "Authorization": f"Bearer {self.access_token}",
"Accept": "application/json" "Accept": "application/json"
} }
# Send a GET request to obtain the base URL
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
response_json = response.json() response_json = response.json()
@ -95,12 +110,18 @@ class EagleEyev3():
} }
def get_current_user(self): def get_current_user(self):
"""Obtains the information of the current user.
Returns:
dict: Dictionary containing the success status, response HTTP status code, and data.
"""
url = f"https://{self.user_base_url}/api/v3.0/users/self" url = f"https://{self.user_base_url}/api/v3.0/users/self"
headers = { headers = {
"Authorization": f"Bearer {self.access_token}", "Authorization": f"Bearer {self.access_token}",
"Accept": "application/json" "Accept": "application/json"
} }
# Send a GET request to obtain the current user information
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
response_json = response.json() response_json = response.json()
@ -117,4 +138,3 @@ class EagleEyev3():
"response_http_status": response.status_code, "response_http_status": response.status_code,
"data": response_json "data": response_json
} }