#!/usr/bin/env python3 import logging import json from flask import request from flask_restful import Resource, reqparse from werkzeug.datastructures import FileStorage import filetype from schemas import * """ Flask Restful endpoints """ __author__ = '@tormakris' __copyright__ = "Copyright 2020, Birbnetes Team" __module_name__ = "endpoints" __version__text__ = "1" LOGGER = logging.getLogger(__name__) class SampleResource(Resource): """ Sample endpoint See: https://swagger.kmlabz.com/?urls.primaryName=Input%20Service """ def post(self): """ Post request send to the endpoint :return: """ parse = reqparse.RequestParser() parse.add_argument('soundFile', type=FileStorage, location='soundFile') parse.add_argument('description', type=str, location='description') args = parse.parse_args() soundFile = args['soundFile'] description = args['description'] kind = filetype.guess(soundFile) if kind.mime != 'wav': LOGGER.error( "Input file was not WAV. Recieved metadata: {}", description) return {'status': 'error', 'message': 'Input file not WAV.'}, 415 try: desc = json.loads(description) except Exception as e: LOGGER.exception(e) return {'status': 'error', 'message': 'Input JSON could not be parsed'}, 400 validate_errors = InputSchema().validate(desc) if validate_errors: LOGGER.error( "Input JSON did not conform to schema. It was: {}", desc) return {'status': 'error', 'message': 'Input JSON schema invalid'}, 417