cd_e2e_monitoring_config/yamo/rest/RestFactory.py

62 lines
2.3 KiB
Python

import json
import yaml
from rest.RestCall import RestCall
from context.Context import Context
import coloredlogs, logging
logger = logging.getLogger(__name__)
class RestFactory:
#def buildRequests(self, name, requestConfig, body):
def __readBodies(self,execConfig, restPath):
bodies={}
for setting in execConfig['config']:
for key, value in setting.items():
body=json.load(open(restPath+"/"+value))
bodies[key]=body
return bodies
def __mapExecutionConfig(self, restName,projectRootPath):
#execConfig=self.readExecutionConfig(projectRootPath+"/"+restName+"/"+restName+".yaml")
mappedExecConfig={}
executionConfigFilePath=projectRootPath+"/"+restName+"/"+restName+".yaml"
executionConfigFile=open(executionConfigFilePath)
execConfig=yaml.safe_load(executionConfigFile)
bodies = self.__readBodies(execConfig,projectRootPath+"/"+restName)
del execConfig['config']
for key, values in execConfig.items():
envSplit = key.split('.')
if len(envSplit) > 1:
if envSplit[1] not in mappedExecConfig:
mappedExecConfig[envSplit[1]]=[]
tmpObj= {}
#tmpBody = bodies[envSplit[0]]
#tmpBody['__yamo__values'] = values
tmpObj['variables']=values
tmpObj['body']=bodies[envSplit[0]]
mappedExecConfig[envSplit[1]].append(tmpObj)
else:
self.logger.warning("Missing Environment config for "+key+" in "+executionConfigFilePath+", will be ignored")
return mappedExecConfig
def getRest(self,restName, projectRootPath):
try:
#execConfig=self.readExecutionConfig(projectRootPath+"/"+name+"/"+name+".yaml")
mappedExecConfig = self.__mapExecutionConfig(restName, projectRootPath)
return RestCall(restName, self.data[restName], mappedExecConfig,Context.getInstance())
except:
return None
def readRestConfig(self, pathToRestConfig):
f = open(pathToRestConfig,)
self.data=json.load(f)
def __init__(self, pathToRestConfig):
self.readRestConfig(pathToRestConfig)