fixed issue of internal server error if single json object not posted as list, refine...
authorSebastian Steiner <e1029038@student.tuwien.ac.at>
Tue, 11 Dec 2018 13:17:28 +0000 (14:17 +0100)
committerSebastian Steiner <e1029038@student.tuwien.ac.at>
Tue, 11 Dec 2018 13:17:28 +0000 (14:17 +0100)
service-analysis/sentiment_analysis.py

index 4e8f63ef786259100fb113636ab903b8f4565bd6..7b449d2082d16f0b9cb5eb26a44756780e9a3530 100644 (file)
@@ -10,15 +10,15 @@ api = Api(app)
 
 indicoio.config.api_key = '525f16078717a430f9dac17cdc9dbaa3'
 
-input_error_409 = 'Input must be a JSON string. JSON objects must contain contain key text.'
+input_error_409 = 'Input must be a list of JSON objects. JSON objects must contain contain key text.'
 service_error_503 = 'The sentiment analysis service is currently unavailable.'
 
 class Sentiment_Analysis(Resource):
        def get(self):
-               return "POST a JSON string as content-type: application/json. JSON objects must contain key 'text'."
+               return "POST a list of JSON objects as content-type: application/json. JSON objects must contain key 'text'.", 405, {'Allow': 'GET'}
                
        def post(self):
-               if not request.json:
+               if not request.json or not isinstance(request.json, (list,)):
                        return abort(409, message=input_error_409)
                texts = request.json
                text_array = []
@@ -35,6 +35,10 @@ class Sentiment_Analysis(Resource):
                sentiment = value/len(text_array)
                data = {'sentiment': sentiment}
                return data, 200, {'Content-Type': 'application/json'}
+               #r = Response(response=json.dumps(data), status=200, mimetype="application/json")
+               #r.headers["Content-Type"] = "application/json"
+               #return r
+               #return output_json(json.dumps(data), 200, headers={'Content-Type': "application/json"})
                                
 api.add_resource(Sentiment_Analysis, '/')