tools.py 1.31 KB
Newer Older
1 2 3 4 5
#coding:utf-8

import json
import logging
import requests
jiaqiying committed
6
from config import BaseConfig, BMCConfig, BmyConfig
7 8 9 10 11 12 13 14 15 16 17 18 19 20


def request_main(url, headers, method, data):
    """封装requests的通用请求方法"""
    res = None
    if headers == None or headers == {}:
        # 如果传的headers为空,使用通用headers
        headers = BaseConfig.headers
    header_content_type = headers["Content-Type"]

    try:
        if method.upper() == "GET":
            res = requests.get(url=url, headers=headers, params=data)
        elif method.upper() == "POST":
jiaqiying committed
21 22
            if header_content_type in ["application/x-www-form-urlencoded"]:
                res = requests.post(url=url, headers=headers, data=data)
23 24 25 26 27 28 29 30
            elif header_content_type == "application/json":
                res = requests.post(url=url, headers=headers, data=json.dumps(data))
    except Exception as e:
        logging.log(str(e))
        raise Exception

    if res != None:
        return res.json()
31 32 33 34
    return res


def get_case_dir(product_name):
jiaqiying committed
35
    """根据传入的产品名来运行对应产品的测试用例目录"""
jiaqiying committed
36
    test_case_dir = BaseConfig.name
37 38
    if product_name == BMCConfig.name:
        test_case_dir = BMCConfig.test_case_dir
jiaqiying committed
39 40
    if product_name == BmyConfig.name:
        test_case_dir = BmyConfig.test_case_dir
41
    return test_case_dir