1
0
forked from erp-dev/erp

feat: search image by tencent tiia

This commit is contained in:
2026-01-18 14:44:29 +08:00
parent a39b9648a4
commit 7e4e5e9cc1
5 changed files with 290 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
"""
TIIA SearchImage simplified API tests.
"""
from django.contrib.auth import get_user_model
from django.test import TestCase
from rest_framework import status
from rest_framework.test import APIClient
from unittest.mock import patch
User = get_user_model()
class TiiaSearchImageAPITestCase(TestCase):
def setUp(self):
self.client = APIClient()
self.user = User.objects.create_user(
username='test_tiia_search_user',
password='testpass123',
email='test_tiia_search@example.com',
)
def test_search_image_unauthenticated(self):
resp = self.client.post(
'/api/v1/tiia/search-image/',
data={'imageUrl': 'https://example.com/a.png'},
format='json',
)
self.assertEqual(resp.status_code, status.HTTP_401_UNAUTHORIZED)
@patch('api_v1.views.tiia.search_image_url_in_tencent_tiia')
def test_search_image_success(self, mock_search):
self.client.force_authenticate(self.user)
mock_search.return_value = {'Candidates': [{'EntityId': '1', 'Score': 99}]}
resp = self.client.post(
'/api/v1/tiia/search-image/',
data={'imageUrl': 'https://example.com/a.png'},
format='json',
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertEqual(resp.json(), {'Candidates': [{'EntityId': '1', 'Score': 99}]})
def test_search_image_missing_image_url(self):
self.client.force_authenticate(self.user)
resp = self.client.post('/api/v1/tiia/search-image/', data={}, format='json')
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn('error', resp.json())

57
api_v1/views/tiia.py Normal file
View File

@@ -0,0 +1,57 @@
"""
Tencent Cloud TIIA simplified APIs.
Currently provides:
- SearchImage (by ImageUrl) with fixed groupId/region from settings.
"""
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from django.conf import settings
from api_v1.utils.tencentcloud_tiia import SimpleRateLimiter, search_image_url_in_tencent_tiia
class TiiaSearchImageView(APIView):
"""
Tencent Cloud TIIA: SearchImage (simplified)
POST /api/v1/tiia/search-image/
Body:
{
"imageUrl": "https://example.com/xxx.png"
}
Notes:
- groupId/region/endpoint are configured via Django settings (TENCENTCLOUD_TIIA_*).
- Enforces QPS using settings.TENCENTCLOUD_TIIA_QPS (default 10).
"""
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')
)
try:
limiter = SimpleRateLimiter(qps=float(getattr(settings, 'TENCENTCLOUD_TIIA_QPS', 10)))
resp = search_image_url_in_tencent_tiia(
image_url=str(image_url or ''),
rate_limiter=limiter,
)
return Response(resp, status=status.HTTP_200_OK)
except ValueError as e:
return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
except Exception as e:
return Response(
{'error': '腾讯云搜图失败', 'message': str(e)},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)