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

104 ontology - add methode get_uri by unit name

parent 3c55b0e9
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
...@@ -7,7 +7,7 @@ from flasgger import Swagger ...@@ -7,7 +7,7 @@ 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 list import list_units, get_uri
from validate import validator from validate import validator, stringmapper
app = Flask(__name__) app = Flask(__name__)
app.config["SWAGGER"] = {"title": "FDA-Units-Service", "uiversion": 3} app.config["SWAGGER"] = {"title": "FDA-Units-Service", "uiversion": 3}
...@@ -50,11 +50,23 @@ def suggest(): ...@@ -50,11 +50,23 @@ def suggest():
@app.route('/api/units/validate', methods=["POST"], endpoint='validate') @app.route('/api/units/validate', methods=["POST"], endpoint='validate')
@swag_from('validate.yml') @swag_from('validate.yml')
def suggest(): def valitate():
input_json = request.get_json() input_json = request.get_json()
try: try:
unit = str(input_json['ustring']) unit = str(input_json['ustring'])
res = validator(unit) res = validator(stringmapper(unit))
except Exception as e:
print(e)
res = {"success": False, "message": "Unknown error"+str(e)+unit}
return jsonify(res)
@app.route('/api/units/geturi', methods=["POST"], endpoint='geturi')
@swag_from('geturi.yml')
def geturi():
input_json = request.get_json()
try:
name = str(input_json['uname'])
res = get_uri(name)
except Exception as e: except Exception as e:
print(e) print(e)
res = {"success": False, "message": "Unknown error"+str(e)+unit} res = {"success": False, "message": "Unknown error"+str(e)+unit}
......
...@@ -36,12 +36,12 @@ def list_units(string,offset=0): ...@@ -36,12 +36,12 @@ def list_units(string,offset=0):
return None return None
def get_uri(name): def get_uri(name):
if bool(re.match('^[a-zA-Z0-9]+$',name)): if bool(re.match('^[a-zA-Z0-9\\s]+$',name)):
uri_query = """ uri_query = """
SELECT ?uri SELECT ?uri ?o
WHERE { WHERE {
?uri <http://www.w3.org/2000/01/rdf-schema#label> ?o . ?uri <http://www.w3.org/2000/01/rdf-schema#label> ?o .
FILTER (str(?o)=\""""+name+"""\") FILTER regex(str(?o),\"^"""+name+"""$\","i")
} LIMIT 1 } LIMIT 1
""" """
qres = g.query(uri_query) qres = g.query(uri_query)
......
summary: "Get URI of units"
description: "This is a simple API for getting the URI of a certain unit in OM."
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- in: "body"
name: "body"
description: "to-do description"
required: true
schema:
type: "object"
properties:
uname:
type: "string"
example : "metre"
responses:
200:
description: "OK"
405:
description: "Invalid input"
\ No newline at end of file
...@@ -20,3 +20,9 @@ def validator(value): ...@@ -20,3 +20,9 @@ def validator(value):
return {"valid": False} return {"valid": False}
else: else:
return {"valid": True} return {"valid": True}
def stringmapper(thisstring):
if ' ' in thisstring:
return thisstring.split(" ",1)[0]+thisstring.split(" ",1)[1].title().replace(" ","")
else:
return thisstring
\ 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