Skip to content
Snippets Groups Projects
Commit 692dd60f authored by wactbprot's avatar wactbprot
Browse files

rm is_valid api

parent 4cae0412
No related branches found
No related tags found
No related merge requests found
...@@ -38,31 +38,66 @@ xmllint --valid --noout 11044_17_DCC_v1.8.1_Au.xml ...@@ -38,31 +38,66 @@ xmllint --valid --noout 11044_17_DCC_v1.8.1_Au.xml
## API ## 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/> > <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> > <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 > curl -X POST -H "Content-Type: text/xml" -d @valid_xml_but_dont_matches_xsd http://localhost:5005/validate
> <error>not valid</error> > <error>verbose error message</error>
``` ```
Example: Example:
``` ```
> curl -X POST -H "Content-Type: text/xml" -d @11044_17_DCC_v1.8.1_Au.xml http://localhost:5005/is_valid > curl -X POST -H "Content-Type: text/xml" -d @11044_17_DCC_v1.8.1_Au_with_error.xml http://localhost:5005/validate
> <ok/> > <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] ### /version [GET]
Returns the version of the validation server: Returns the version of the validation server:
...@@ -71,3 +106,8 @@ Returns the version of the validation server: ...@@ -71,3 +106,8 @@ Returns the version of the validation server:
> curl http://localhost:5005/version > curl http://localhost:5005/version
> <version>0.2.0</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
...@@ -38,19 +38,27 @@ def update_xsd(): ...@@ -38,19 +38,27 @@ def update_xsd():
return utils.xml_response(ret) return utils.xml_response(ret)
@app.route('/is_valid', methods=['post']) @app.route('/is_valid', methods=['post'])
def is_valid(): 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') app.logger.debug('hit validate')
xml_str = request.data xml_str = request.data
xml_tree = utils.parse(xml_str) xml_tree = utils.parse(xml_str)
if xml_tree: if xml_tree:
xsd_str = utils.get_xsd(config, xsd_file_name, from_server=False) xsd_str = utils.get_xsd(config, xsd_file_name, from_server=False)
res = utils.is_valid(xml_str=xml_str, xsd_str=xsd_str) ret = utils.validate(xml_str=xml_str, xsd_str=xsd_str)
if res:
ret = '<ok/>'
else:
ret = '<error>not valid</error>'
else: else:
ret = '<error>unvalid xml data</error>' ret = '<error>unvalid xml data</error>'
......
...@@ -46,13 +46,14 @@ def parse(xml_str): ...@@ -46,13 +46,14 @@ def parse(xml_str):
return tree return tree
def is_valid(xml_str, xsd_str): def validate(xml_str, xsd_str):
tree = parse(xml_str) tree = parse(xml_str)
if tree: schema = xmlschema.XMLSchema(xsd_str)
schema = xmlschema.XMLSchema(xsd_str) try:
return schema.is_valid(tree) schema.validate(tree)
else: return '<ok/>'
return False except xmlschema.XMLSchemaValidationError as error:
return '<error>{error}</error>'.format(error=error)
def xml_response(xml_str): def xml_response(xml_str):
return Response(xml_str, content_type='text/xml; charset=utf-8') return Response(xml_str, content_type='text/xml; charset=utf-8')
\ 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