Merge branch '35-analysis-does-not-return-json' into 'master'
authorMichael Winsauer <e1429715@student.tuwien.ac.at>
Tue, 11 Dec 2018 14:07:35 +0000 (15:07 +0100)
committerMichael Winsauer <e1429715@student.tuwien.ac.at>
Tue, 11 Dec 2018 14:07:35 +0000 (15:07 +0100)
refined README.me, fixed issue #35

Closes #35

See merge request aic18/G6T4!30

service-analysis/README.md
service-analysis/sentiment_analysis.py

index fb414facdeaefcf0091aa27e6bf9e16b4cbbaf37..a4d4cc4e353fb4f1c5096c55508c5167e4c4d627 100644 (file)
@@ -31,7 +31,7 @@ JSON string may contain more value-key pairs than 'text', but 'text' is needed f
 
 ## run with docker
 - `docker build -t sentiment_analysis .`
-- `docker run -d -p YOUR_PORT:8081 sentiment_analysis`
+- `docker container run -d -p YOUR_PORT:8081 sentiment_analysis`
 
 ## run local
 ### requirements
index 933ce9c3114804a85a6f64487bfafd4cf5b703cc..2a013e943a63174c62de7ddde25b3da8177478ee 100644 (file)
@@ -2,7 +2,7 @@
 import json
 import indicoio
 
-from flask import Flask, request, jsonify
+from flask import Flask, request, jsonify, Response
 from flask_restful import Resource, Api, output_json, abort
 
 app = Flask(__name__)
@@ -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 = []
@@ -34,7 +34,7 @@ class Sentiment_Analysis(Resource):
                        return make_error(503, message=service_error_503)
                sentiment = value/len(text_array)
                data = {'sentiment': sentiment}
-               return output_json(json.dumps(data), 200)
+               return data
                                
 api.add_resource(Sentiment_Analysis, '/')