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

#104 ontology add validation to app.py

parent 86423b95
No related branches found
No related tags found
4 merge requests!81New stable release,!43Merge dev to master,!33Draft: merge dev to master,!32Add units-service to dev
......@@ -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
......
......@@ -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 ./
......
......@@ -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/'),
......
......@@ -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
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):
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment