1
0
forked from erp-dev/erp

fear: added imageBase64 param for image search api

This commit is contained in:
2026-01-20 14:29:49 +08:00
parent 7e4e5e9cc1
commit 90a70d6f6d
3 changed files with 70 additions and 15 deletions

View File

@@ -41,6 +41,37 @@ class TiiaSearchImageAPITestCase(TestCase):
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertEqual(resp.json(), {'Candidates': [{'EntityId': '1', 'Score': 99}]})
mock_search.assert_called()
@patch('api_v1.views.tiia.search_image_url_in_tencent_tiia')
def test_search_image_success_base64(self, mock_search):
self.client.force_authenticate(self.user)
mock_search.return_value = {'Candidates': []}
resp = self.client.post(
'/api/v1/tiia/search-image/',
data={'imageBase64': 'iVBORw0KGgoAAAANSUhEUgAAAAUA'}, # dummy
format='json',
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
mock_search.assert_called()
@patch('api_v1.views.tiia.search_image_url_in_tencent_tiia')
def test_search_image_both_params_url_wins(self, mock_search):
self.client.force_authenticate(self.user)
mock_search.return_value = {'Candidates': []}
resp = self.client.post(
'/api/v1/tiia/search-image/',
data={
'imageUrl': 'https://example.com/a.png',
'imageBase64': 'iVBORw0KGgoAAAANSUhEUgAAAAUA',
},
format='json',
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
_, kwargs = mock_search.call_args
self.assertEqual(kwargs.get('image_url'), 'https://example.com/a.png')
def test_search_image_missing_image_url(self):
self.client.force_authenticate(self.user)

View File

@@ -34,16 +34,16 @@ class TiiaSearchImageView(APIView):
permission_classes = [IsAuthenticated]
def post(self, request):
image_url = (
request.data.get('imageUrl')
or request.data.get('ImageUrl')
or request.data.get('image_url')
or request.data.get('ImageURL')
)
# Keep API params consistent with existing style for this endpoint:
# - imageUrl
# - imageBase64
image_url = request.data.get('imageUrl')
image_base64 = request.data.get('imageBase64')
try:
limiter = SimpleRateLimiter(qps=float(getattr(settings, 'TENCENTCLOUD_TIIA_QPS', 10)))
resp = search_image_url_in_tencent_tiia(
image_url=str(image_url or ''),
image_url=str(image_url or '') if image_url is not None else None,
image_base64=str(image_base64 or '') if image_base64 is not None else None,
rate_limiter=limiter,
)
return Response(resp, status=status.HTTP_200_OK)