X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=it%2Fotf.git;a=blobdiff_plain;f=a1-sdnc-vth%2Fapp%2Fhelpers%2Faction_helper.py;fp=a1-sdnc-vth%2Fapp%2Fhelpers%2Faction_helper.py;h=a4f7a3a7e1b6f7254c416cab7548b97ea7e30a65;hp=0000000000000000000000000000000000000000;hb=63b197b11b12a536254352f134262ad90f31593b;hpb=fa00af93f9173da630cd425fa8e0cbfaadf39a68 diff --git a/a1-sdnc-vth/app/helpers/action_helper.py b/a1-sdnc-vth/app/helpers/action_helper.py new file mode 100644 index 0000000..a4f7a3a --- /dev/null +++ b/a1-sdnc-vth/app/helpers/action_helper.py @@ -0,0 +1,42 @@ +import json +import ast +from app.helpers import response_helper as ResponseHelper +from flask import current_app, jsonify +from app.errors.bad_request_exception import BadRequestException +import requests + + +def execute_action(request, response_dict, config): + headers = ResponseHelper.create_headers(); + request_data = request.json + action_request = request_data.get("action").lower() + + creds = ResponseHelper.get_credentials(request_data, config) + proxies = ResponseHelper.get_proxies(config) + url = ResponseHelper.create_url(config=config, uri_path="/restconf/operations/A1-ADAPTER-API:"+action_request) +# ret_url = request.args.get('retURL') + + json_req = ast.literal_eval(request_data["action_data"]["jsonBody"]) + current_app.logger.info("Requesting Url: {}, body: {}, auth: {}, proxies: {}".format(url, json_req, creds, proxies)) + try: + res = requests.post(url, proxies=proxies, auth=creds, headers=headers, json=json_req) + response = { + "status_code":res.status_code, + "result": res.json() + } + except(json.decoder.JSONDecodeError): + response = { + "status_code":res.status_code, + "result": res.reason + } + except requests.exceptions.RequestException: + response = { + "status_code":504, + "result": "Something Happned" + } + finally: + response_dict['vthResponse']['resultData'] = response +# if ret_url is not None: +# ResponseHelper.sendCallback(ret_url,response_dict) +# return '',200 + return response_dict