This commit is contained in:
parent
479ca0f44d
commit
bc81a65fea
@ -4,9 +4,9 @@ Flask
|
|||||||
Flask-RESTful
|
Flask-RESTful
|
||||||
requests
|
requests
|
||||||
werkzeug
|
werkzeug
|
||||||
filetype
|
|
||||||
sqlalchemy
|
sqlalchemy
|
||||||
flask_sqlalchemy
|
flask_sqlalchemy
|
||||||
xeger
|
xeger
|
||||||
pika
|
pika
|
||||||
psycopg2-binary
|
psycopg2-binary
|
||||||
|
marshmallow
|
@ -48,8 +48,8 @@ logger = logging.getLogger(__name__)
|
|||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
|
|
||||||
api.add_resource(SampleResource, "/v1/input/sample")
|
api.add_resource(SampleResource, "/sample")
|
||||||
api.add_resource(SampleParameterResource, '/v1/input/sample/<tag>')
|
api.add_resource(SampleParameterResource, '/sample/<tag>')
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(
|
app.run(
|
||||||
|
@ -22,7 +22,7 @@ RELEASE_ID = os.environ.get("RELEASE_ID", "test")
|
|||||||
RELEASEMODE = os.environ.get("INPUT_SERVICE_RELEASEMODE", "dev")
|
RELEASEMODE = os.environ.get("INPUT_SERVICE_RELEASEMODE", "dev")
|
||||||
|
|
||||||
RABBITMQ_HOST = os.getenv("INPUT_RABBITMQ_HOSTNAME", "localhost")
|
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_QUEUE = os.getenv("INPUT_RABBITMQ_QUEUE", "wave-extract")
|
||||||
RABBITMQ_USERNAME = os.getenv("INPUT_RABBITMQ_USERNAME", "rabbitmq")
|
RABBITMQ_USERNAME = os.getenv("INPUT_RABBITMQ_USERNAME", "rabbitmq")
|
||||||
RABBITMQ_PASSWORD = os.getenv("INPUT_RABBITMQ_PASSWORD", "rabbitmq")
|
RABBITMQ_PASSWORD = os.getenv("INPUT_RABBITMQ_PASSWORD", "rabbitmq")
|
||||||
|
@ -13,6 +13,6 @@ __module_name__ = "endpoints"
|
|||||||
__version__text__ = "1"
|
__version__text__ = "1"
|
||||||
|
|
||||||
credentials = pika.PlainCredentials(RABBITMQ_USERNAME, RABBITMQ_PASSWORD)
|
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 = rabbitmq.channel()
|
||||||
rabbitmq_channel.queue_declare(RABBITMQ_QUEUE)
|
rabbitmq_channel.queue_declare(RABBITMQ_QUEUE)
|
@ -1,13 +1,14 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import logging
|
import logging
|
||||||
|
import json
|
||||||
from xeger import Xeger
|
from xeger import Xeger
|
||||||
from flask_restful import Resource
|
from flask_restful import Resource
|
||||||
from flask import request
|
from flask import request, jsonify
|
||||||
import requests
|
import requests
|
||||||
import filetype
|
|
||||||
from db import db
|
from db import db
|
||||||
from models import SampleMetadata
|
from models import SampleMetadata
|
||||||
from rabbitmqqueue import rabbitmq_channel
|
from rabbitmqqueue import rabbitmq_channel
|
||||||
|
from schemas import SampleSchema
|
||||||
from config import *
|
from config import *
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -38,39 +39,47 @@ class SampleResource(Resource):
|
|||||||
else:
|
else:
|
||||||
soundfile = request.files['file']
|
soundfile = request.files['file']
|
||||||
|
|
||||||
if 'date' not in request.form:
|
if 'description' not in request.form:
|
||||||
return {"status": "error", "message": "no date found"}, 470
|
return {"status": "error", "message": "no description found"}, 470
|
||||||
else:
|
else:
|
||||||
date = request.form.get("date")
|
description = request.form.get("description")
|
||||||
|
|
||||||
if 'device_id' not in request.form:
|
if soundfile.content_type != 'audio/wave':
|
||||||
return {"status": "error", "message": "no device_id found"}, 471
|
LOGGER.info(
|
||||||
else:
|
f"Input file was not WAV.")
|
||||||
device_id = request.form.get("device_id")
|
return {'status': 'error', 'message': 'Input file not a wave file.'}, 415
|
||||||
|
|
||||||
kind = filetype.guess(soundfile)
|
try:
|
||||||
if kind is None or kind.mime != 'wav':
|
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(
|
LOGGER.error(
|
||||||
f"Input file was not WAV. Recieved metadata: device_id: {device_id}")
|
"Input JSON did not conform to schema. It was: {}", desc)
|
||||||
return {'status': 'error', 'message': 'Input file not WAV.'}, 415
|
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_]*$')
|
generated_tag = xeger.xeger(r'^[a-zA-Z]+[0-9a-zA-Z_]*$')
|
||||||
|
|
||||||
record = SampleMetadata(
|
record = SampleMetadata(
|
||||||
device_id=device_id,
|
device_id=desc['device_id'],
|
||||||
device_date=date,
|
device_date=desc['date'],
|
||||||
tag=generated_tag)
|
tag=generated_tag)
|
||||||
try:
|
try:
|
||||||
db.session.add(record)
|
db.session.add(record)
|
||||||
requests.post(
|
requests.post(
|
||||||
f"http://{STORAGE_HOSTNAME}/v1/storage/object",
|
f"http://{STORAGE_HOSTNAME}/object",
|
||||||
files={
|
files={
|
||||||
'tag': (None, generated_tag),
|
'tag': (None, generated_tag),
|
||||||
'file': (
|
'file': (
|
||||||
'wave.wav',
|
'wave.wav',
|
||||||
soundfile,
|
soundfile,
|
||||||
kind.mime)})
|
soundfile.content_type)})
|
||||||
rabbitmq_channel.basic_publish(
|
rabbitmq_channel.basic_publish(
|
||||||
exchange=RABBITMQ_EXCHANGE,
|
exchange=RABBITMQ_EXCHANGE,
|
||||||
routing_key='feature',
|
routing_key='feature',
|
||||||
@ -90,7 +99,7 @@ class SampleResource(Resource):
|
|||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
samples = SampleMetadata.query.all()
|
samples = SampleMetadata.query.all()
|
||||||
return {"status": "ok", "message": samples}, 200
|
return {"status": "ok", "message": jsonify(json_list=samples)}, 200
|
||||||
|
|
||||||
|
|
||||||
class SampleParameterResource(Resource):
|
class SampleParameterResource(Resource):
|
||||||
@ -105,4 +114,4 @@ class SampleParameterResource(Resource):
|
|||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
sample = SampleMetadata.query.filter_by(tag=tag).first_or_404()
|
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
25
src/schemas.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user