68 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| from flask import Flask
 | |
| from flask import request
 | |
| from lxml import etree
 | |
| import json
 | |
| import time
 | |
| import Parser
 | |
| 
 | |
| 
 | |
| app = Flask(__name__)
 | |
| 
 | |
| 
 | |
| def process_xml(request: request, type: str) -> str:
 | |
|     """Function to process 
 | |
| 
 | |
|     :param request: Received request
 | |
|     :type request: request
 | |
|     :param type: Type of needed processing: xsd, xslt or xpath
 | |
|     :type type: str
 | |
|     :raises ValueError: is raised when type is different than those provided above
 | |
|     :return: response JSON converted to string and response code
 | |
|     :rtype: str, int
 | |
|     """
 | |
|     start = time.time_ns()
 | |
|     code = 200
 | |
|     response_json = dict()
 | |
|     try:
 | |
|         request_data = json.loads(request.get_data(as_text=True))
 | |
|         data = request_data['data']
 | |
|         process = request_data['process']
 | |
|         if (type == "xsd"):
 | |
|             response_json['result'] = Parser.xsd(data, process)
 | |
|         elif (type == "xslt"):
 | |
|             response_json['result'] = Parser.xslt(data, process)
 | |
|         elif (type == "xpath"):
 | |
|             response_json['result'] = Parser.xpath(data, process)
 | |
|         else:
 | |
|             raise ValueError("Valid operation types are: xsd, xslt, xpath")
 | |
| 
 | |
|         response_json['status'] = "OK"
 | |
|     except KeyError as e:
 | |
|         response_json['result'] = "Missing key: " + str(e)
 | |
|         response_json['status'] = "ERR"
 | |
|         code = 400
 | |
|     except Exception as e:
 | |
|         response_json['result'] = str(e)
 | |
|         response_json['status'] = "ERR"
 | |
|         code = 400
 | |
|     finally:
 | |
|         exec_time = (time.time_ns() - start) / 10**6
 | |
|         response_json['time'] = f"{exec_time:.03f}"
 | |
|         response_json['processor'] = "libxml2 over lxml"
 | |
|         return json.dumps(response_json), code
 | |
| 
 | |
| 
 | |
| @app.route("/xpathpost", methods=["POST"])
 | |
| def xpath():
 | |
|     return process_xml(request, "xpath")
 | |
| 
 | |
| @app.route("/xsdpost", methods=["POST"])
 | |
| def xsd():
 | |
|     return process_xml(request, "xsd")
 | |
| 
 | |
| @app.route("/xsltpost", methods=["POST"])
 | |
| def xslt():
 | |
|     return process_xml(request, "xslt")
 | |
| 
 | |
| if __name__ == "__main__":
 | |
|     app.run() |