import os
import requests

if __name__ == "__main__":
    url = "http://store.evmiot.com/api/v1/evm_store/application/build"
    datas = {
        "access_key": "69b80cc6-4ea0-11eb-904e-a85e459dc84f",
        "app_name": "test",
        "app_version": "v1.0",
        "category": "test",
    }

    # upload_files = [
    #     ("field1" , ("filename1", open("filePath1", "rb"))),
    #     ("field1" , ("filename2", open("filePath2", "rb"), "image/png")), 
    #     ("field1" , open("filePath3", "rb")),
    #     ("field1" , open("filePath4", "rb").read())
    # ]
    upload_files = []
    for root, dirs, files in os.walk("./watch_launcher"):
        for f in files:
            upload_files.append(("binfiles", (f, open(os.sep.join([root, f]), "rb"))))

    # print(upload_files)

    res = requests.post(url, datas, files = upload_files)
    result = res.json()

    print(result)

    if result['code'] == 200:
        down_res = requests.get(url=result['data']['app_url'])
        with open(result['data']['app_file'], "wb") as fd:
            fd.write(down_res.content)