import os
import requests
from contextlib import ExitStack

url = 'http://192.168.10.217:10001/img_search'

file_paths = [
    # r'D:\Amazon-Selection\pyspark_job\image_search\img\t1_1.png',
    r'D:\Amazon-Selection\pyspark_job\image_search\img\t1_2.png',
]

data = {
    'site_name': 'us',
    'img_type': 'amazon_inv',
    'search_key': 'file',
    'search_value': '',
    'top_k': '5'
}

with ExitStack() as stack:
    files = []
    for idx, file_path in enumerate(file_paths, start=1):
        files.append(
            (
                f'file{idx}',
                (
                    os.path.basename(file_path),
                    stack.enter_context(open(file_path, 'rb')),
                    'image/png'
                )
            )
        )

    response = requests.post(url, files=files, data=data, timeout=120)

# print(response.status_code)
# print(response.text)

# if response.status_code == 200:
#     print(response.json())
if response.status_code == 200:
    result_list = response.json()

    for item in result_list:
        print("id:", item.get("id"))
        print("img_id:", item.get("img_id"))
        print("img_type:", item.get("img_type"))
        print("similarity:", item.get("similarity"))
        print("img_url:", item.get("img_url"))
        print("-" * 80)
else:
    print(f"Error: {response.status_code}, {response.text}")