From bc81a65fea03d154f66ec4fbdc074e93f2e45768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Tue, 31 Mar 2020 17:32:04 +0200 Subject: [PATCH] add back marshmallow --- requirements.txt | 4 ++-- src/app.py | 4 ++-- src/config.py | 2 +- src/rabbitmqqueue.py | 2 +- src/resources.py | 49 ++++++++++++++++++++++++++------------------ src/schemas.py | 25 ++++++++++++++++++++++ 6 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 src/schemas.py diff --git a/requirements.txt b/requirements.txt index a0416e4..703c714 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,9 +4,9 @@ Flask Flask-RESTful requests werkzeug -filetype sqlalchemy flask_sqlalchemy xeger pika -psycopg2-binary \ No newline at end of file +psycopg2-binary +marshmallow \ No newline at end of file diff --git a/src/app.py b/src/app.py index 30ed593..9d917cf 100644 --- a/src/app.py +++ b/src/app.py @@ -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/') +api.add_resource(SampleResource, "/sample") +api.add_resource(SampleParameterResource, '/sample/') if __name__ == "__main__": app.run( diff --git a/src/config.py b/src/config.py index 2c036a2..c7bf615 100644 --- a/src/config.py +++ b/src/config.py @@ -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") diff --git a/src/rabbitmqqueue.py b/src/rabbitmqqueue.py index 9f23509..adf7431 100644 --- a/src/rabbitmqqueue.py +++ b/src/rabbitmqqueue.py @@ -13,6 +13,6 @@ __module_name__ = "endpoints" __version__text__ = "1" credentials = pika.PlainCredentials(RABBITMQ_USERNAME, RABBITMQ_PASSWORD) -rabbitmq = pika.BlockingConnection(pika.ConnectionParameters(host=RABBITMQ_HOST,credentials=credentials)) +rabbitmq = pika.BlockingConnection(pika.ConnectionParameters(host=RABBITMQ_HOST, credentials=credentials)) rabbitmq_channel = rabbitmq.channel() rabbitmq_channel.queue_declare(RABBITMQ_QUEUE) \ No newline at end of file diff --git a/src/resources.py b/src/resources.py index ab76da9..27f44a2 100644 --- a/src/resources.py +++ b/src/resources.py @@ -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 diff --git a/src/schemas.py b/src/schemas.py new file mode 100644 index 0000000..552c8da --- /dev/null +++ b/src/schemas.py @@ -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)