import requests
import time

from config import base_url, coze_token





def upload_file(file_path,_coze_token =coze_token):
    url = f"{base_url}/files/upload"
    headers = {
        "Authorization": f"Bearer {_coze_token}"
    }
    print(file_path)
    with open(file_path, 'rb') as f:
        files = {
            'file': f
        }
        # with open(f"{time.time()".replace(".","_")+".png","wb") as file:
        #     file.write(f.read())
        # allow_redirects=True 模拟 curl 的 --location，默认即为 True
        response = requests.post(url, headers=headers, files=files, allow_redirects=True)
    data = response.json()
    print("上传图片->>",data)
    return data

def workflow_run(workflow_id, parameters,_coze_token = coze_token,is_async=True):
    url = f"{base_url}/workflow/run"
    headers = {
        "Authorization": f"Bearer {_coze_token}",
        "Content-Type": "application/json"
    }

    # 请求体（JSON 格式）
    payload = {
        "parameters": parameters,
        # "parameters": {
        #     "name": "包包",
        #     "ratio": "3:4",
        #     "menu": 3,
        #     "resolution": "2K",
        #     "count": 1,
        #     "desc": "1、一个中国美女手上挎着这个包，2、站在商场内",
        #     "language": "中文",
        #     "style": "极简高级"
        # },
        "workflow_id": workflow_id,
        "is_async" : is_async,
    }
    # 发送 POST 请求（使用 json 参数自动序列化并设置 Content-Type）
    response = requests.post(url, headers=headers, json=payload)
    data = response.json()

    print("图片生成参数->>",payload)
    print("图片生成->>",data)
    return data


def query_result(workflow_id, execute_id,_coze_token=coze_token):
    url = f"{base_url}/workflows/{workflow_id}/run_histories/{execute_id}"
    headers = {
        "Authorization": f"Bearer {_coze_token}",
        "Content-Type": "application/json"
    }
    response = requests.get(url, headers=headers)
    data = response.json()
    print(f"【{execute_id}】结果查询->>",data)
    return data

if __name__ == '__main__':
    from config import  workflow_id

    parameters = {
    "images": [
      {
        "file_id": "7612667079497613350",

      },{
            "file_id": "7612667042990768174",
        },{
            "file_id": "7612667111332577332",
        }
    ]
  }
    # resp = workflow_run("7607680760402100258",parameters,_coze_token="sat_ZVNLR9Om54A3iMlJWAasHF9kbtDZnR3BjwjAxRwe8x7igEW446y5ROyVlW1UlpVX")
    # print(resp)

    # resp = query_result("7607680760402100258","7612668958797447187",_coze_token="sat_ZVNLR9Om54A3iMlJWAasHF9kbtDZnR3BjwjAxRwe8x7igEW446y5ROyVlW1UlpVX")
    # print(resp)
    resp = upload_file("D:\私单交付\maixiang_AI\测试图片数据\IMG_2686.JPG",_coze_token="sat_ZVNLR9Om54A3iMlJWAasHF9kbtDZnR3BjwjAxRwe8x7igEW446y5ROyVlW1UlpVX")
    print(resp)