Skip to content

Commit 1705774

Browse files
committed
refactor(g4f/client/client.py): Simplify AsyncClient methods
1 parent 75316a2 commit 1705774

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

g4f/client/client.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -144,39 +144,32 @@ async def async_images(self) -> Images:
144144
class AsyncClient(Client):
145145
"""Legacy AsyncClient that redirects to the main Client class.
146146
This class exists for backwards compatibility."""
147-
147+
148148
def __init__(self, *args, **kwargs):
149149
import warnings
150150
warnings.warn(
151-
"AsyncClient is deprecated and will be removed in a future version. "
151+
"AsyncClient is deprecated and will be removed in future versions."
152152
"Use Client instead, which now supports both sync and async operations.",
153153
DeprecationWarning,
154154
stacklevel=2
155155
)
156156
super().__init__(*args, **kwargs)
157-
self.chat = Chat(self)
158-
self._images = Images(self)
159-
self.completions = Completions(self)
160-
161-
@property
162-
def images(self) -> 'Images':
163-
return self._images
164157

165-
async def async_create(self, *args, **kwargs) -> Union['ChatCompletion', AsyncIterator['ChatCompletionChunk']]:
166-
response = await super().async_create(*args, **kwargs)
167-
async for result in response:
168-
return result
158+
async def async_create(self, *args, **kwargs):
159+
"""Asynchronous create method that calls the synchronous method."""
160+
return await super().async_create(*args, **kwargs)
169161

170-
async def async_generate(self, *args, **kwargs) -> 'ImagesResponse':
162+
async def async_generate(self, *args, **kwargs):
163+
"""Asynchronous image generation method."""
171164
return await super().async_generate(*args, **kwargs)
172165

173-
async def _fetch_image(self, url: str) -> bytes:
174-
async with ClientSession() as session:
175-
async with session.get(url) as resp:
176-
if resp.status == 200:
177-
return await resp.read()
178-
else:
179-
raise Exception(f"Failed to fetch image from {url}, status code {resp.status}")
166+
async def async_images(self) -> Images:
167+
"""Asynchronous access to images."""
168+
return await super().async_images()
169+
170+
async def async_fetch_image(self, url: str) -> bytes:
171+
"""Asynchronous fetching of an image by URL."""
172+
return await self._fetch_image(url)
180173

181174
class Completions:
182175
def __init__(self, client: Client, provider: ProviderType = None):

0 commit comments

Comments
 (0)