This commit is contained in:
@@ -2,13 +2,12 @@
|
||||
import json
|
||||
from xeger import Xeger
|
||||
from flask_restful import Resource
|
||||
from flask import request, current_app
|
||||
from flask import request, current_app, abort
|
||||
import requests
|
||||
import pika
|
||||
from db import db
|
||||
from models import SampleMetadata
|
||||
from schemas import SampleSchema, SampleMetadataSchema
|
||||
from config import *
|
||||
|
||||
"""
|
||||
Flask Restful endpoints
|
||||
@@ -35,25 +34,23 @@ class SampleResource(Resource):
|
||||
:return:
|
||||
"""
|
||||
if 'file' not in request.files:
|
||||
return {"err_msg": "no file found"}, 469
|
||||
return abort(400, "no file found")
|
||||
else:
|
||||
soundfile = request.files['file']
|
||||
|
||||
if 'description' not in request.form:
|
||||
return {"err_msg": "no description found"}, 470
|
||||
return abort(400, "no description found")
|
||||
else:
|
||||
description = request.form.get("description")
|
||||
|
||||
if soundfile.content_type != 'audio/wave':
|
||||
current_app.logger.info(
|
||||
f"Input file was not WAV.")
|
||||
return {'err_msg': 'Input file not a wave file.'}, 415
|
||||
|
||||
current_app.logger.info(f"Input file was not WAV.")
|
||||
return abort(415, 'Input file not a wave file.')
|
||||
try:
|
||||
desc = self.sampleschema.loads(description)
|
||||
except Exception as e:
|
||||
current_app.logger.exception(e)
|
||||
return {'err_msg': 'Input JSON schema invalid'}, 417
|
||||
return abort(417, 'Input JSON schema invalid')
|
||||
|
||||
xeger = Xeger(limit=30)
|
||||
while True:
|
||||
@@ -80,18 +77,26 @@ class SampleResource(Resource):
|
||||
record = SampleMetadata(
|
||||
device_id=desc['device_id'],
|
||||
device_date=desc['date'],
|
||||
tag=generated_tag)
|
||||
tag=generated_tag
|
||||
)
|
||||
db.session.add(record)
|
||||
|
||||
files = {
|
||||
'description': (None, json.dumps({'tag': generated_tag}), 'application/json'),
|
||||
'soundFile': (
|
||||
'wave.wav',
|
||||
soundfile,
|
||||
soundfile.content_type,
|
||||
{'Content-Length': soundfile_content_length})}
|
||||
|
||||
r = requests.post(
|
||||
f"http://{current_app.config.get('STORAGE_HOSTNAME')}/object",
|
||||
files=files)
|
||||
|
||||
if r.status_code not in [200, 201]:
|
||||
return abort(500, f"Failed to upload sample to storage service. Upstream status: {r.status_code}: {r.text}")
|
||||
|
||||
try:
|
||||
db.session.add(record)
|
||||
requests.post(
|
||||
f"http://{STORAGE_HOSTNAME}/object",
|
||||
files={
|
||||
'description': (None, json.dumps({'tag': generated_tag}), 'application/json'),
|
||||
'soundFile': (
|
||||
'wave.wav',
|
||||
soundfile,
|
||||
soundfile.content_type,
|
||||
{'Content-Length': soundfile_content_length})}).raise_for_status() # Anyádat curl am
|
||||
credentials = pika.PlainCredentials(current_app.config['FLASK_PIKA_PARAMS']['username'],
|
||||
current_app.config['FLASK_PIKA_PARAMS']['password'])
|
||||
connection = pika.BlockingConnection(
|
||||
@@ -109,20 +114,19 @@ class SampleResource(Resource):
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.exception(e)
|
||||
db.session.rollback()
|
||||
return {"err_msg": str(
|
||||
e), "hint": "DB or downstream service error"}, 569
|
||||
return abort(569, "AMPQ Publish error")
|
||||
|
||||
db.session.commit()
|
||||
return {"tag": generated_tag}, 200
|
||||
|
||||
def get(self):
|
||||
"""
|
||||
Get all stored items
|
||||
:return:
|
||||
"""
|
||||
samples = SampleMetadata.query.all()
|
||||
return self.samplemetadataschema.dump(list(samples)), 200
|
||||
|
||||
def get(self):
|
||||
"""
|
||||
Get all stored items
|
||||
:return:
|
||||
"""
|
||||
samples = SampleMetadata.query.all()
|
||||
return self.samplemetadataschema.dump(list(samples)), 200
|
||||
|
||||
|
||||
class SampleParameterResource(Resource):
|
||||
|
||||
Reference in New Issue
Block a user