From 859a5d880a16c4f0018a4b210092a6a594fd8aae Mon Sep 17 00:00:00 2001 From: Tobias Kurze Date: Tue, 13 Aug 2019 16:30:45 +0200 Subject: [PATCH] working on control --- api/__init__.py | 5 +++++ api/control_api.py | 40 ++++++++++++++++++++++++++++++++++++++ api/virtual_command_api.py | 17 +++++++++++----- serve_frontend.py | 1 + 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 api/control_api.py diff --git a/api/__init__.py b/api/__init__.py index 439d94e..8f55aec 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -33,6 +33,8 @@ api_virtual_command = Namespace('virtual_command', description="Virtual command authorizations=api_authorizations) api_cron_job = Namespace('cron_job', description="Cron job namespace", authorizations=api_authorizations) +api_control = Namespace('control', description="Control namespace", + authorizations=api_authorizations) api_v1.add_namespace(api_user) api_v1.add_namespace(api_group) @@ -40,6 +42,7 @@ api_v1.add_namespace(api_room) api_v1.add_namespace(api_recorder) api_v1.add_namespace(api_virtual_command) api_v1.add_namespace(api_cron_job) +api_v1.add_namespace(api_control) auth_api_bp = Blueprint('auth_api', __name__, url_prefix='/api/auth') # user_api_bp = Blueprint('user_api', __name__, url_prefix='/api/user') @@ -51,6 +54,8 @@ from .user_api import * from .group_api import * from .room_api import * from .recorder_api import * +from .control_api import * +from .virtual_command_api import * # from .group_api import * diff --git a/api/control_api.py b/api/control_api.py new file mode 100644 index 0000000..5cd259e --- /dev/null +++ b/api/control_api.py @@ -0,0 +1,40 @@ +# Copyright (c) 2019. Tobias Kurze +""" +This module provides functions related to authentication through the API. +For example: listing of available auth providers or registration of users. + +Login through API does not start a new session, but instead returns JWT. +""" +import json +from datetime import datetime + +from flask_jwt_extended import jwt_required +from flask_restplus import fields, Resource + +from backend import db +from backend.api import api_control, get_jwt_identity + +control_command_response_model = api_control.model('Control Command Response', { + 'time': fields.DateTime(required=False, description='Creation date of the recorder'), + 'state': fields.String(min_length=3, required=True, description='The recorder\'s name'), + 'output': fields.String(required=False, description='The recorder\'s description'), + 'error': fields.String(required=False, description='The recorder\'s description'), +}) + + +@api_control.route('') +class ControlCommand(Resource): + control_command_parser = api_control.parser() + control_command_parser.add_argument('recorder_id', type=int, default=1, required=True) + control_command_parser.add_argument('command_id', type=int, default=1, required=True) + control_command_parser.add_argument('parameters', default=json.dumps({'p1': 'v1'}), type=dict, required=False, + location='json') + + @jwt_required + @api_control.doc('run_command') + @api_control.expect(control_command_parser) + @api_control.marshal_with(control_command_response_model, skip_none=False, code=201) + def post(self): + current_user = {'identity': get_jwt_identity(), 'type': type(get_jwt_identity())} + args = self.control_command_parser.parse_args() + return {'time': datetime.utcnow(), 'output': args, 'state': current_user} diff --git a/api/virtual_command_api.py b/api/virtual_command_api.py index 68f7698..f7100aa 100644 --- a/api/virtual_command_api.py +++ b/api/virtual_command_api.py @@ -24,11 +24,18 @@ virtual_command_model = api_virtual_command.model('VirtualCommand', { 'name': fields.String(min_length=3, required=True, description='The recorder\'s name'), 'description': fields.String(required=False, description='The recorder\'s description'), - 'parent_virtual_command': fields.Nested('virtual_command_model', - required=False, - allow_null=True, - skip_none=False, - description='Parent virtual command.'), + 'parent_virtual_command': fields.Nested(api_virtual_command.model('VirtualCommandParent', + { + 'id': fields.String(required=False, + description='The recorder\'s identifier'), + 'name': fields.String(min_length=3, + required=True, + description='The recorder\'s name'), + }, + required=False, + allow_null=True, + skip_none=False, + description='Parent virtual command.')), 'room': fields.Nested(api_virtual_command.model('recorder_room', {'id': fields.Integer(), 'name': fields.String(), 'number': fields.String(), 'alternate_name': fields.String()}), diff --git a/serve_frontend.py b/serve_frontend.py index 36e73ba..dd1d12e 100644 --- a/serve_frontend.py +++ b/serve_frontend.py @@ -56,6 +56,7 @@ def has_no_empty_params(rule): @fe_bp.route("/site-map") def site_map(): + print("# serving site-map!!") links = [] for rule in app.url_map.iter_rules(): # Filter out rules we can't navigate to in a browser