input-service/src/resources.py

109 lines
3.0 KiB
Python
Raw Normal View History

2020-03-25 01:19:22 +01:00
#!/usr/bin/env python3
import logging
2020-03-31 16:51:31 +02:00
from xeger import Xeger
from flask_restful import Resource
from flask import request
2020-03-31 14:46:46 +02:00
import requests
2020-03-25 02:36:05 +01:00
import filetype
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 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-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:
return {"status": "error", "message": "no file found"}, 469
else:
soundfile = request.files['file']
if 'date' not in request.form:
return {"status": "error", "message": "no date found"}, 470
else:
date = request.form.get("date")
if 'device_id' not in request.form:
return {"status": "error", "message": "no device_id found"}, 471
else:
device_id = request.form.get("device_id")
kind = filetype.guess(soundfile)
if kind is None or kind.mime != 'wav':
2020-03-25 02:36:05 +01:00
LOGGER.error(
2020-03-31 16:51:31 +02:00
f"Input file was not WAV. Recieved metadata: device_id: {device_id}")
2020-03-25 02:36:05 +01:00
return {'status': 'error', 'message': 'Input file not WAV.'}, 415
2020-03-31 16:51:31 +02:00
xeger = Xeger(limit=32)
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 16:51:31 +02:00
device_id=device_id,
device_date=date,
2020-03-31 14:46:46 +02:00
tag=generated_tag)
try:
db.session.add(record)
requests.post(
2020-03-31 15:10:58 +02:00
f"http://{STORAGE_HOSTNAME}/v1/storage/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 14:46:46 +02:00
kind.mime)})
rabbitmq_channel.basic_publish(
exchange=RABBITMQ_EXCHANGE,
routing_key='feature',
body=generated_tag)
except Exception as e:
LOGGER.exception(e)
db.session.rollback()
return {"status": "exception", "message": str(
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()
return {"status": "ok", "message": 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-03-31 15:10:58 +02:00
return {"status": "ok", "message": samples}, 200
class SampleParameterResource(Resource):
"""
Sample endpoint with parameters
"""
def get(self, tag: str):
"""
Get a specific item
:param tag:
:return:
"""
sample = SampleMetadata.query.filter_by(tag=tag).first_or_404()
return {"status": "ok", "message": sample}, 200