From b0437fdeb49776c881764ea173d7a78c8ff631a2 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Thu, 10 Sep 2026 17:42:18 +0100 Subject: [PATCH] Type Cloud Reco multipart fields --- src/vws/async_query.py | 46 ++++++++++++++++++++++++++---------------- src/vws/query.py | 33 +++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/vws/async_query.py b/src/vws/async_query.py index 752f6e398..209ef1f14 100644 --- a/src/vws/async_query.py +++ b/src/vws/async_query.py @@ -4,9 +4,10 @@ import json from http import HTTPMethod, HTTPStatus -from typing import Any, Self +from typing import Self from beartype import BeartypeConf, beartype +from urllib3.fields import RequestField from urllib3.filepost import encode_multipart_formdata from vws_auth_tools import authorization_header, rfc_1123_date @@ -131,23 +132,34 @@ async def query( targets. """ image_content = _get_image_data(image=image) - body: dict[str, Any] = { # pyrefly: ignore [explicit-any] - "image": ( - "image.jpeg", - image_content, - "image/jpeg", - ), - "max_num_results": ( - None, - max_num_results, - "text/plain", - ), - "include_target_data": ( - None, - include_target_data.value, - "text/plain", + max_num_results_field = RequestField( + name="max_num_results", + data=str(object=max_num_results), + ) + max_num_results_field.make_multipart( + content_disposition="form-data", + content_type="text/plain", + ) + include_target_data_field = RequestField( + name="include_target_data", + data=include_target_data.value, + ) + include_target_data_field.make_multipart( + content_disposition="form-data", + content_type="text/plain", + ) + body = [ + RequestField.from_tuples( + fieldname="image", + value=( + "image.jpeg", + image_content, + "image/jpeg", + ), ), - } + max_num_results_field, + include_target_data_field, + ] date = rfc_1123_date() request_path = "/v1/query" content, content_type_header = encode_multipart_formdata(fields=body) diff --git a/src/vws/query.py b/src/vws/query.py index 49a6691cf..6c25a745b 100644 --- a/src/vws/query.py +++ b/src/vws/query.py @@ -2,9 +2,9 @@ import json from http import HTTPMethod, HTTPStatus -from typing import Any from beartype import BeartypeConf, beartype +from urllib3.fields import RequestField from urllib3.filepost import encode_multipart_formdata from vws_auth_tools import authorization_header, rfc_1123_date @@ -112,15 +112,30 @@ def query( An ordered list of target details of matching targets. """ image_content = _get_image_data(image=image) - body: dict[str, Any] = { # pyrefly: ignore [explicit-any] - "image": ("image.jpeg", image_content, "image/jpeg"), - "max_num_results": (None, max_num_results, "text/plain"), - "include_target_data": ( - None, - include_target_data.value, - "text/plain", + max_num_results_field = RequestField( + name="max_num_results", + data=str(object=max_num_results), + ) + max_num_results_field.make_multipart( + content_disposition="form-data", + content_type="text/plain", + ) + include_target_data_field = RequestField( + name="include_target_data", + data=include_target_data.value, + ) + include_target_data_field.make_multipart( + content_disposition="form-data", + content_type="text/plain", + ) + body = [ + RequestField.from_tuples( + fieldname="image", + value=("image.jpeg", image_content, "image/jpeg"), ), - } + max_num_results_field, + include_target_data_field, + ] date = rfc_1123_date() request_path = "/v1/query" content, content_type_header = encode_multipart_formdata(fields=body)