diff --git a/fda-units-service/app.py b/fda-units-service/app.py index 423481f2044562cdba33b4c5d02e7dc7d6a2d94d..36f94de0c545325ca02a20c3f751fda12e46943e 100644 --- a/fda-units-service/app.py +++ b/fda-units-service/app.py @@ -7,7 +7,7 @@ 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 +from validate import validator, stringmapper app = Flask(__name__) app.config["SWAGGER"] = {"title": "FDA-Units-Service", "uiversion": 3} @@ -50,11 +50,23 @@ def suggest(): @app.route('/api/units/validate', methods=["POST"], endpoint='validate') @swag_from('validate.yml') -def suggest(): +def valitate(): input_json = request.get_json() try: 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: print(e) res = {"success": False, "message": "Unknown error"+str(e)+unit} diff --git a/fda-units-service/list.py b/fda-units-service/list.py index 889da99214820deef9bda67ae9bd6884db02dec9..cbfaaf6c462397afabd186f9fd4a136d9efe5973 100644 --- a/fda-units-service/list.py +++ b/fda-units-service/list.py @@ -36,12 +36,12 @@ def list_units(string,offset=0): return None 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 = """ - SELECT ?uri + SELECT ?uri ?o WHERE { ?uri <http://www.w3.org/2000/01/rdf-schema#label> ?o . - FILTER (str(?o)=\""""+name+"""\") + FILTER regex(str(?o),\"^"""+name+"""$\","i") } LIMIT 1 """ qres = g.query(uri_query) diff --git a/fda-units-service/us-yml/geturi.yml b/fda-units-service/us-yml/geturi.yml new file mode 100644 index 0000000000000000000000000000000000000000..cefbb9c04aa70e2bf86cbfa0f1bd2c0d3fb9cdb3 --- /dev/null +++ b/fda-units-service/us-yml/geturi.yml @@ -0,0 +1,22 @@ +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 diff --git a/fda-units-service/validate.py b/fda-units-service/validate.py index 6c57624703e02b6acc0135cc13ef8e38292f7e1f..df9010f5def1440b6644f6825ea62c8ce2aedf0a 100644 --- a/fda-units-service/validate.py +++ b/fda-units-service/validate.py @@ -19,4 +19,10 @@ def validator(value): 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 {"valid": False} else: - return {"valid": True} \ No newline at end of file + 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