Repository: jina-ai/jina
File: jina/clients/base/helper.py (Python)

Code before:

    async def send_message(self, request: 'Request'):
        """Sends a POST request to the server

        :param request: request as dict
        :return: send post message
        """
        req_dict = request.to_dict()
        req_dict['exec_endpoint'] = req_dict['header']['exec_endpoint']
        if 'target_executor' in req_dict['header']:
            req_dict['target_executor'] = req_dict['header']['target_executor']
        for attempt in range(1, self.max_attempts + 1):
            try:
                request_kwargs = {'url': self.url}
                if not docarray_v2:
                    request_kwargs['json'] = req_dict
                else:
                    from docarray.base_doc.io.json import orjson_dumps
                    request_kwargs['data'] = JinaJsonPayload(value=req_dict)
                response = await self.session.post(
                    **request_kwargs
                ).__aenter__()
                r_str = await response.json()
                handle_response_status(response.status, r_str, self.url)
                return response
            except (ValueError, ConnectionError, BadClient, aiohttp.ClientError) as err:
                await retry.wait_or_raise_err(
                    attempt=attempt,
                    err=err,
                    max_attempts=self.max_attempts,
                    backoff_multiplier=self.backoff_multiplier,
                    initial_backoff=self.initial_backoff,
                    max_backoff=self.max_backoff,
                )

    async def send_dry_run(self, **kwargs):
        """Query the dry_run endpoint from Gateway
        :param kwargs: keyword arguments to make sure compatible API with other clients
        :return: send get message
        """
        return await self.session.get(
            url=self.url, timeout=kwargs.get('timeout', None)
        ).__aenter__()

    async def recv_message(self):
        """Receive message for HTTP (sleep)

        :return: await sleep
        """
        return await asyncio.sleep(1e10)


Review comment (suggestion):
This should be `response.text()` since we are using aiohttp package.
https://docs.aiohttp.org/en/stable/client_quickstart.html#make-a-request

Code after:

    async def send_message(self, request: 'Request'):
        """Sends a POST request to the server

        :param request: request as dict
        :return: send post message
        """
        req_dict = request.to_dict()
        req_dict['exec_endpoint'] = req_dict['header']['exec_endpoint']
        if 'target_executor' in req_dict['header']:
            req_dict['target_executor'] = req_dict['header']['target_executor']
        for attempt in range(1, self.max_attempts + 1):
            try:
                request_kwargs = {'url': self.url}
                if not docarray_v2:
                    request_kwargs['json'] = req_dict
                else:
                    from docarray.base_doc.io.json import orjson_dumps
                    request_kwargs['data'] = JinaJsonPayload(value=req_dict)
                response = await self.session.post(
                    **request_kwargs
                ).__aenter__()
                try:
                    r_str = await response.json()
                except aiohttp.ContentTypeError:
                    r_str = response.text
                handle_response_status(response.status, r_str, self.url)
                return response
            except (ValueError, ConnectionError, BadClient, aiohttp.ClientError) as err:
                await retry.wait_or_raise_err(
                    attempt=attempt,
                    err=err,
                    max_attempts=self.max_attempts,
                    backoff_multiplier=self.backoff_multiplier,
                    initial_backoff=self.initial_backoff,
                    max_backoff=self.max_backoff,
                )

    async def send_dry_run(self, **kwargs):
        """Query the dry_run endpoint from Gateway
        :param kwargs: keyword arguments to make sure compatible API with other clients
        :return: send get message
        """
        return await self.session.get(
            url=self.url, timeout=kwargs.get('timeout', None)
        ).__aenter__()

    async def recv_message(self):
        """Receive message for HTTP (sleep)

        :return: await sleep