Skip to content

Commit

Permalink
Async RPClient: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Sep 7, 2023
1 parent a611524 commit a528086
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 89 deletions.
25 changes: 14 additions & 11 deletions reportportal_client/aio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ async def start_launch(self,
if not response:
return

launch_uuid = response.id
launch_uuid = await response.id
logger.debug(f'start_launch - ID: %s', launch_uuid)
if self.launch_uuid_print and self.print_output:
print(f'Report Portal Launch UUID: {launch_uuid}', file=self.print_output)
Expand Down Expand Up @@ -263,10 +263,11 @@ async def start_test_item(self,
response = await AsyncHttpRequest(self.session.post, url=url, json=request_payload).make()
if not response:
return
item_id = response.id
item_id = await response.id
if item_id is NOT_FOUND:
logger.warning('start_test_item - invalid response: %s',
str(response.json))
logger.warning('start_test_item - invalid response: %s', str(await response.json))
else:
logger.debug('start_test_item - ID: %s', item_id)
return item_id

async def finish_test_item(self,
Expand Down Expand Up @@ -294,9 +295,10 @@ async def finish_test_item(self,
response = await AsyncHttpRequest(self.session.put, url=url, json=request_payload).make()
if not response:
return
logger.debug('finish_test_item - ID: %s', item_id)
logger.debug('response message: %s', response.message)
return response.message
message = await response.message
logger.debug('finish_test_item - ID: %s', await await_if_necessary(item_id))
logger.debug('response message: %s', message)
return message

async def finish_launch(self,
launch_uuid: Union[str, asyncio.Task],
Expand All @@ -316,9 +318,10 @@ async def finish_launch(self,
name='Finish Launch').make()
if not response:
return
logger.debug('finish_launch - ID: %s', launch_uuid)
logger.debug('response message: %s', response.message)
return response.message
message = await response.message
logger.debug('finish_launch - ID: %s', await await_if_necessary(launch_uuid))
logger.debug('response message: %s', message)
return message

async def update_test_item(self,
item_uuid: Union[str, asyncio.Task],
Expand All @@ -335,7 +338,7 @@ async def update_test_item(self,
if not response:
return
logger.debug('update_test_item - Item: %s', item_id)
return response.message
return await response.message

async def __get_item_uuid_url(self, item_uuid_future: Union[str, asyncio.Task]) -> Optional[str]:
item_uuid = await await_if_necessary(item_uuid_future)
Expand Down
6 changes: 3 additions & 3 deletions reportportal_client/core/rp_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from reportportal_client import helpers
from reportportal_client.core.rp_file import RPFile
from reportportal_client.core.rp_issues import Issue
from reportportal_client.core.rp_responses import RPResponse
from reportportal_client.core.rp_responses import RPResponse, AsyncRPResponse
from reportportal_client.helpers import dict_to_payload, await_if_necessary
from reportportal_client.static.abstract import (
AbstractBaseClass,
Expand Down Expand Up @@ -139,14 +139,14 @@ def __init__(self,
"""
super().__init__(session_method=session_method, url=url, data=data, json=json, name=name)

async def make(self) -> Optional[RPResponse]:
async def make(self) -> Optional[AsyncRPResponse]:
"""Make HTTP request to the Report Portal API."""
url = await await_if_necessary(self.url)
if not url:
return

try:
return RPResponse(await self.session_method(url, data=self.data, json=self.json))
return AsyncRPResponse(await self.session_method(url, data=self.data, json=self.json))
except (KeyError, IOError, ValueError, TypeError) as exc:
logger.warning(
"Report Portal %s request failed",
Expand Down
82 changes: 45 additions & 37 deletions reportportal_client/core/rp_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,75 +19,83 @@
# limitations under the License

import logging
from functools import lru_cache
from typing import Any, Optional

from aiohttp import ClientResponse
from requests import Response

from reportportal_client.static.defines import NOT_FOUND

logger = logging.getLogger(__name__)


class RPMessage:
"""Model for the message returned by RP API."""

__slots__ = ['message', 'error_code']
class RPResponse:
"""Class representing RP API response."""
_resp: Response
__json: Any

def __init__(self, data):
def __init__(self, data: Response) -> None:
"""Initialize instance attributes.
:param data: Dictionary representation of the API response
:param data: requests.Response object
"""
self.error_code = data.get('error_code', NOT_FOUND)
self.message = data.get('message', NOT_FOUND)
self._resp = data
self.__json = None

def __str__(self):
"""Change string representation of the class."""
if self.error_code is NOT_FOUND:
return self.message
return '{error_code}: {message}'.format(error_code=self.error_code,
message=self.message)
@property
def id(self) -> Optional[str]:
"""Get value of the 'id' key."""
return self.json.get('id', NOT_FOUND)

@property
def is_empty(self):
"""Check if returned message is empty."""
return self.message is NOT_FOUND
def is_success(self) -> bool:
"""Check if response to API has been successful."""
return self._resp.ok

@property
def json(self) -> Any:
"""Get the response in dictionary."""
if not self.__json:
self.__json = self._resp.json()
return self.__json

class RPResponse:
@property
def message(self) -> Optional[str]:
"""Get value of the 'message' key."""
return self.json.get('message')


class AsyncRPResponse:
"""Class representing RP API response."""
_resp: Response
__json: Any

def __init__(self, data):
def __init__(self, data: Response) -> None:
"""Initialize instance attributes.
:param data: requests.Response object
"""
self._resp = data

@staticmethod
def _get_json(data):
"""Get response in dictionary.
:param data: requests.Response object
:return: dict
"""
return data.json()
self.__json = None

@property
def id(self):
async def id(self) -> Optional[str]:
"""Get value of the 'id' key."""
return self.json.get('id', NOT_FOUND)
return (await self.json).get('id', NOT_FOUND)

@property
def is_success(self):
def is_success(self) -> bool:
"""Check if response to API has been successful."""
return self._resp.ok

@property
@lru_cache()
def json(self):
async def json(self) -> Any:
"""Get the response in dictionary."""
return self._get_json(self._resp)
if not self.__json:
self.__json = await self._resp.json()
return self.__json

@property
def message(self):
async def message(self) -> Optional[str]:
"""Get value of the 'message' key."""
return self.json.get('message', NOT_FOUND)
return (await self.json).get('message')
38 changes: 0 additions & 38 deletions reportportal_client/core/rp_responses.pyi

This file was deleted.

0 comments on commit a528086

Please sign in to comment.