input-service/src/resources.py

113 lines
3.4 KiB
Python
Raw Normal View History

2020-03-25 01:19:22 +01:00
#!/usr/bin/env python3
2020-03-31 14:46:46 +02:00
from app import rabbitmq_channel
2020-03-25 01:19:22 +01:00
import logging
2020-03-25 02:36:05 +01:00
import json
2020-03-25 03:57:07 +01:00
import rstr
2020-03-25 02:36:05 +01:00
from flask_restful import Resource, reqparse
from werkzeug.datastructures import FileStorage
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-25 01:19:22 +01:00
from schemas import *
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:
"""
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
2020-03-25 03:57:07 +01:00
validate_errors = SampleSchema().validate(desc)
2020-03-25 02:36:05 +01:00
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 03:57:07 +01:00
generated_tag = rstr.xeger(r'^[a-zA-Z]+[0-9a-zA-Z_]*$', 2, 32)
2020-03-31 14:46:46 +02:00
record = SampleMetadata(
device_id=desc['device_id'],
device_date=desc['date'],
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',
soundFile,
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:
"""
samples = SampleMetadata.query.get.all()
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