initial commit

full_example
Mark Cotton 2023-05-18 10:40:02 -05:00
commit 2141731fe5
5 changed files with 177 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
__pycache__
*.pyc
.DS_Store
my_settings.py
*.orig

120
EagleEyev3/__init__.py Normal file
View File

@ -0,0 +1,120 @@
import json
import logging
import requests
from flask import Flask, request
from .settings import *
logging.basicConfig(level=logging.INFO)
class EagleEyev3():
def __init__(self):
self.client_id = None
self.client_secret = None
self.access_token = None
self.refresh_token = None
self.redirect_uri = None
self._load_vars_from_settings()
self.user_base_url = None
self.current_user = None
self.users = []
self.bridges = []
self.cameras = []
self.users = []
self.accounts = []
self.base_url = None
def _load_vars_from_settings(self):
self.client_id = settings.client_id
self.client_secret = settings.client_secret
self.server_host = settings.server_host
self.server_port = settings.server_port
# 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
self.redirect_uri = f"{settings.server_protocol}://{settings.server_host}:{settings.server_port}"
def login_tokens(self, code=None, cascade=True):
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
url = baseUrl + pathUrl
response = requests.post(url, auth=(self.client_id, self.client_secret))
response_json = response.json()
logging.info(f"{response.status_code} in login_tokens")
if response.status_code == 200:
success = True
self.access_token = response_json['access_token']
self.refresh_token = response_json['refresh_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):
url = "https://api.eagleeyenetworks.com/api/v3.0/clientSettings"
headers = {
"Authorization": f"Bearer {self.access_token}",
"Accept": "application/json"
}
response = requests.get(url, headers=headers)
response_json = response.json()
logging.info(f"{response.status_code} in get_base_url")
if response.status_code == 200:
success = True
if 'httpsBaseUrl' in response_json and 'hostname' in response_json['httpsBaseUrl']:
self.user_base_url = response_json['httpsBaseUrl']['hostname']
if cascade:
self.get_current_user()
else:
success = False
return {
"success": success,
"response_http_status": response.status_code,
"data": response_json
}
def get_current_user(self):
url = f"https://{self.user_base_url}/api/v3.0/users/self"
headers = {
"Authorization": f"Bearer {self.access_token}",
"Accept": "application/json"
}
response = requests.get(url, headers=headers)
response_json = response.json()
logging.info(f"{response.status_code} in get_current_user")
if response.status_code == 200:
success = True
self.current_user = response_json
else:
success = False
return {
"success": success,
"response_http_status": response.status_code,
"data": response_json
}

15
EagleEyev3/settings.py Normal file
View File

@ -0,0 +1,15 @@
# Reach out to api_support@een.com in order to get a client_id, client_secret and to have your redirect_uri whitelisted
client_id = ""
client_secret = ""
server_protocol = "http"
server_host = "127.0.0.1" # Do not use 'localhost', use '127.0.0.1' instead
server_port = "3333"
# Note:
# This code is not anticipating a trailing slash at the end of the redirect_uri.
# When you submit your URLs to be whitelisted please do not include the trailing slash
# or you can modify the __init__.py file to append the slash on to the redirect_uri

7
README.md Normal file
View File

@ -0,0 +1,7 @@
# EagleEyev3 #
## Summary ##
This is a python package for working with the Eagle Eye Networks v3 API. It is all about syntatical sugar and making things a little nicer to work with.
## Settings File ##
There is file `settings.py` that is needed to run. Please reach out to api_support@een.com to get the necessary credentials.

30
server.py Normal file
View File

@ -0,0 +1,30 @@
import json, requests
from flask import Flask, request
from EagleEyev3 import *
een = EagleEyev3()
app = Flask(__name__)
@app.route('/')
def index():
# This is getting the ?code= querystring value from the HTTP request.
code = request.args.get('code')
if (code):
oauth_object = een.login_tokens(code)
return f"Hello {een.current_user['firstName']} {een.current_user['lastName']}"
else:
base_url = "https://auth.eagleeyenetworks.com/oauth2/authorize"
path_url = f"?client_id={een.client_id}&response_type=code&scope=vms.all&redirect_uri={een.redirect_uri}"
request_auth_url = f"{base_url}{path_url}"
return "<a href='"+request_auth_url+"'>Login with Eagle Eye Networks</a>"
if __name__ == '__main__':
app.run(host=een.server_host, port=een.server_port)