Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding possibility to verify requests #2507

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading