Skip to content

Commit

Permalink
adding possibility to verify requests
Browse files Browse the repository at this point in the history
  • Loading branch information
faydef committed May 30, 2024
1 parent 86e0e8f commit 8406f67
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion utils/_weblog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import re
import sys
import json


class bcolors:
Expand Down Expand Up @@ -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

0 comments on commit 8406f67

Please sign in to comment.