-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.py
More file actions
228 lines (190 loc) · 7.45 KB
/
client.py
File metadata and controls
228 lines (190 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""
Kling AI API client implementation.
"""
from __future__ import annotations
import json
import logging
from typing import Any, TypeVar
import httpx
from pydantic import BaseModel, ValidationError
from tenacity import (
retry,
retry_if_exception_type,
stop_after_attempt,
wait_exponential,
)
from .api.image_to_video.image_to_video import ImageToVideoAPI
from .api.multi_image_to_video.multi_image_to_video import MultiImageToVideoAPI
from .api.text_to_video.text_to_video import TextToVideoAPI
from .api.video_extension.video_extension import VideoExtensionAPI
from .config import KlingConfig
# Type variable for generic model parsing
T = TypeVar("T", bound=BaseModel)
# Configure logging
logger = logging.getLogger(__name__)
class KlingSingletonAPIError(Exception):
"""Base exception for Kling API errors."""
def __init__(self, message: str, status_code: int | None = None):
self.status_code = status_code
super().__init__(message)
class KlingClient:
"""
Singleton client for interacting with the Kling AI API.
Attributes:
text_to_video (TextToVideoAPI): API for text-to-video generation tasks.
multi_image_to_video (MultiImageToVideoAPI): API for multi-image to video generation tasks.
image_to_video (ImageToVideoAPI): API for image-to-video generation tasks.
video_extension (VideoExtensionAPI): API for extending existing videos with AI.
Usage:
config = KlingConfig(...)
client = KlingClient(config)
# All subsequent instantiations return the same instance
"""
_instance: KlingClient | None = None
_initialized: bool = False
def __new__(cls, config: KlingConfig) -> KlingClient:
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self, config: KlingConfig):
if self._initialized:
return
self.config = config
self.base_url = config.base_url.rstrip("/")
self.timeout = config.timeout
self.max_retries = config.max_retries
self._client = self._create_http_client()
# Register API subclients here (add more as needed)
try:
self.text_to_video = TextToVideoAPI(self)
self.multi_image_to_video = MultiImageToVideoAPI(self)
self.image_to_video = ImageToVideoAPI(self)
self.video_extension = VideoExtensionAPI(self._client)
except ImportError as e:
logger.warning(f"Failed to import one or more API modules: {e}")
self.text_to_video = None # Could not import TextToVideoAPI
self.multi_image_to_video = None # Could not import MultiImageToVideoAPI
self.image_to_video = None # Could not import ImageToVideoAPI
self.video_extension = None # Could not import VideoExtensionAPI
# todo: Register additional subclients
self._initialized = True
def _create_http_client(self) -> httpx.AsyncClient:
"""Create and configure an HTTP client.
Returns:
Configured httpx.AsyncClient instance
"""
return httpx.AsyncClient(
base_url=self.base_url,
timeout=self.timeout,
headers={
"Authorization": f"Bearer {self.config.api_key}",
"Content-Type": "application/json",
"Accept": "application/json",
},
)
async def close(self) -> None:
"""Close the HTTP client."""
if hasattr(self, "_client") and self._client:
await self._client.aclose()
async def __aenter__(self):
"""Async context manager entry."""
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""Async context manager exit - ensure client is closed."""
await self.close()
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=1, max=10),
retry=retry_if_exception_type(
(httpx.NetworkError, httpx.TimeoutException, httpx.HTTPStatusError)
),
)
async def _request(
self,
method: str,
endpoint: str,
**kwargs,
) -> dict[str, Any]:
"""Make an HTTP request to the Kling API with retries.
Args:
method: HTTP method (GET, POST, etc.)
endpoint: API endpoint path (e.g., "/v1/videos/text2video")
**kwargs: Additional arguments to pass to the request
Returns:
Parsed JSON response as a dictionary
Raises:
KlingAPIError: If the API returns an error response
httpx.HTTPStatusError: For HTTP errors
httpx.RequestError: For request errors
"""
url = f"{self.base_url}{endpoint}"
logger.debug("Making %s request to %s", method, url)
try:
response = await self._client.request(method, url, **kwargs)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
error_msg = f"HTTP error {e.response.status_code}"
try:
error_data = e.response.json()
error_msg = error_data.get("message", error_msg)
except (json.JSONDecodeError, AttributeError):
error_msg = f"{error_msg}: {e.response.text}"
logger.error(
"%s: %s",
error_msg,
str(e),
extra={"status_code": e.response.status_code},
)
raise KlingSingletonAPIError(error_msg, status_code=e.response.status_code) from e
except httpx.RequestError as e:
logger.error("Request failed: %s", str(e))
raise KlingSingletonAPIError(f"Request failed: {str(e)}") from e
async def _get_paginated(
self,
endpoint: str,
model: type[T],
page_num: int = 1,
page_size: int = 30,
**params,
) -> list[T]:
"""Get paginated results from the API.
Args:
endpoint: API endpoint path
model: Pydantic model to parse the response data
page_num: Page number to fetch
page_size: Number of items per page
**params: Additional query parameters
Returns:
List of parsed model instances
"""
params.update({"pageNum": page_num, "pageSize": page_size})
response = await self._request("GET", endpoint, params=params)
if not isinstance(response.get("data"), list):
return []
return [model.model_validate(item) for item in response["data"]]
async def _handle_response(
self,
response: dict[str, Any],
model: type[T],
) -> T:
"""Handle API response and parse it using the provided model.
Args:
response: Raw API response
model: Pydantic model to parse the response data
Returns:
Parsed model instance
Raises:
KlingAPIError: If the response indicates an error
ValidationError: If response data doesn't match the model
"""
if response.get("code") != 0:
raise KlingSingletonAPIError(
response.get("message", "Unknown error"),
status_code=response.get("code"),
)
try:
return model.parse_obj(response.get("data", {}))
except ValidationError as e:
logger.error("Failed to validate response: %s", str(e))
raise