Skip to content

Commit

Permalink
Fix response data forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
bwoebi committed Jun 7, 2024
1 parent a697311 commit a6448f4
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions ddapm_test_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ async def handle_exception_middleware(request: Request, handler: _Handler) -> we
return web.HTTPBadRequest(reason=str(e))


async def _forward_request(request_data: bytes, headers: Mapping[str, str], full_agent_url: str) -> ClientResponse:
async def _forward_request(request_data: bytes, headers: Mapping[str, str], full_agent_url: str) -> tuple[
ClientResponse, str]:
async with ClientSession() as session:
async with session.post(
full_agent_url,
Expand All @@ -134,7 +135,10 @@ async def _forward_request(request_data: bytes, headers: Mapping[str, str], full
) as resp:
assert resp.status == 200, f"Request to agent unsuccessful, received [{resp.status}] response."

if "text/html" in resp.content_type:
if "text/plain" in resp.content_type:
raw_response_data = await resp.text()
log.info("Response %r from agent:", raw_response_data)
else:
raw_response_data = await resp.read()
if len(raw_response_data) == 0:
log.info("Received empty response: %r from agent.", raw_response_data)
Expand All @@ -148,11 +152,7 @@ async def _forward_request(request_data: bytes, headers: Mapping[str, str], full
log.warning("Original Request: %r", request_data)
response_data = ""
log.info("Response %r from agent:", response_data)
elif "text/plain" in resp.content_type:
log.info("Response %r from agent:", await resp.text())
else:
log.info("Response %r from agent:", await resp.json())
return resp
return resp, raw_response_data


async def _prepare_and_send_request(data: bytes, request: Request, headers: Mapping[str, str]) -> web.Response:
Expand All @@ -165,11 +165,11 @@ async def _prepare_and_send_request(data: bytes, request: Request, headers: Mapp
log.info("Forwarding request to agent at %r", full_agent_url)
log.debug(f"Using headers: {headers}")

client_response = await _forward_request(data, headers, full_agent_url)
(client_response, body) = await _forward_request(data, headers, full_agent_url)
return web.Response(
status=client_response.status,
headers=client_response.headers,
body=await client_response.read(),
body=body,
)


Expand Down

0 comments on commit a6448f4

Please sign in to comment.