通过api方式调用自动化平台,统计自动化数据

1. 方向

1.1. 统计效能节省

复用自动化平台统计能力方案

1.2. 实时报告查看

走新开发发送钉钉实时报告能力

2. 获取ms自动化平台的key

2.1. ms 入口:

2.2. key信息:

2.2.1. Access Key

2.2.2. Secrec Key

3. 加密方式 - AES

3.1. AES加密方式实现

def aes_encrypt(src: str, secret_key: str, iv: str) -> str:
    if not secret_key:
        raise ValueError("secret_key is empty")

    try:
        # Convert secret_key and iv to bytes
        secret_key = secret_key.encode('utf-8')
        iv = iv.encode('utf-8')

        # Create AES cipher object in CBC mode with PKCS5 padding
        cipher = AES.new(secret_key, AES.MODE_CBC, iv)

        # Pad the input data to a multiple of AES block size
        padded_data = pad(src.encode('utf-8'), AES.block_size)

        # Encrypt the data
        encrypted = cipher.encrypt(padded_data)

        # Return the encrypted data as a base64-encoded string
        return base64.b64encode(encrypted).decode('utf-8')
    except Exception as e:
        raise RuntimeError("AES encrypt error") from e

3.2. 生成签名,以header方式放置进api

def get_headers(access_key: str, secret_key: str) -> dict:
    timestamp = int(round(time.time() * 1000))

    combox_key = access_key + '|padding|' + str(timestamp)
    signature = aes_encrypt(combox_key, secret_key, access_key)

    return {'accessKey': access_key, 'signature': signature}

4. 自动化平台api调用方法

class MeterSphere:
    def __init__(self):
        self.domain = "https://ms.chuanyinet.com"
        self.access_key = "access_key "
        self.secret_key = "secret_key "

    def _request(self, path: str, body: dict = None) -> Union[dict, list]:
        """
        :param path:
        :param body: if body is empty, will use get method
        :return:
        """
        url = f"{self.domain.rstrip('/')}/{path.lstrip('/')}"

        headers = {'Content-Type': 'application/json', 'ACCEPT': 'application/json'}
        headers.update(get_headers(self.access_key, self.secret_key))

        logging.info("request url: %s", url)
        logging.info("request headers: %s", headers)
        if body:
            resp = requests.post(url, headers=headers, json=body)
        else:
            resp = requests.get(url, headers=headers)
        return resp.json()

    def post_factory_api(self):
        res = self._request('/data-factory/data/run', body={
            "id": 539,
            "params": [],
            "mode": "run",
            "env": "TEST"
        })
        logging.warning("post_factory_api: %s", res)
        return res

5. 主运行入口调用该方法

if __name__ == "__main__":
    # 跑用例之前,调用自动化平台造数接口api,实现自动化数据统计
    ms = MeterSphere()
    ms.post_factory_api()

    pytest.main(['-s', '-v', '.\\testcases\data', '--alluredir', 'allure_result', "--clean-alluredir"])
    os.system(r"allure generate .\allure_result\ -o .\allure_report\ --clean")

欢迎关注公众号,与Joker一起探索测试之道。

参考记录:

https://www.cnblogs.com/jinbangyi/articles/17586574.html

https://blog.51cto.com/u_15922911/6010351

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Anthony_路人甲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值