diff --git a/docker-compose.yml b/docker-compose.yml index 316db35a755a05f025d3c2994a058dc77e2fd718..4c2e887ab4b0382d45284bb5c01ce4773bd74111 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -265,6 +265,7 @@ services: - "5010:5010" volumes: - /tmp:/tmp + - /var/run/docker.sock:/var/run/docker.sock depends_on: fda-discovery-service: condition: service_healthy diff --git a/fda-units-service/Dockerfile b/fda-units-service/Dockerfile index 708c3d9f2c97a2174850b9e0c58d5a9596f678be..40e95a160bf8256771728f9e1374af6e8056b5ef 100644 --- a/fda-units-service/Dockerfile +++ b/fda-units-service/Dockerfile @@ -12,7 +12,8 @@ COPY requirements.txt requirements.txt RUN pip install -r requirements.txt > /dev/null -COPY ./us-yml ./ +COPY ./us-yml/*.yml ./ +COPY ./onto/*.ttl ./onto/ COPY ./*.py ./ COPY ./service_ready ./ diff --git a/fda-units-service/app.py b/fda-units-service/app.py index 542a3a1e1cfb44cb68bc294e908121e82e3e2b7e..9d8a8684b95a7f558286eaa827496191de6451c1 100644 --- a/fda-units-service/app.py +++ b/fda-units-service/app.py @@ -6,6 +6,8 @@ import json from flasgger import Swagger from flasgger.utils import swag_from from flasgger import LazyString, LazyJSONEncoder +from list import list_units, get_uri +from validate import validator app = Flask(__name__) app.config["SWAGGER"] = {"title": "FDA-Units-Service", "uiversion": 3} @@ -31,10 +33,32 @@ template = dict( app.json_encoder = LazyJSONEncoder swagger = Swagger(app, config=swagger_config, template=template) -@app.route('/api/units/suggest', methods=["POST"], endpoint='units_suggest') -#@swag_from('/as-yml/suggest.yml') +@app.route('/units/suggest', methods=["POST"], endpoint='suggest') +@swag_from('suggest.yml') def suggest(): - return 200 + input_json = request.get_json() + try: + unit = str(input_json['ustring']) + offset = int(input_json['offset']) + res = list_units(unit,offset) + return jsonify(res),200 + except Exception as e: + print(e) + #str(print_exc()) + res = {"success": False, "message": "Unknown error"+str(e)+unit} + return jsonify(res) + +@app.route('/units/validate', methods=["POST"], endpoint='validate') +@swag_from('validate.yml') +def suggest(): + input_json = request.get_json() + try: + unit = str(input_json['ustring']) + res = validator(unit) + except Exception as e: + print(e) + res = {"success": False, "message": "Unknown error"+str(e)+unit} + return jsonify(res) rest_server_port = 5010 eureka_client.init(eureka_server=os.getenv('EUREKA_SERVER', 'http://localhost:9090/eureka/'), @@ -45,4 +69,4 @@ eureka_client.init(eureka_server=os.getenv('EUREKA_SERVER', 'http://localhost:90 if __name__ == '__main__': http_server = WSGIServer(('', 5010), app) - http_server.serve_forever() + http_server.serve_forever() \ No newline at end of file diff --git a/fda-units-service/requirements.txt b/fda-units-service/requirements.txt index d17d81d597cf8f083c8e451c0cb00a6a328cac52..ae9776238a804662273fdbf5c63b2a03f2bb2697 100644 --- a/fda-units-service/requirements.txt +++ b/fda-units-service/requirements.txt @@ -2,6 +2,5 @@ flask==1.1.2 flasgger==0.9.5 py-eureka-client==0.9.1 docker==5.0.0 -sqlalchemy==1.4.15 gevent==21.8.0 rdflib==6.0.1 diff --git a/fda-units-service/us-yml/suggest.yml b/fda-units-service/us-yml/suggest.yml new file mode 100644 index 0000000000000000000000000000000000000000..b47a657ae9b0c886a4c6161d5e194623d9ef2027 --- /dev/null +++ b/fda-units-service/us-yml/suggest.yml @@ -0,0 +1,25 @@ +summary: "Autosuggest units" +description: "This is a simple API which returns a list of suggested units." +consumes: +- "application/json" +produces: +- "application/json" +parameters: +- in: "body" + name: "body" + description: "to-do description" + required: true + schema: + type: "object" + properties: + ustring: + type: "string" + example : "met" + offset: + type: "integer" + example: 0 +responses: + 200: + description: "OK" + 405: + description: "Invalid input" \ No newline at end of file diff --git a/fda-units-service/us-yml/validate.yml b/fda-units-service/us-yml/validate.yml new file mode 100644 index 0000000000000000000000000000000000000000..bd92c5cb29715274338bf0efe5b59c3ac78f05f8 --- /dev/null +++ b/fda-units-service/us-yml/validate.yml @@ -0,0 +1,22 @@ +summary: "Validate units" +description: "This is a simple API for validating units." +consumes: +- "application/json" +produces: +- "application/json" +parameters: +- in: "body" + name: "body" + description: "to-do description" + required: true + schema: + type: "object" + properties: + ustring: + type: "string" + example : "metre" +responses: + 200: + description: "OK" + 405: + description: "Invalid input" \ No newline at end of file diff --git a/fda-units-service/validate.py b/fda-units-service/validate.py index e74641dc08c7962aa11a2b6eee590e3d6381787e..6c57624703e02b6acc0135cc13ef8e38292f7e1f 100644 --- a/fda-units-service/validate.py +++ b/fda-units-service/validate.py @@ -17,6 +17,6 @@ def validator(value): tmp = str(om)+value t_uri = rdflib.term.URIRef(tmp) if next(g.triples((t_uri,None,om.Unit)), _exhausted) is _exhausted and next(g.triples((t_uri,None,om.PrefixedUnit)),_exhausted) is _exhausted and next(g.triples((t_uri,None,om.UnitDivision)),_exhausted) is _exhausted: - return 'invalid' + return {"valid": False} else: - return 'valid' \ No newline at end of file + return {"valid": True} \ No newline at end of file