2020-03-25 01:19:22 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import logging
|
2020-03-25 02:36:05 +01:00
|
|
|
import json
|
2020-03-25 01:19:22 +01:00
|
|
|
from flask import request
|
2020-03-25 02:36:05 +01:00
|
|
|
from flask_restful import Resource, reqparse
|
|
|
|
from werkzeug.datastructures import FileStorage
|
|
|
|
import filetype
|
2020-03-25 02:54:59 +01:00
|
|
|
import db
|
|
|
|
from models import SampleMetadata
|
2020-03-25 01:19:22 +01:00
|
|
|
from schemas import *
|
|
|
|
|
|
|
|
"""
|
|
|
|
Flask Restful endpoints
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = '@tormakris'
|
2020-03-25 02:36:05 +01:00
|
|
|
__copyright__ = "Copyright 2020, Birbnetes Team"
|
2020-03-25 01:19:22 +01:00
|
|
|
__module_name__ = "endpoints"
|
|
|
|
__version__text__ = "1"
|
|
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class SampleResource(Resource):
|
|
|
|
"""
|
|
|
|
Sample endpoint
|
|
|
|
See: https://swagger.kmlabz.com/?urls.primaryName=Input%20Service
|
|
|
|
"""
|
2020-03-25 02:36:05 +01:00
|
|
|
|
2020-03-25 01:19:22 +01:00
|
|
|
def post(self):
|
2020-03-25 02:36:05 +01:00
|
|
|
"""
|
|
|
|
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
|
2020-03-25 02:54:59 +01:00
|
|
|
# TODO: Preserve input data in relational DB
|
|
|
|
# TODO: Pass data and tag to ObjectStorage
|
|
|
|
# TODO: Pass tag to MQ
|