Removed duplicate code
This commit is contained in:
		| @@ -8,15 +8,34 @@ import Parser | ||||
|  | ||||
| app = Flask(__name__) | ||||
|  | ||||
| def process_xml(request, type): | ||||
|  | ||||
| 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 | ||||
|     :rtype: str | ||||
|     """ | ||||
|     start = time.time_ns() | ||||
|  | ||||
|     response = dict() | ||||
|     try: | ||||
|         request_data = json.loads(request.get_data(as_text=True)) | ||||
|         xml = request_data['data'] | ||||
|         xsd = request_data['process'] | ||||
|         response['result'] = Parser.xsd(xml, xsd) | ||||
|         data = request_data['data'] | ||||
|         process = request_data['process'] | ||||
|         if (type == "xsd"): | ||||
|             response['result'] = Parser.xsd(data, process) | ||||
|         elif (type == "xslt"): | ||||
|             response['result'] = Parser.xslt(data, process) | ||||
|         elif (type == "xpath"): | ||||
|             response['result'] = Parser.xpath(data, process) | ||||
|         else: | ||||
|             raise ValueError("Valid operation types are: xsd, xslt, xpath") | ||||
|  | ||||
|         response['status'] = "OK" | ||||
|     except KeyError as e: | ||||
|         response['result'] = "Missing key: " + str(e) | ||||
| @@ -33,26 +52,7 @@ def process_xml(request, type): | ||||
|  | ||||
| @app.route("/xpath", methods=["POST"]) | ||||
| def xpath(): | ||||
|     start = time.time_ns() | ||||
|      | ||||
|     response = dict() | ||||
|     try: | ||||
|         request_data = json.loads(request.get_data(as_text=True)) | ||||
|         xml = request_data['data'] | ||||
|         xpath = request_data['process'] | ||||
|         response['result'] = Parser.xpath(xml, xpath) | ||||
|         response['status'] = "OK" | ||||
|     except KeyError as e: | ||||
|         response['result'] = "Missing key: " + str(e) | ||||
|         response['status'] = "ERR" | ||||
|     except Exception as e: | ||||
|         response['result'] = str(e) | ||||
|         response['status'] = "ERR" | ||||
|     finally: | ||||
|         exec_time = (time.time_ns() - start) / 10**6 | ||||
|         response['time'] = f"{exec_time:.03f}" | ||||
|         response['processor'] = "libxml2 over lxml" | ||||
|         return json.dumps(response) | ||||
|     return process_xml(request, "xpath") | ||||
|  | ||||
| @app.route("/xsd", methods=["POST"]) | ||||
| def xsd(): | ||||
| @@ -60,23 +60,7 @@ def xsd(): | ||||
|  | ||||
| @app.route("/xslt", methods=["POST"]) | ||||
| def xslt(): | ||||
|     start = time.time_ns() | ||||
|     return process_xml(request, "xslt") | ||||
|  | ||||
|     response = dict() | ||||
|     try: | ||||
|         request_data = json.loads(request.get_data(as_text=True)) | ||||
|         xml = request_data['data'] | ||||
|         xslt = request_data['process'] | ||||
|         response['result'] = Parser.xslt(xml, xslt) | ||||
|         response['status'] = "OK" | ||||
|     except KeyError as e: | ||||
|         response['result'] = "Missing key: " + str(e) | ||||
|         response['status'] = "ERR" | ||||
|     except Exception as e: | ||||
|         response['result'] = str(e) | ||||
|         response['status'] = "ERR" | ||||
|     finally: | ||||
|         exec_time = (time.time_ns() - start) / 10**6 | ||||
|         response['time'] = f"{exec_time:.03f}" | ||||
|         response['processor'] = "libxml2 over lxml" | ||||
|         return json.dumps(response) | ||||
| if __name__ == "__main__": | ||||
|     app.run() | ||||
		Reference in New Issue
	
	Block a user