Skip to content

Commit

Permalink
fix rest api aiohttp timeout (#337)
Browse files Browse the repository at this point in the history
* fix rest api aiohttp timeout

* Added test cases for rest api timeout parameter

* handled scenario when request_timeout is ClientTimeout object
  • Loading branch information
soamicharan authored Oct 28, 2024
1 parent 089f487 commit 6897114
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
16 changes: 14 additions & 2 deletions kubernetes_asyncio/client/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ async def request(self, method, url, query_params=None, headers=None,
:param _request_timeout: timeout setting for this request. If one
number provided, it will be total request
timeout. It can also be a pair (tuple) of
(connection, read) timeouts.
(connection, read) timeouts or object
of aiohttp.ClientTimeout.
"""
method = method.upper()
assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT',
Expand All @@ -117,7 +118,18 @@ async def request(self, method, url, query_params=None, headers=None,

post_params = post_params or {}
headers = headers or {}
timeout = _request_timeout or 5 * 60
timeout = aiohttp.ClientTimeout()
if _request_timeout:
if isinstance(_request_timeout, (int, float)):
timeout = aiohttp.ClientTimeout(total=_request_timeout)
elif isinstance(_request_timeout, tuple) and len(_request_timeout) == 2:
timeout = aiohttp.ClientTimeout(
connect=_request_timeout[0],
sock_connect=_request_timeout[0],
sock_read=_request_timeout[1],
)
elif isinstance(_request_timeout, aiohttp.ClientTimeout):
timeout = _request_timeout

if 'Content-Type' not in headers:
headers['Content-Type'] = 'application/json'
Expand Down
32 changes: 32 additions & 0 deletions kubernetes_asyncio/client/test_rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import asyncio
import unittest
from unittest.mock import AsyncMock
import aiohttp
from kubernetes_asyncio.client.rest import RESTClientObject
from kubernetes_asyncio.client.configuration import Configuration
class TestRESTClientObject(unittest.IsolatedAsyncioTestCase):

@classmethod
def setUpClass(cls):
cls.config = Configuration()


async def test_rest_request_timeout(self):
rest_api = RESTClientObject(configuration=self.config)
for request_timeout, expected_timeout_arg in [
(None, aiohttp.ClientTimeout()),
(5.0, aiohttp.ClientTimeout(total=5.0)),
(3, aiohttp.ClientTimeout(total=3)),
((5, 7), aiohttp.ClientTimeout(connect=5, sock_connect=5, sock_read=7)),
(aiohttp.ClientTimeout(total=None), aiohttp.ClientTimeout(total=None)),
]:
with self.subTest(request_timeout=request_timeout, expected_timeout_arg=expected_timeout_arg):
mock_request = AsyncMock()
rest_api.pool_manager.request = mock_request
await rest_api.request(method="GET", url="http://test-api", _preload_content=False, _request_timeout=request_timeout)
mock_request.assert_called_once_with(
method="GET",
url="http://test-api",
timeout=expected_timeout_arg,
headers={"Content-Type": "application/json"}
)

0 comments on commit 6897114

Please sign in to comment.