From 692dd60fdb8562fd0b4eba3f4e554ec729bdc54c Mon Sep 17 00:00:00 2001
From: wactbprot <wactbprot@gmail.com>
Date: Thu, 6 Dec 2018 14:15:55 +0100
Subject: [PATCH] rm is_valid api

---
 README.md | 54 +++++++++++++++++++++++++++++++++++++++++++++++-------
 server.py | 20 ++++++++++++++------
 utils.py  | 15 ++++++++-------
 3 files changed, 69 insertions(+), 20 deletions(-)

diff --git a/README.md b/README.md
index 4f8cd37..20380f3 100644
--- a/README.md
+++ b/README.md
@@ -38,31 +38,66 @@ xmllint --valid --noout 11044_17_DCC_v1.8.1_Au.xml
 
 ## API
 
-### /is_valid endpoint [POST]
+### /validate endpoint [POST]
+
 
 
 ```
-> curl -X POST -H "Content-Type: text/xml" -d @valid_xml_matches_xsd http://localhost:5005/is_valid
+> curl -X POST -H "Content-Type: text/xml" -d @valid_xml_matches_xsd http://localhost:5005/validate
 > <ok/>
 ```
 
 ```
-> curl -X POST -H "Content-Type: text/xml" -d @no_valid_xml http://localhost:5005/is_valid
+> curl -X POST -H "Content-Type: text/xml" -d @no_valid_xml http://localhost:5005/validate
 > <error>unvalid xml data</error>
 ```
 
 ```
-> curl -X POST -H "Content-Type: text/xml" -d @valid_xml_but_dont_matches_xsd http://localhost:5005/is_valid
-> <error>not valid</error>
+> curl -X POST -H "Content-Type: text/xml" -d @valid_xml_but_dont_matches_xsd http://localhost:5005/validate
+> <error>verbose error message</error>
 ```
 
 Example:
 
 ```
-> curl -X POST -H "Content-Type: text/xml" -d @11044_17_DCC_v1.8.1_Au.xml http://localhost:5005/is_valid
-> <ok/>
+> curl -X POST -H "Content-Type: text/xml" -d @11044_17_DCC_v1.8.1_Au_with_error.xml http://localhost:5005/validate
+> <error>failed decoding 'A' with XsdAtomicBuiltin(name='xs:double'):
+
+Reason: could not convert string to float: 'A'
+
+Schema:
+
+  <simpleType xmlns="http://www.w3.org/2001/XMLSchema" xmlns:hfp="http://www.w3.org/2001/XMLSchema-hasFacetAndProperty" id="double" name="double">
+    <annotation>
+      <appinfo>
+        <hfp:hasFacet name="pattern" />
+        <hfp:hasFacet name="enumeration" />
+        <hfp:hasFacet name="whiteSpace" />
+        <hfp:hasFacet name="maxInclusive" />
+        <hfp:hasFacet name="maxExclusive" />
+        <hfp:hasFacet name="minInclusive" />
+        <hfp:hasFacet name="minExclusive" />
+        <hfp:hasProperty name="ordered" value="total" />
+        <hfp:hasProperty name="bounded" value="true" />
+        <hfp:hasProperty name="cardinality" value="finite" />
+        <hfp:hasProperty name="numeric" value="true" />
+      </appinfo>
+      <documentation source="http://www.w3.org/TR/xmlschema-2/#double" />
+    </annotation>
+    <restriction base="xs:anySimpleType">
+      <whiteSpace fixed="true" id="double.whiteSpace" value="collapse" />
+    </restriction>
+  </simpleType>
+
+Instance:
+
+  <ns0:coverageProbability xmlns:ns0="https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/SI">A</ns0:coverageProbability>
+
+Path: /{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}digitalCalibrationCertificate/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}measurementResults/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}influenceConditions/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}influenceCondition/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}outcome/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}data/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}quantity/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/SI}list/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/SI}coverageProbability
+</error>
 ```
 
+
 ### /version [GET]
 
 Returns the version of the validation server:
@@ -71,3 +106,8 @@ Returns the version of the validation server:
 > curl http://localhost:5005/version
 > <version>0.2.0</version>
 ```
+
+
+### /update [POST]
+
+Updates the server; used by webhook (no security checks so far).
\ No newline at end of file
diff --git a/server.py b/server.py
index 6d4279a..008748e 100644
--- a/server.py
+++ b/server.py
@@ -38,19 +38,27 @@ def update_xsd():
     
     return  utils.xml_response(ret)
 
-
 @app.route('/is_valid', methods=['post'])
 def is_valid():
+    app.logger.debug('hit is_valid')
+    xml_str = request.data
+    xml_tree = utils.parse(xml_str)
+    if xml_tree:
+        xsd_str = utils.get_xsd(config, xsd_file_name, from_server=False)
+        ret = utils.is_valid(xml_str=xml_str, xsd_str=xsd_str)
+    else:
+        ret = '<error>unvalid xml data</error>'
+
+    return  utils.xml_response(ret)
+
+@app.route('/validate', methods=['post'])
+def validate():
     app.logger.debug('hit validate')
     xml_str = request.data
     xml_tree = utils.parse(xml_str)
     if xml_tree:
         xsd_str = utils.get_xsd(config, xsd_file_name, from_server=False)
-        res = utils.is_valid(xml_str=xml_str, xsd_str=xsd_str)
-        if res:
-            ret = '<ok/>'
-        else:
-            ret = '<error>not valid</error>'
+        ret = utils.validate(xml_str=xml_str, xsd_str=xsd_str)
     else:
         ret = '<error>unvalid xml data</error>'
 
diff --git a/utils.py b/utils.py
index d001765..ae944ff 100644
--- a/utils.py
+++ b/utils.py
@@ -46,13 +46,14 @@ def parse(xml_str):
     
     return tree
 
-def is_valid(xml_str, xsd_str):
+def validate(xml_str, xsd_str):
     tree = parse(xml_str)
-    if tree:
-        schema = xmlschema.XMLSchema(xsd_str)
-        return schema.is_valid(tree)
-    else:
-        return False
-    
+    schema = xmlschema.XMLSchema(xsd_str)
+    try:
+        schema.validate(tree) 
+        return '<ok/>'
+    except xmlschema.XMLSchemaValidationError as error:
+        return '<error>{error}</error>'.format(error=error)
+   
 def xml_response(xml_str):
     return Response(xml_str, content_type='text/xml; charset=utf-8')
\ No newline at end of file
-- 
GitLab