Skip to content
Snippets Groups Projects
Commit 532c7f1d authored by wactbprot's avatar wactbprot
Browse files

rdm

parent 91abd2e0
No related branches found
No related tags found
No related merge requests found
...@@ -36,8 +36,38 @@ xmllint --valid --noout 11044_17_DCC_v1.8.1_Au.xml ...@@ -36,8 +36,38 @@ xmllint --valid --noout 11044_17_DCC_v1.8.1_Au.xml
> ./server > ./server
``` ```
### Example ## API
'`` ### /is_valid endpoint [POST]
curl -X POST -H "Content-Type: text/xml" -d @11044_17_DCC_v1.8.1_Au.xml http://localhost:5005/is_valid
```
\ No newline at end of file ```
> curl -X POST -H "Content-Type: text/xml" -d @valid_xml_matches_xsd http://localhost:5005/is_valid
> <ok/>
```
```
> curl -X POST -H "Content-Type: text/xml" -d @no_valid_xml http://localhost:5005/is_valid
> <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>
```
Example:
```
> curl -X POST -H "Content-Type: text/xml" -d @11044_17_DCC_v1.8.1_Au.xml http://localhost:5005/is_valid
> <ok/>
```
### /version [GET]
Returns the version of the validation server:
```
> curl http://localhost:5005/version
> <version>0.2.0</version>
```
from flask import Flask, request, Response from flask import Flask, request
from flask_cors import CORS from flask_cors import CORS
import utils as utils import utils as utils
...@@ -14,7 +14,19 @@ CORS(app) ...@@ -14,7 +14,19 @@ CORS(app)
@app.route('/version', methods=['get']) @app.route('/version', methods=['get'])
def version(): def version():
app.logger.debug('hit version') app.logger.debug('hit version')
return Response(git_cmd.describe(), content_type='text/xml; charset=utf-8') ret = '<version>{version}</version>'.format(version=git_cmd.describe())
return utils.xml_response(ret)
@app.route('/update', methods=['post'])
def update():
app.logger.debug('hit update')
req = request.get_json()
git_cmd.pull()
app.logger.info("pulled {log}".format(log=g.log("-n 1")))
ret = '<ok/>'
return utils.xml_response(ret)
@app.route('/update_xsd', methods=['post']) @app.route('/update_xsd', methods=['post'])
def update_xsd(): def update_xsd():
...@@ -24,7 +36,7 @@ def update_xsd(): ...@@ -24,7 +36,7 @@ def update_xsd():
if xsd_xml: if xsd_xml:
res = utils.save_xsd(config, xsd_str, xsd_file_name) res = utils.save_xsd(config, xsd_str, xsd_file_name)
return Response(res, content_type='text/xml; charset=utf-8') return utils.xml_response(ret)
@app.route('/is_valid', methods=['post']) @app.route('/is_valid', methods=['post'])
...@@ -38,11 +50,12 @@ def is_valid(): ...@@ -38,11 +50,12 @@ def is_valid():
if res: if res:
ret = '<ok/>' ret = '<ok/>'
else: else:
ret = '<error/>' ret = '<error>not valid</error>'
else:
ret = '<error>unvalid xml data</error>'
return Response(ret, content_type='text/xml; charset=utf-8') return utils.xml_response(ret)
if __name__ == '__main__': if __name__ == '__main__':
app.run(host=config['server']['host'], port=config['server']['port']) app.run(host=config['server']['host'], port=config['server']['port'])
\ No newline at end of file
import json import json
import git import git
import requests import requests
from flask import Response
from xml.etree import ElementTree as ET from xml.etree import ElementTree as ET
import xmlschema import xmlschema
...@@ -14,18 +15,25 @@ def get_config_dict(): ...@@ -14,18 +15,25 @@ def get_config_dict():
def git_cmd(config): def git_cmd(config):
return git.cmd.Git(config['git']['dir']) return git.cmd.Git(config['git']['dir'])
def get_xsd_url(config, xsd_name):
return "{uri}/{xsd_name}".format(uri=config['xsd']['url'], xsd_name=xsd_name)
def get_xsd_path_file(config, xsd_name):
return "{dir}/{xsd_name}".format(dir=config['xsd']['dir'], xsd_name=xsd_name)
def get_xsd(config, xsd_name, from_server=True): def get_xsd(config, xsd_name, from_server=True):
if from_server: if from_server:
url = "{uri}/{xsd_name}".format(uri=config['xsd']['url'], xsd_name=xsd_name) url = get_xsd_url(config, xsd_name)
xsd_str = requests.get(url).text xsd_str = requests.get(url).text
else: else:
path_file = "{dir}/{xsd_name}".format(dir=config['xsd']['dir'], xsd_name=xsd_name) path_file = get_xsd_path_file(config, xsd_name)
xsd_str = open(path_file).read() xsd_str = open(path_file).read()
return xsd_str return xsd_str
def save_xsd(config, xsd_str, file_name): def save_xsd(config, xsd_str, xsd_name):
file = open("{dir}/{file}".format (dir = config['xsd']['dir'], file=file_name), "w") path_file = get_xsd_path_file(config, xsd_name)
file = open(path_file, "w")
file.write(xsd_str) file.write(xsd_str)
return 'ok' return 'ok'
...@@ -46,4 +54,5 @@ def is_valid(xml_str, xsd_str): ...@@ -46,4 +54,5 @@ def is_valid(xml_str, xsd_str):
else: else:
return False return False
def xml_response(xml_str):
\ No newline at end of file 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