From 532c7f1d7cd028dddbaa3078d99926d6dacf35ce Mon Sep 17 00:00:00 2001
From: wactbprot <wactbprot@gmail.com>
Date: Thu, 6 Dec 2018 11:11:00 +0100
Subject: [PATCH] rdm

---
 README.md | 38 ++++++++++++++++++++++++++++++++++----
 server.py | 25 +++++++++++++++++++------
 utils.py  | 19 ++++++++++++++-----
 3 files changed, 67 insertions(+), 15 deletions(-)

diff --git a/README.md b/README.md
index 01d6d17..9293711 100644
--- a/README.md
+++ b/README.md
@@ -36,8 +36,38 @@ xmllint --valid --noout 11044_17_DCC_v1.8.1_Au.xml
 > ./server
 ```
 
-### Example
+## API
 
-'``
-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
+### /is_valid endpoint [POST]
+
+
+```
+> 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>
+```
diff --git a/server.py b/server.py
index f693514..3ffa394 100644
--- a/server.py
+++ b/server.py
@@ -1,4 +1,4 @@
-from flask import Flask, request, Response
+from flask import Flask, request
 from flask_cors import CORS
 import utils as utils
 
@@ -14,7 +14,19 @@ CORS(app)
 @app.route('/version', methods=['get'])
 def 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'])
 def update_xsd():
@@ -24,7 +36,7 @@ def update_xsd():
     if xsd_xml:
         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'])
@@ -38,11 +50,12 @@ def is_valid():
         if res:
             ret = '<ok/>'
         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__':
-
     app.run(host=config['server']['host'], port=config['server']['port'])
\ No newline at end of file
diff --git a/utils.py b/utils.py
index 44aa9d2..d001765 100644
--- a/utils.py
+++ b/utils.py
@@ -1,6 +1,7 @@
 import json
 import git
 import requests
+from flask import Response
 from xml.etree import ElementTree as ET
 import xmlschema
 
@@ -14,18 +15,25 @@ def get_config_dict():
 def git_cmd(config):
     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):
     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
     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()
 
     return xsd_str
 
-def save_xsd(config, xsd_str, file_name):
-    file = open("{dir}/{file}".format (dir = config['xsd']['dir'], file=file_name), "w")
+def save_xsd(config, xsd_str, xsd_name):
+    path_file = get_xsd_path_file(config, xsd_name)
+    file = open(path_file, "w")
     file.write(xsd_str)  
     
     return 'ok'
@@ -46,4 +54,5 @@ def is_valid(xml_str, xsd_str):
     else:
         return False
     
-    
\ No newline at end of file
+def xml_response(xml_str):
+    return Response(xml_str, content_type='text/xml; charset=utf-8')
\ No newline at end of file
-- 
GitLab