添加授权列表
This commit is contained in:
@@ -3,18 +3,53 @@ from typing import Any, Dict, Optional
|
||||
from app.utils.douyinRequest import DouyinRequest
|
||||
from app.models.user_oauth import UserOAuth
|
||||
|
||||
class DouyinApi:
|
||||
def __init__(self, request: DouyinRequest):
|
||||
self.request = request
|
||||
|
||||
async def get_advertiser_list(self, oauth_id: str, params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||
class DouyinApi:
|
||||
def __init__(self):
|
||||
self.request = DouyinRequest()
|
||||
|
||||
async def get_advertiser_by_agent(self, oauth_id: str, params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||
if not oauth_id:
|
||||
raise RuntimeError('OAuth ID is not set.')
|
||||
|
||||
url = ""
|
||||
url = "https://api.oceanengine.com/open_api/2/agent/advertiser/select/"
|
||||
return await self.request.request_with_token_with_context(
|
||||
oauth_id,
|
||||
url,
|
||||
'GET',
|
||||
{'params': params or {}}
|
||||
)
|
||||
|
||||
async def upload_image_material(self, oauth_id: str, data: Optional[Dict[str, Any]] = None, files: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||
if not oauth_id:
|
||||
raise RuntimeError('OAuth ID is not set.')
|
||||
|
||||
url = "https://api.oceanengine.com/open_api/2/file/image/ad/"
|
||||
options: Dict[str, Any] = {}
|
||||
if data:
|
||||
options['data'] = data
|
||||
if files:
|
||||
options['files'] = files
|
||||
return await self.request.request_with_token_with_context(
|
||||
oauth_id,
|
||||
url,
|
||||
'POST',
|
||||
options
|
||||
)
|
||||
|
||||
async def upload_video_material(self, oauth_id: str, data: Optional[Dict[str, Any]] = None, files: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||
if not oauth_id:
|
||||
raise RuntimeError('OAuth ID is not set.')
|
||||
|
||||
url = "https://api.oceanengine.com/open_api/2/file/video/ad/"
|
||||
options: Dict[str, Any] = {}
|
||||
if data:
|
||||
options['data'] = data
|
||||
if files:
|
||||
options['files'] = files
|
||||
return await self.request.request_with_token_with_context(
|
||||
oauth_id,
|
||||
url,
|
||||
'POST',
|
||||
options
|
||||
)
|
||||
@@ -187,24 +187,31 @@ class DouyinRequest:
|
||||
url: str,
|
||||
method: str = 'GET',
|
||||
options: Optional[Dict[str, Any]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
) -> Any:
|
||||
options = options or {}
|
||||
token = await self.get_access_token(oauth_id)
|
||||
|
||||
for i in range(1, 6):
|
||||
for i in range(1, 2):
|
||||
try:
|
||||
headers = options.get('headers', {}).copy()
|
||||
headers['Access-Token'] = token
|
||||
headers.setdefault('Content-Type', 'application/json')
|
||||
|
||||
has_files = 'files' in options
|
||||
if has_files:
|
||||
# 移除可能错误设置的 Content-Type,让库自动生成 multipart 头
|
||||
headers.pop('Content-Type', None)
|
||||
else:
|
||||
headers.setdefault('Content-Type', 'application/json')
|
||||
|
||||
options['headers'] = headers
|
||||
|
||||
|
||||
response = await self.client.request(method, url, **options)
|
||||
response.raise_for_status()
|
||||
|
||||
|
||||
try:
|
||||
data = response.json()
|
||||
except json.JSONDecodeError:
|
||||
data = {'code': 0, 'data': response.text}
|
||||
data = {'code': 0, 'data': response.text, 'msg':'JSON解析失败'}
|
||||
|
||||
if 'code' not in data:
|
||||
await asyncio.sleep(i * 5)
|
||||
@@ -236,9 +243,18 @@ class DouyinRequest:
|
||||
continue
|
||||
|
||||
res = json.dumps(data) if 'data' in locals() else ''
|
||||
|
||||
options_log = {}
|
||||
if options:
|
||||
for key, value in options.items():
|
||||
if key == 'files':
|
||||
options_log[key] = {k: (v[0], 'bytes_content', v[2]) for k, v in value.items()}
|
||||
else:
|
||||
options_log[key] = value
|
||||
|
||||
raise RuntimeError(
|
||||
f'DouYin API request failed after 5 retries. '
|
||||
f'url:{url};oauthId:{oauth_id};options:{json.dumps(options)};response:{res}'
|
||||
f'url:{url};oauthId:{oauth_id};options:{json.dumps(options_log)};response:{res}'
|
||||
)
|
||||
|
||||
# 无token请求
|
||||
@@ -250,7 +266,7 @@ class DouyinRequest:
|
||||
) -> Dict[str, Any]:
|
||||
options = options or {}
|
||||
|
||||
for i in range(1, 6):
|
||||
for i in range(1, 2):
|
||||
try:
|
||||
headers = options.get('headers', {}).copy()
|
||||
headers.setdefault('Content-Type', 'application/json')
|
||||
|
||||
Reference in New Issue
Block a user