Skip to content

Commit 9ceb91e

Browse files
committed
Use timedelta for elapsed-time handling to improve Requests compatibility
1 parent cdcb284 commit 9ceb91e

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

curl_cffi/requests/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from concurrent.futures import Future
66
from typing import Any, Optional, Union
77
from collections.abc import Awaitable, Callable
8+
from datetime import timedelta
89

910
from ..curl import Curl
1011
from ..utils import CurlCffiWarning
@@ -54,7 +55,7 @@ class Response:
5455
ok: is status_code in [200, 400)?
5556
headers: response headers.
5657
cookies: response cookies.
57-
elapsed: how many seconds the request cost.
58+
elapsed: timedelta of the request duration.
5859
encoding: http body encoding.
5960
charset: alias for encoding.
6061
primary_ip: primary ip of the server.
@@ -86,7 +87,7 @@ def __init__(self, curl: Optional[Curl] = None, request: Optional[Request] = Non
8687
self.ok = True
8788
self.headers = Headers()
8889
self.cookies = Cookies()
89-
self.elapsed = 0.0
90+
self.elapsed: timedelta = timedelta()
9091
self.default_encoding: Union[str, Callable[[bytes], str]] = "utf-8"
9192
self.redirect_count = 0
9293
self.redirect_url = ""

curl_cffi/requests/session.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
cast,
2323
)
2424
from urllib.parse import urlparse
25+
from datetime import timedelta
2526

2627
from ..aio import AsyncCurl
2728
from ..const import CurlHttpVersion, CurlInfo, CurlOpt
@@ -304,7 +305,7 @@ def _parse_response(
304305
rsp.local_ip = cast(bytes, c.getinfo(CurlInfo.LOCAL_IP)).decode()
305306
rsp.local_port = cast(int, c.getinfo(CurlInfo.LOCAL_PORT))
306307
rsp.default_encoding = default_encoding
307-
rsp.elapsed = cast(float, c.getinfo(CurlInfo.TOTAL_TIME))
308+
rsp.elapsed = timedelta(seconds=cast(float, c.getinfo(CurlInfo.TOTAL_TIME)))
308309
rsp.redirect_count = cast(int, c.getinfo(CurlInfo.REDIRECT_COUNT))
309310
redirect_url_bytes = cast(bytes, c.getinfo(CurlInfo.REDIRECT_URL))
310311
try:

0 commit comments

Comments
 (0)