Skip to content
Snippets Groups Projects
Commit 3f19d61c authored by Cornelia Michlits's avatar Cornelia Michlits
Browse files

#104 ontology add validation to app.py

Former-commit-id: 4cd1b6e5
parent 163dafae
Branches
Tags
1 merge request!42Fixed the query service tests
...@@ -265,6 +265,7 @@ services: ...@@ -265,6 +265,7 @@ services:
- "5010:5010" - "5010:5010"
volumes: volumes:
- /tmp:/tmp - /tmp:/tmp
- /var/run/docker.sock:/var/run/docker.sock
depends_on: depends_on:
fda-discovery-service: fda-discovery-service:
condition: service_healthy condition: service_healthy
......
...@@ -12,7 +12,8 @@ COPY requirements.txt requirements.txt ...@@ -12,7 +12,8 @@ COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt > /dev/null RUN pip install -r requirements.txt > /dev/null
COPY ./us-yml ./ COPY ./us-yml/*.yml ./
COPY ./onto/*.ttl ./onto/
COPY ./*.py ./ COPY ./*.py ./
COPY ./service_ready ./ COPY ./service_ready ./
......
...@@ -6,6 +6,8 @@ import json ...@@ -6,6 +6,8 @@ import json
from flasgger import Swagger from flasgger import Swagger
from flasgger.utils import swag_from from flasgger.utils import swag_from
from flasgger import LazyString, LazyJSONEncoder from flasgger import LazyString, LazyJSONEncoder
from list import list_units, get_uri
from validate import validator
app = Flask(__name__) app = Flask(__name__)
app.config["SWAGGER"] = {"title": "FDA-Units-Service", "uiversion": 3} app.config["SWAGGER"] = {"title": "FDA-Units-Service", "uiversion": 3}
...@@ -31,10 +33,32 @@ template = dict( ...@@ -31,10 +33,32 @@ template = dict(
app.json_encoder = LazyJSONEncoder app.json_encoder = LazyJSONEncoder
swagger = Swagger(app, config=swagger_config, template=template) swagger = Swagger(app, config=swagger_config, template=template)
@app.route('/api/units/suggest', methods=["POST"], endpoint='units_suggest') @app.route('/units/suggest', methods=["POST"], endpoint='suggest')
#@swag_from('/as-yml/suggest.yml') @swag_from('suggest.yml')
def suggest(): 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 rest_server_port = 5010
eureka_client.init(eureka_server=os.getenv('EUREKA_SERVER', 'http://localhost:9090/eureka/'), eureka_client.init(eureka_server=os.getenv('EUREKA_SERVER', 'http://localhost:9090/eureka/'),
......
...@@ -2,6 +2,5 @@ flask==1.1.2 ...@@ -2,6 +2,5 @@ flask==1.1.2
flasgger==0.9.5 flasgger==0.9.5
py-eureka-client==0.9.1 py-eureka-client==0.9.1
docker==5.0.0 docker==5.0.0
sqlalchemy==1.4.15
gevent==21.8.0 gevent==21.8.0
rdflib==6.0.1 rdflib==6.0.1
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
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
...@@ -17,6 +17,6 @@ def validator(value): ...@@ -17,6 +17,6 @@ def validator(value):
tmp = str(om)+value tmp = str(om)+value
t_uri = rdflib.term.URIRef(tmp) 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: 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: else:
return 'valid' return {"valid": True}
\ No newline at end of file \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment