use marshmallow to load json
This commit is contained in:
parent
15909a46b3
commit
e0d8a5b867
@ -9,4 +9,6 @@ flask_sqlalchemy
|
||||
xeger
|
||||
pika
|
||||
psycopg2-binary
|
||||
marshmallow
|
||||
marshmallow
|
||||
marshmallow-sqlalchemy
|
||||
flask-marshmallow
|
||||
|
@ -8,6 +8,7 @@ from sentry_sdk.integrations.flask import FlaskIntegration
|
||||
import rabbitmqqueue
|
||||
from config import *
|
||||
from db import db
|
||||
from marshm import ma
|
||||
from resources import SampleResource, SampleParameterResource
|
||||
|
||||
"""
|
||||
@ -33,6 +34,7 @@ app = Flask(__name__)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOSTNAME}:5432/{POSTGRES_DB}"
|
||||
api = Api(app)
|
||||
db.init_app(app)
|
||||
ma.init_app(app)
|
||||
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
|
14
src/marshm.py
Normal file
14
src/marshm.py
Normal file
@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from flask_marshmallow import Marshmallow
|
||||
|
||||
"""
|
||||
Marshmallow
|
||||
"""
|
||||
|
||||
__author__ = '@tormakris'
|
||||
__copyright__ = "Copyright 2020, Birbnetes Team"
|
||||
__module_name__ = "marshm"
|
||||
__version__text__ = "1"
|
||||
|
||||
ma = Marshmallow()
|
@ -8,7 +8,7 @@ import requests
|
||||
from db import db
|
||||
from models import SampleMetadata
|
||||
from rabbitmqqueue import rabbitmq_channel
|
||||
from schemas import SampleSchema
|
||||
from schemas import SampleSchema, SampleMetadataSchema
|
||||
from config import *
|
||||
|
||||
"""
|
||||
@ -29,6 +29,9 @@ class SampleResource(Resource):
|
||||
See: https://swagger.kmlabz.com/?urls.primaryName=Input%20Service
|
||||
"""
|
||||
|
||||
sampleschema = SampleSchema(many=False)
|
||||
samplemetadataschema = SampleMetadataSchema(many=True)
|
||||
|
||||
def post(self):
|
||||
"""
|
||||
Post request send to the endpoint
|
||||
@ -50,15 +53,9 @@ class SampleResource(Resource):
|
||||
return {'err_msg': 'Input file not a wave file.'}, 415
|
||||
|
||||
try:
|
||||
desc = json.loads(description)
|
||||
desc = self.sampleschema.loads(description)
|
||||
except Exception as e:
|
||||
LOGGER.exception(e)
|
||||
return {'err_msg': 'Input JSON could not be parsed'}, 400
|
||||
|
||||
validate_errors = SampleSchema().validate(desc)
|
||||
if validate_errors:
|
||||
LOGGER.error(
|
||||
"Input JSON did not conform to schema. It was: {}".format(desc))
|
||||
return {'err_msg': 'Input JSON schema invalid'}, 417
|
||||
|
||||
xeger = Xeger(limit=30)
|
||||
@ -97,7 +94,7 @@ class SampleResource(Resource):
|
||||
:return:
|
||||
"""
|
||||
samples = SampleMetadata.query.all()
|
||||
return list(samples), 200
|
||||
return self.samplemetadataschema.dumps(list(samples)), 200
|
||||
|
||||
|
||||
class SampleParameterResource(Resource):
|
||||
@ -105,6 +102,8 @@ class SampleParameterResource(Resource):
|
||||
Sample endpoint with parameters
|
||||
"""
|
||||
|
||||
samplemetadataschema = SampleMetadataSchema(many=False)
|
||||
|
||||
def get(self, tag: str):
|
||||
"""
|
||||
Get a specific item
|
||||
@ -112,4 +111,4 @@ class SampleParameterResource(Resource):
|
||||
:return:
|
||||
"""
|
||||
sample = SampleMetadata.query.filter_by(tag=tag).first_or_404()
|
||||
return sample, 200
|
||||
return self.samplemetadataschema.dumps(sample), 200
|
||||
|
@ -1,5 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
from marshmallow import Schema, fields
|
||||
from models import SampleMetadata
|
||||
from marshm import ma
|
||||
from marshmallow import fields
|
||||
|
||||
|
||||
"""
|
||||
@ -13,12 +15,21 @@ __module_name__ = "schemas"
|
||||
__version__text__ = "1"
|
||||
|
||||
|
||||
class SampleSchema(Schema):
|
||||
class SampleSchema(ma.Schema):
|
||||
"""
|
||||
Parameters:
|
||||
- date (date)
|
||||
- device_id (str)
|
||||
"""
|
||||
|
||||
date = fields.Date(required=True)
|
||||
date = fields.DateTime(required=True)
|
||||
device_id = fields.Str(required=True)
|
||||
|
||||
|
||||
class SampleMetadataSchema(ma.SQLAlchemyAutoSchema):
|
||||
"""
|
||||
Marshmallow schema generated
|
||||
"""
|
||||
class Meta:
|
||||
model = SampleMetadata
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user