use marshmallow to load json
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user