mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Allow to send datetime/timedelta values
This commit is contained in:
parent
4189c3e798
commit
a2bc73e347
1 changed files with 16 additions and 4 deletions
|
|
@ -1,9 +1,12 @@
|
||||||
import abc
|
import abc
|
||||||
import asyncio
|
import asyncio
|
||||||
from typing import Generic, TypeVar
|
import datetime
|
||||||
|
import json
|
||||||
|
from typing import TypeVar, Union, Any, List, Dict
|
||||||
|
|
||||||
|
from pydantic.dataclasses import dataclass
|
||||||
|
|
||||||
from aiogram.api.methods import Response, TelegramMethod
|
from aiogram.api.methods import Response, TelegramMethod
|
||||||
from pydantic.dataclasses import dataclass
|
|
||||||
|
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
|
@ -27,8 +30,12 @@ PRODUCTION = TelegramAPIServer(
|
||||||
|
|
||||||
|
|
||||||
class BaseSession(abc.ABC):
|
class BaseSession(abc.ABC):
|
||||||
def __init__(self, api: TelegramAPIServer = PRODUCTION):
|
def __init__(
|
||||||
|
self, api: TelegramAPIServer = PRODUCTION, json_loads=json.loads, json_dumps=json.dumps
|
||||||
|
):
|
||||||
self.api = api
|
self.api = api
|
||||||
|
self.json_loads = json_loads
|
||||||
|
self.json_dumps = json_dumps
|
||||||
|
|
||||||
def raise_for_status(self, response: Response[T]):
|
def raise_for_status(self, response: Response[T]):
|
||||||
if response.ok:
|
if response.ok:
|
||||||
|
|
@ -58,7 +65,12 @@ class BaseSession(abc.ABC):
|
||||||
if isinstance(value, (bool, str, int)):
|
if isinstance(value, (bool, str, int)):
|
||||||
return value
|
return value
|
||||||
if isinstance(value, (list, dict)):
|
if isinstance(value, (list, dict)):
|
||||||
return json.dumps(self.clean_json(value))
|
return self.json_dumps(self.clean_json(value))
|
||||||
|
if isinstance(value, datetime.timedelta):
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
return int((now + value).timestamp())
|
||||||
|
if isinstance(value, datetime.datetime):
|
||||||
|
return round(value.timestamp())
|
||||||
else:
|
else:
|
||||||
return str(value)
|
return str(value)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue