add back marshmallow
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2020-03-31 17:32:04 +02:00
parent 479ca0f44d
commit bc81a65fea
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
6 changed files with 60 additions and 26 deletions

View File

@ -4,9 +4,9 @@ Flask
Flask-RESTful
requests
werkzeug
filetype
sqlalchemy
flask_sqlalchemy
xeger
pika
psycopg2-binary
marshmallow

View File

@ -48,8 +48,8 @@ logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
api.add_resource(SampleResource, "/v1/input/sample")
api.add_resource(SampleParameterResource, '/v1/input/sample/<tag>')
api.add_resource(SampleResource, "/sample")
api.add_resource(SampleParameterResource, '/sample/<tag>')
if __name__ == "__main__":
app.run(

View File

@ -22,7 +22,7 @@ RELEASE_ID = os.environ.get("RELEASE_ID", "test")
RELEASEMODE = os.environ.get("INPUT_SERVICE_RELEASEMODE", "dev")
RABBITMQ_HOST = os.getenv("INPUT_RABBITMQ_HOSTNAME", "localhost")
RABBITMQ_EXCHANGE = os.getenv("INPUT_RABBITMQ_EXCHANGE", "wave-extract")
RABBITMQ_EXCHANGE = os.getenv("INPUT_RABBITMQ_EXCHANGE", "")
RABBITMQ_QUEUE = os.getenv("INPUT_RABBITMQ_QUEUE", "wave-extract")
RABBITMQ_USERNAME = os.getenv("INPUT_RABBITMQ_USERNAME", "rabbitmq")
RABBITMQ_PASSWORD = os.getenv("INPUT_RABBITMQ_PASSWORD", "rabbitmq")

View File

@ -1,13 +1,14 @@
#!/usr/bin/env python3
import logging
import json
from xeger import Xeger
from flask_restful import Resource
from flask import request
from flask import request, jsonify
import requests
import filetype
from db import db
from models import SampleMetadata
from rabbitmqqueue import rabbitmq_channel
from schemas import SampleSchema
from config import *
"""
@ -38,39 +39,47 @@ class SampleResource(Resource):
else:
soundfile = request.files['file']
if 'date' not in request.form:
return {"status": "error", "message": "no date found"}, 470
if 'description' not in request.form:
return {"status": "error", "message": "no description found"}, 470
else:
date = request.form.get("date")
description = request.form.get("description")
if 'device_id' not in request.form:
return {"status": "error", "message": "no device_id found"}, 471
else:
device_id = request.form.get("device_id")
if soundfile.content_type != 'audio/wave':
LOGGER.info(
f"Input file was not WAV.")
return {'status': 'error', 'message': 'Input file not a wave file.'}, 415
kind = filetype.guess(soundfile)
if kind is None or kind.mime != 'wav':
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 = SampleSchema().validate(desc)
if validate_errors:
LOGGER.error(
f"Input file was not WAV. Recieved metadata: device_id: {device_id}")
return {'status': 'error', 'message': 'Input file not WAV.'}, 415
"Input JSON did not conform to schema. It was: {}", desc)
return {'status': 'error',
'message': 'Input JSON schema invalid'}, 417
xeger = Xeger(limit=32)
xeger = Xeger(limit=30)
generated_tag = xeger.xeger(r'^[a-zA-Z]+[0-9a-zA-Z_]*$')
record = SampleMetadata(
device_id=device_id,
device_date=date,
device_id=desc['device_id'],
device_date=desc['date'],
tag=generated_tag)
try:
db.session.add(record)
requests.post(
f"http://{STORAGE_HOSTNAME}/v1/storage/object",
f"http://{STORAGE_HOSTNAME}/object",
files={
'tag': (None, generated_tag),
'file': (
'wave.wav',
soundfile,
kind.mime)})
soundfile.content_type)})
rabbitmq_channel.basic_publish(
exchange=RABBITMQ_EXCHANGE,
routing_key='feature',
@ -90,7 +99,7 @@ class SampleResource(Resource):
:return:
"""
samples = SampleMetadata.query.all()
return {"status": "ok", "message": samples}, 200
return {"status": "ok", "message": jsonify(json_list=samples)}, 200
class SampleParameterResource(Resource):
@ -105,4 +114,4 @@ class SampleParameterResource(Resource):
:return:
"""
sample = SampleMetadata.query.filter_by(tag=tag).first_or_404()
return {"status": "ok", "message": sample}, 200
return {"status": "ok", "message": jsonify(sample)}, 200

25
src/schemas.py Normal file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env python3
from marshmallow import Schema, fields
"""
Schemas of input objects
"""
__author__ = "@tormakris"
__copyright__ = "Copyright 2020, Birbnetes Team"
__module_name__ = "schemas"
__version__text__ = "1"
class SampleSchema(Schema):
""" /v1/email/pay - POST
Parameters:
- date (date)
- device_id (str)
"""
date = fields.Date(required=True)
device_id = fields.Str(required=True)