input-service/src/resources.py

120 lines
3.4 KiB
Python
Raw Normal View History

2020-03-25 01:19:22 +01:00
#!/usr/bin/env python3
import logging
2020-03-31 17:32:04 +02:00
import json
2020-03-31 16:51:31 +02:00
from xeger import Xeger
from flask_restful import Resource
2020-04-27 12:42:46 +02:00
from flask import request
2020-03-31 14:46:46 +02:00
import requests
2020-03-31 15:10:58 +02:00
from db import db
2020-03-25 02:54:59 +01:00
from models import SampleMetadata
2020-03-31 16:51:31 +02:00
from rabbitmqqueue import rabbitmq_channel
2020-03-31 17:32:04 +02:00
from schemas import SampleSchema
2020-03-31 14:46:46 +02:00
from config import *
2020-03-25 01:19:22 +01:00
"""
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__)
2020-03-31 15:10:58 +02:00
2020-03-25 01:19:22 +01:00
class SampleResource(Resource):
"""
Sample endpoint
See: https://swagger.kmlabz.com/?urls.primaryName=Input%20Service
"""
2020-04-27 12:42:46 +02:00
samplemetadatas_schema = SampleMetadata(many=True,
exclude=['id', 'device_id', 'device_date', 'tag', 'timestamp'])
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:
"""
2020-03-31 16:51:31 +02:00
if 'file' not in request.files:
2020-04-13 00:47:05 +02:00
return {"err_msg": "no file found"}, 469
2020-03-31 16:51:31 +02:00
else:
soundfile = request.files['file']
2020-03-31 17:32:04 +02:00
if 'description' not in request.form:
2020-04-13 00:47:05 +02:00
return {"err_msg": "no description found"}, 470
2020-03-31 16:51:31 +02:00
else:
2020-03-31 17:32:04 +02:00
description = request.form.get("description")
2020-03-31 16:51:31 +02:00
2020-03-31 17:32:04 +02:00
if soundfile.content_type != 'audio/wave':
LOGGER.info(
f"Input file was not WAV.")
2020-04-13 00:47:05 +02:00
return {'err_msg': 'Input file not a wave file.'}, 415
2020-03-31 17:32:04 +02:00
try:
desc = json.loads(description)
except Exception as e:
LOGGER.exception(e)
2020-04-13 00:47:05 +02:00
return {'err_msg': 'Input JSON could not be parsed'}, 400
2020-03-31 16:51:31 +02:00
2020-03-31 17:32:04 +02:00
validate_errors = SampleSchema().validate(desc)
if validate_errors:
2020-03-25 02:36:05 +01:00
LOGGER.error(
2020-04-27 12:42:46 +02:00
"Input JSON did not conform to schema. It was: {}".format(desc))
2020-04-13 00:47:05 +02:00
return {'err_msg': 'Input JSON schema invalid'}, 417
2020-03-31 16:51:31 +02:00
2020-03-31 17:32:04 +02:00
xeger = Xeger(limit=30)
2020-03-31 16:51:31 +02:00
generated_tag = xeger.xeger(r'^[a-zA-Z]+[0-9a-zA-Z_]*$')
2020-03-25 03:57:07 +01:00
2020-03-31 14:46:46 +02:00
record = SampleMetadata(
2020-03-31 17:32:04 +02:00
device_id=desc['device_id'],
device_date=desc['date'],
2020-03-31 14:46:46 +02:00
tag=generated_tag)
try:
db.session.add(record)
requests.post(
2020-03-31 17:32:04 +02:00
f"http://{STORAGE_HOSTNAME}/object",
2020-03-31 14:46:46 +02:00
files={
'tag': (None, generated_tag),
'file': (
'wave.wav',
2020-03-31 16:51:31 +02:00
soundfile,
2020-03-31 17:32:04 +02:00
soundfile.content_type)})
2020-03-31 14:46:46 +02:00
rabbitmq_channel.basic_publish(
exchange=RABBITMQ_EXCHANGE,
routing_key='feature',
body=generated_tag)
except Exception as e:
LOGGER.exception(e)
db.session.rollback()
2020-04-13 00:47:05 +02:00
return {"err_msg": str(
2020-03-31 14:46:46 +02:00
e), "hint": "DB or downstream service error"}, 569
2020-03-25 03:57:07 +01:00
2020-03-31 14:46:46 +02:00
db.session.commit()
2020-04-13 00:47:05 +02:00
return {"tag": generated_tag}, 200
2020-03-31 15:10:58 +02:00
def get(self):
"""
Get all stored items
:return:
"""
2020-03-31 16:51:31 +02:00
samples = SampleMetadata.query.all()
2020-04-13 00:47:05 +02:00
return list(samples), 200
2020-03-31 15:10:58 +02:00
class SampleParameterResource(Resource):
"""
Sample endpoint with parameters
"""
2020-04-27 12:42:46 +02:00
samplemetadata_schema = SampleMetadata(many=False)
2020-03-31 15:10:58 +02:00
def get(self, tag: str):
"""
Get a specific item
:param tag:
:return:
"""
sample = SampleMetadata.query.filter_by(tag=tag).first_or_404()
2020-04-27 12:42:46 +02:00
return self.samplemetadata_schema.dump(sample), 200