[docs]defdump_httpx_request(request:httpx.Request)->str:# Convert the request headers to a stringheaders_str="\n".join(f"{name}: {value}"forname,valueinrequest.headers.items())# Access the request body; for non-streaming bodies, you can directly use request.content# For streaming bodies, you might need to handle them differently depending on your use casetry:body_str=request.content.decode("utf-8")ifrequest.contentelse""exceptRequestNotRead:body_str=""body_str=truncate_string(body_str,BODY_MAX_LENGTH_TO_DISPLAY)# Combine method, URL, headers, and body into a formatted stringrequest_dump=f"{request.method}{request.url.raw_path.decode()} HTTP/1.1\n{headers_str}\n\n{body_str}"returnrequest_dump.strip()
[docs]defdump_httpx_response(response:httpx.Response)->str:# Convert the response headers to a stringheaders_str="\n".join(f"{name}: {value}"forname,valueinresponse.headers.items())# Access the response body; for non-streaming bodies, you can directly use response.content# For streaming bodies, you might need to handle them differently depending on your use casetry:body_str=response.textexceptResponseNotRead:body_str=""body_str=truncate_string(body_str,BODY_MAX_LENGTH_TO_DISPLAY)# Combine status code, headers, and body into a formatted stringresponse_dump=(f"{response.http_version}{response.status_code}{response.reason_phrase}\n{headers_str}\n\n{body_str}")returnresponse_dump.strip()