From 8406f6737703f0f495d5fe4792f974558eb234ce Mon Sep 17 00:00:00 2001 From: Fayssal Defaa Date: Thu, 30 May 2024 13:58:48 +0200 Subject: [PATCH] adding possibility to verify requests --- utils/_weblog.py | 7 ++++++- utils/tools.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/utils/_weblog.py b/utils/_weblog.py index 4b9b696b9f..6b0eb08b28 100644 --- a/utils/_weblog.py +++ b/utils/_weblog.py @@ -15,7 +15,7 @@ import grpc import google.protobuf.struct_pb2 as pb -from utils.tools import logger +from utils.tools import logger, generate_curl_command import utils.grpc.weblog_pb2_grpc as grpcapi # monkey patching header validation in requests module, as we want to be able to send anything to weblog @@ -168,7 +168,9 @@ def request( req = requests.Request(method, url, params=params, data=data, headers=headers, cookies=cookies, **kwargs) r = req.prepare() r.url = url + curl_command = generate_curl_command(method, url, headers, params) logger.debug(f"Sending request {rid}: {method} {url}") + logger.info(f"Here is a curl command to try it out: {curl_command}") r = requests.Session().send(r, timeout=timeout, stream=stream, allow_redirects=allow_redirects) response_data["status_code"] = r.status_code @@ -177,8 +179,11 @@ def request( except Exception as e: logger.error(f"Request {rid} raise an error: {e}") + logger.info(f"Here is the culprit: {curl_command}") + else: logger.debug(f"Request {rid}: {r.status_code}") + logger.info(f"To try it out: {curl_command} {curl_command}") self.responses[self.current_nodeid].append(response_data) diff --git a/utils/tools.py b/utils/tools.py index ef40df3a89..5696920592 100644 --- a/utils/tools.py +++ b/utils/tools.py @@ -6,6 +6,7 @@ import os import re import sys +import json class bcolors: @@ -182,3 +183,20 @@ def nested_lookup(needle: str, heystack, look_in_keys=False, exact_match=False): return False raise TypeError(f"Can't handle type {type(heystack)}") + + +def generate_curl_command(method, url, headers, params): + curl_command = f"curl -X {method} '{url}'" + + for key, value in headers.items(): + curl_command += f" -H '{key}: {value}'" + + if method.upper() == "GET" and params: + query_string = "&".join([f"{key}={value}" for key, value in params.items()]) + curl_command += f"?{query_string}" + + if method.upper() in ["POST", "PUT", "PATCH"] and params: + json_data = json.dumps(params) + curl_command += f" -d '{json_data}'" + + return curl_command