Skip to content

Commit

Permalink
Client.session method made async
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Oct 3, 2023
1 parent e0c1e17 commit e3035f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
21 changes: 10 additions & 11 deletions reportportal_client/aio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ def __init__(
stacklevel=2
)

@property
def session(self) -> aiohttp.ClientSession:
async def session(self) -> aiohttp.ClientSession:
"""Return aiohttp.ClientSession class instance, initialize it if necessary.
:return: aiohttp.ClientSession instance.
Expand Down Expand Up @@ -261,7 +260,7 @@ async def start_launch(self,
rerun_of=rerun_of
).payload

response = await AsyncHttpRequest(self.session.post, url=url, json=request_payload).make()
response = await AsyncHttpRequest((await self.session()).post, url=url, json=request_payload).make()
if not response:
return

Expand Down Expand Up @@ -327,7 +326,7 @@ async def start_test_item(self,
test_case_id=test_case_id
).payload

response = await AsyncHttpRequest(self.session.post, url=url, json=request_payload).make()
response = await AsyncHttpRequest((await self.session()).post, url=url, json=request_payload).make()
if not response:
return
item_id = await response.id
Expand Down Expand Up @@ -373,7 +372,7 @@ async def finish_test_item(self,
issue=issue,
retry=retry
).payload
response = await AsyncHttpRequest(self.session.put, url=url, json=request_payload).make()
response = await AsyncHttpRequest((await self.session()).put, url=url, json=request_payload).make()
if not response:
return
message = await response.message
Expand Down Expand Up @@ -404,7 +403,7 @@ async def finish_launch(self,
attributes=attributes,
description=kwargs.get('description')
).payload
response = await AsyncHttpRequest(self.session.put, url=url, json=request_payload,
response = await AsyncHttpRequest((await self.session()).put, url=url, json=request_payload,
name='Finish Launch').make()
if not response:
return
Expand All @@ -431,7 +430,7 @@ async def update_test_item(self,
}
item_id = await self.get_item_id_by_uuid(item_uuid)
url = root_uri_join(self.base_url_v1, 'item', item_id, 'update')
response = await AsyncHttpRequest(self.session.put, url=url, json=data).make()
response = await AsyncHttpRequest((await self.session()).put, url=url, json=data).make()
if not response:
return
logger.debug('update_test_item - Item: %s', item_id)
Expand All @@ -452,7 +451,7 @@ async def get_launch_info(self, launch_uuid_future: Union[str, Task[str]]) -> Op
:return: Launch information in dictionary.
"""
url = self.__get_launch_uuid_url(launch_uuid_future)
response = await AsyncHttpRequest(self.session.get, url=url).make()
response = await AsyncHttpRequest((await self.session()).get, url=url).make()
if not response:
return
if response.is_success:
Expand All @@ -477,7 +476,7 @@ async def get_item_id_by_uuid(self, item_uuid_future: Union[str, Task[str]]) ->
:return: Test Item ID.
"""
url = self.__get_item_uuid_url(item_uuid_future)
response = await AsyncHttpRequest(self.session.get, url=url).make()
response = await AsyncHttpRequest((await self.session()).get, url=url).make()
return response.id if response else None

async def get_launch_ui_id(self, launch_uuid_future: Union[str, Task[str]]) -> Optional[int]:
Expand Down Expand Up @@ -519,7 +518,7 @@ async def get_project_settings(self) -> Optional[Dict]:
:return: Settings response in Dictionary.
"""
url = root_uri_join(self.base_url_v1, 'settings')
response = await AsyncHttpRequest(self.session.get, url=url).make()
response = await AsyncHttpRequest((await self.session()).get, url=url).make()
return await response.json if response else None

async def log_batch(self, log_batch: Optional[List[AsyncRPRequestLog]]) -> Tuple[str, ...]:
Expand All @@ -530,7 +529,7 @@ async def log_batch(self, log_batch: Optional[List[AsyncRPRequestLog]]) -> Tuple
"""
url = root_uri_join(self.base_url_v2, 'log')
if log_batch:
response = await AsyncHttpRequest(self.session.post, url=url,
response = await AsyncHttpRequest((await self.session()).post, url=url,
data=AsyncRPLogBatch(log_batch).payload).make()
return await response.messages

Expand Down
10 changes: 6 additions & 4 deletions tests/aio/test_aio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ def test_client_pickling():
(NOT_SET, RetryingClientSession, DEFAULT_RETRY_NUMBER)
]
)
def test_retries_param(retry_num, expected_class, expected_param):
@pytest.mark.asyncio
async def test_retries_param(retry_num, expected_class, expected_param):
client = Client(ENDPOINT, PROJECT, api_key=API_KEY, retries=retry_num)
session = client.session
session = await client.session()
assert isinstance(session, expected_class)
if expected_param is not NOT_SET:
assert getattr(session, '_RetryingClientSession__retry_number') == expected_param
Expand All @@ -61,9 +62,10 @@ def test_retries_param(retry_num, expected_class, expected_param):
]
)
@mock.patch('reportportal_client.aio.client.RetryingClientSession')
def test_timeout_param(mocked_session, timeout_param, expected_connect_param, expected_sock_read_param):
@pytest.mark.asyncio
async def test_timeout_param(mocked_session, timeout_param, expected_connect_param, expected_sock_read_param):
client = Client(ENDPOINT, PROJECT, api_key=API_KEY, http_timeout=timeout_param)
session = client.session
session = await client.session()
assert session is not None
assert len(mocked_session.call_args_list) == 1
args, kwargs = mocked_session.call_args_list[0]
Expand Down

0 comments on commit e3035f3

Please sign in to comment.