initial push
This commit is contained in:
1
echo-master/echo_api/__init__.py
Normal file
1
echo-master/echo_api/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
BIN
echo-master/echo_api/__init__.pyc
Normal file
BIN
echo-master/echo_api/__init__.pyc
Normal file
Binary file not shown.
BIN
echo-master/echo_api/__pycache__/__init__.cpython-37.pyc
Normal file
BIN
echo-master/echo_api/__pycache__/__init__.cpython-37.pyc
Normal file
Binary file not shown.
BIN
echo-master/echo_api/__pycache__/__init__.cpython-38.pyc
Normal file
BIN
echo-master/echo_api/__pycache__/__init__.cpython-38.pyc
Normal file
Binary file not shown.
BIN
echo-master/echo_api/__pycache__/api.cpython-37.pyc
Normal file
BIN
echo-master/echo_api/__pycache__/api.cpython-37.pyc
Normal file
Binary file not shown.
BIN
echo-master/echo_api/__pycache__/api.cpython-38.pyc
Normal file
BIN
echo-master/echo_api/__pycache__/api.cpython-38.pyc
Normal file
Binary file not shown.
BIN
echo-master/echo_api/__pycache__/auth.cpython-37.pyc
Normal file
BIN
echo-master/echo_api/__pycache__/auth.cpython-37.pyc
Normal file
Binary file not shown.
BIN
echo-master/echo_api/__pycache__/auth.cpython-38.pyc
Normal file
BIN
echo-master/echo_api/__pycache__/auth.cpython-38.pyc
Normal file
Binary file not shown.
BIN
echo-master/echo_api/__pycache__/device.cpython-37.pyc
Normal file
BIN
echo-master/echo_api/__pycache__/device.cpython-37.pyc
Normal file
Binary file not shown.
BIN
echo-master/echo_api/__pycache__/device.cpython-38.pyc
Normal file
BIN
echo-master/echo_api/__pycache__/device.cpython-38.pyc
Normal file
Binary file not shown.
BIN
echo-master/echo_api/__pycache__/devices.cpython-37.pyc
Normal file
BIN
echo-master/echo_api/__pycache__/devices.cpython-37.pyc
Normal file
Binary file not shown.
BIN
echo-master/echo_api/__pycache__/devices.cpython-38.pyc
Normal file
BIN
echo-master/echo_api/__pycache__/devices.cpython-38.pyc
Normal file
Binary file not shown.
BIN
echo-master/echo_api/__pycache__/devices_endpoint.cpython-37.pyc
Normal file
BIN
echo-master/echo_api/__pycache__/devices_endpoint.cpython-37.pyc
Normal file
Binary file not shown.
43
echo-master/echo_api/api.py
Normal file
43
echo-master/echo_api/api.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import flask
|
||||
|
||||
from flask import request, abort, Flask, g
|
||||
from flask_restful import Resource, Api
|
||||
from flask_cors import CORS, cross_origin
|
||||
|
||||
import echo_api.auth as Auth
|
||||
|
||||
# Endpoints
|
||||
import echo_api.devices as DevicesAPI
|
||||
import echo_api.device as DeviceInfoAPI
|
||||
import echo_api.device as DeviceControlAPI
|
||||
|
||||
class EchoAPI:
|
||||
|
||||
# Initialize Flask API Object
|
||||
app = flask.Flask(__name__)
|
||||
cors = CORS(app)
|
||||
app.config['CORS_HEADERS'] = 'Content-Type'
|
||||
api = Api(app)
|
||||
|
||||
# Start API
|
||||
@staticmethod
|
||||
def init():
|
||||
EchoAPI.app.run(host='0.0.0.0', port=1337, debug=False)
|
||||
|
||||
@staticmethod
|
||||
@app.route('/')
|
||||
def index():
|
||||
if not Auth.Auth.verify(request):
|
||||
return {'status': 'failed', 'message': 'Not Authenticated!'}, 401
|
||||
|
||||
return {'status': 'success', 'message': 'Echo API is healthy and running!'}, 200
|
||||
|
||||
@staticmethod
|
||||
@app.errorhandler(404)
|
||||
def not_found(error):
|
||||
return {'status': 'failed', 'message': 'Endpoint not found!'}, 404
|
||||
|
||||
api.add_resource(DevicesAPI.DevicesAPI, '/devices')
|
||||
api.add_resource(DeviceInfoAPI.DeviceInfoAPI, '/device/<string:uuid>')
|
||||
api.add_resource(DeviceControlAPI.DeviceControlAPI, '/device')
|
||||
|
BIN
echo-master/echo_api/api.pyc
Normal file
BIN
echo-master/echo_api/api.pyc
Normal file
Binary file not shown.
14
echo-master/echo_api/auth.py
Normal file
14
echo-master/echo_api/auth.py
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
class Auth:
|
||||
|
||||
tokens = {
|
||||
'Bearer f8c85565-ece7-471e-999f-9bb6a7b61a4c': 'Nick Leeman Development'
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def verify(req):
|
||||
token = req.headers.get('Authorization')
|
||||
if token in Auth.tokens:
|
||||
return True
|
||||
else:
|
||||
return False
|
56
echo-master/echo_api/device.py
Normal file
56
echo-master/echo_api/device.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from flask_restful import Resource, Api, reqparse
|
||||
from flask import request
|
||||
from bson import json_util, ObjectId
|
||||
import json
|
||||
|
||||
import echo_db.database as EchoDB
|
||||
import echo_api.auth as Auth
|
||||
import echo_controller.controller as Controller
|
||||
|
||||
class DeviceInfoAPI(Resource):
|
||||
|
||||
def get(self, uuid):
|
||||
# Verify Auth
|
||||
if not Auth.Auth.verify(request):
|
||||
return {'status': 'failed', 'message': 'Not Authenticated!'}, 401
|
||||
|
||||
# Get Device
|
||||
collection = EchoDB.EchoDB.database['Devices']
|
||||
device = collection.find_one({"uuid": uuid})
|
||||
|
||||
# Check if Device Exists
|
||||
if device == None:
|
||||
return {'status': 'failed', 'message': 'Device not found!', 'data': {}}, 404
|
||||
|
||||
# Format and respond
|
||||
device_json = json.loads(json_util.dumps(device))
|
||||
return {'status': 'success', 'data': device_json}, 200
|
||||
|
||||
class DeviceControlAPI(Resource):
|
||||
def post(self):
|
||||
if not Auth.Auth.verify(request):
|
||||
return {'status': 'failed', 'message': 'Not Authenticated!'}, 401
|
||||
|
||||
parser = reqparse.RequestParser()
|
||||
parser.add_argument('uuid', required = True)
|
||||
parser.add_argument('action', required = True)
|
||||
parser.add_argument('data', required = False, default = {})
|
||||
|
||||
# Parse the arguments into an object
|
||||
args = parser.parse_args()
|
||||
|
||||
# Get Device
|
||||
collection = EchoDB.EchoDB.database['Devices']
|
||||
device = collection.find_one({'uuid': args['uuid']})
|
||||
|
||||
# Check if Device Exists
|
||||
if device == None:
|
||||
return {'status': 'failed', 'message': 'Device not found!'}, 200
|
||||
|
||||
if args['action'] not in device['actions']:
|
||||
return {'status': 'failed', 'message': 'Action not found!'}, 200
|
||||
|
||||
# Send Action to Echo Controller
|
||||
return Controller.Controller.handle_action(device, args['action'], args['data'])
|
||||
|
||||
|
18
echo-master/echo_api/devices.py
Normal file
18
echo-master/echo_api/devices.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from flask_restful import Resource, Api, reqparse
|
||||
from flask import request
|
||||
from bson import json_util, ObjectId
|
||||
import json
|
||||
|
||||
import echo_db.database as EchoDB
|
||||
import echo_api.auth as Auth
|
||||
|
||||
class DevicesAPI(Resource):
|
||||
|
||||
def get(self):
|
||||
if not Auth.Auth.verify(request):
|
||||
return {'status': 'failed', 'message': 'Not Authenticated!'}, 401
|
||||
|
||||
collection = EchoDB.EchoDB.database['Devices']
|
||||
devices = collection.find({})
|
||||
devices_json = json.loads(json_util.dumps(devices))
|
||||
return {'status': 'success', 'data': devices_json}, 200
|
Reference in New Issue
Block a user