import requests
from middleware import UnicornException
def forward_request(target_url, params=None, method="GET", headers=None):
    """
    转发HTTP请求到目标URL

    :param target_url: 目标接口的URL
    :param params: 请求参数（字典格式）
    :param method: 请求方法（GET或POST）
    :param headers: 请求头（字典格式）
    :return: 目标接口的响应
    """
    try:
        if method.upper() == "GET":
            response = requests.get(target_url, params=params, headers=headers)
        elif method.upper() == "POST":
            response = requests.post(target_url, data=params, headers=headers)
        else:
            raise UnicornException("仅支持GET和POST方法")
        return response.json()  # 假设目标接口返回JSON格式的数据
    except requests.RequestException as e:
        raise UnicornException(e)
