1
0
forked from erp-dev/erp
Files
erpnew/api_v1/views/tiia.py

86 lines
3.0 KiB
Python

"""
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):
# Keep API params consistent with existing style for this endpoint:
# - imageUrl
# - imageBase64
image_url = request.data.get('imageUrl')
image_base64 = request.data.get('imageBase64')
limit = request.data.get('limit', None)
offset = request.data.get('offset', None)
match_threshold = request.data.get('matchThreshold', None)
try:
if limit is not None and str(limit).strip() != '':
limit = int(limit)
# TencentCloud SearchImage: default 10, max 100
if limit <= 0 or limit > 100:
raise ValueError('limit 必须在 1~100 之间')
else:
limit = None
if offset is not None and str(offset).strip() != '':
offset = int(offset)
if offset < 0:
raise ValueError('offset 必须 >= 0')
else:
offset = None
if match_threshold is not None and str(match_threshold).strip() != '':
match_threshold = int(match_threshold)
if match_threshold < 0 or match_threshold > 100:
raise ValueError('matchThreshold 必须在 0~100 之间')
else:
match_threshold = None
limiter = SimpleRateLimiter(qps=float(getattr(settings, 'TENCENTCLOUD_TIIA_QPS', 10)))
resp = search_image_url_in_tencent_tiia(
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,
limit=limit,
offset=offset,
match_threshold=match_threshold,
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,
)