mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Merge remote-tracking branch 'origin/dev-3.x-download' into dev-3.x
This commit is contained in:
commit
a41bccddf9
5 changed files with 66 additions and 6 deletions
|
|
@ -1,8 +1,8 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Callable, Optional, TypeVar, cast
|
||||
from typing import AsyncGenerator, Callable, Optional, TypeVar, cast
|
||||
|
||||
from aiohttp import ClientSession, FormData
|
||||
from aiohttp import ClientSession, ClientTimeout, FormData
|
||||
|
||||
from aiogram.api.methods import Request, TelegramMethod
|
||||
|
||||
|
|
@ -56,6 +56,16 @@ class AiohttpSession(BaseSession):
|
|||
self.raise_for_status(response)
|
||||
return cast(T, response.result)
|
||||
|
||||
async def stream_content(
|
||||
self, url: str, timeout: int, chunk_size: int
|
||||
) -> AsyncGenerator[bytes, None]:
|
||||
session = await self.create_session()
|
||||
client_timeout = ClientTimeout(total=timeout)
|
||||
|
||||
async with session.get(url, timeout=client_timeout) as resp:
|
||||
async for chunk in resp.content.iter_chunked(chunk_size):
|
||||
yield chunk
|
||||
|
||||
async def __aenter__(self) -> AiohttpSession:
|
||||
await self.create_session()
|
||||
return self
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||
import abc
|
||||
import datetime
|
||||
import json
|
||||
from typing import Any, Callable, Optional, TypeVar, Union
|
||||
from typing import Any, AsyncGenerator, Callable, Optional, TypeVar, Union
|
||||
|
||||
from aiogram.utils.exceptions import TelegramAPIError
|
||||
|
||||
|
|
@ -44,6 +44,12 @@ class BaseSession(abc.ABC):
|
|||
async def make_request(self, token: str, method: TelegramMethod[T]) -> T: # pragma: no cover
|
||||
pass
|
||||
|
||||
@abc.abstractmethod
|
||||
async def stream_content(
|
||||
self, url: str, timeout: int, chunk_size: int
|
||||
) -> AsyncGenerator[bytes, None]: # pragma: no cover
|
||||
yield b""
|
||||
|
||||
def prepare_value(self, value: Any) -> Union[str, int, bool]:
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue