Generates an HTTP request and returns the response. Args: method (str): The HTTP method (e.g., 'GET', 'POST'). url (str): The URL for the request. payload_json (Optional[str]): A JSON string representing the request payload. Returns: dict: The response
(self, method: str, url: str, payload_json: Optional[str] = None)
| 103 | |
| 104 | |
| 105 | def http_request(self, method: str, url: str, payload_json: Optional[str] = None): |
| 106 | """ |
| 107 | Generates an HTTP request and returns the response. |
| 108 | |
| 109 | Args: |
| 110 | method (str): The HTTP method (e.g., 'GET', 'POST'). |
| 111 | url (str): The URL for the request. |
| 112 | payload_json (Optional[str]): A JSON string representing the request payload. |
| 113 | |
| 114 | Returns: |
| 115 | dict: The response from the HTTP request. |
| 116 | """ |
| 117 | try: |
| 118 | headers = {"Content-Type": "application/json"} |
| 119 | |
| 120 | # For GET requests, ignore the payload |
| 121 | if method.upper() == "GET": |
| 122 | print(f"[HTTP] launching GET request to {url}") |
| 123 | response = requests.get(url, headers=headers) |
| 124 | else: |
| 125 | # Validate and convert the payload for other types of requests |
| 126 | if payload_json: |
| 127 | payload = json_loads(payload_json) |
| 128 | else: |
| 129 | payload = {} |
| 130 | print(f"[HTTP] launching {method} request to {url}, payload=\n{json_dumps(payload, indent=2)}") |
| 131 | response = requests.request(method, url, json=payload, headers=headers) |
| 132 | |
| 133 | return {"status_code": response.status_code, "headers": dict(response.headers), "body": response.text} |
| 134 | except Exception as e: |
| 135 | return {"error": str(e)} |
nothing calls this directly
no test coverage detected