From 026de90d01e1127b7944600818aa94dc53850518 Mon Sep 17 00:00:00 2001 From: jasquat Date: Tue, 24 Oct 2023 17:07:24 -0400 Subject: [PATCH] fixed post request v2 so it uses data instead of params w/ burnettk --- src/connector_http/commands/get_request_v2.py | 5 ++++- src/connector_http/commands/post_request_v2.py | 11 +++++++++++ src/connector_http/http_request_base.py | 17 ++++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/connector_http/commands/get_request_v2.py b/src/connector_http/commands/get_request_v2.py index 1b13b04..12fe92b 100644 --- a/src/connector_http/commands/get_request_v2.py +++ b/src/connector_http/commands/get_request_v2.py @@ -16,7 +16,10 @@ def __init__(self, basic_auth_password: str | None = None, attempts: int | None = None, ): - HttpRequestBase.__init__(self, url=url, headers=headers, params=params, basic_auth_username=basic_auth_username, basic_auth_password=basic_auth_password) + HttpRequestBase.__init__(self, url=url, headers=headers, basic_auth_username=basic_auth_username, basic_auth_password=basic_auth_password) + + self.params = params or {} + if not isinstance(attempts, int) or attempts < 1 or attempts > 10: attempts = 1 self.attempts = attempts diff --git a/src/connector_http/commands/post_request_v2.py b/src/connector_http/commands/post_request_v2.py index 1cbb40e..ad4f244 100644 --- a/src/connector_http/commands/post_request_v2.py +++ b/src/connector_http/commands/post_request_v2.py @@ -8,5 +8,16 @@ class PostRequestV2(ConnectorCommand, HttpRequestBase): + def __init__(self, + url: str, + headers: dict[str, str] | None = None, + data: dict[str, str] | None = None, + basic_auth_username: str | None = None, + basic_auth_password: str | None = None, + ): + HttpRequestBase.__init__(self, url=url, headers=headers, basic_auth_username=basic_auth_username, basic_auth_password=basic_auth_password) + + self.data =data or {} + def execute(self, _config: Any, _task_data: dict) -> ConnectorProxyResponseDict: return self.run_request(requests.post) diff --git a/src/connector_http/http_request_base.py b/src/connector_http/http_request_base.py index cc2d74d..9d0a497 100644 --- a/src/connector_http/http_request_base.py +++ b/src/connector_http/http_request_base.py @@ -12,17 +12,18 @@ class HttpRequestBase: def __init__(self, url: str, headers: dict[str, str] | None = None, - params: dict[str, str] | None = None, basic_auth_username: str | None = None, basic_auth_password: str | None = None, ): self.url = url self.headers = headers or {} - self.params = params or {} self.basic_auth_username = basic_auth_username self.basic_auth_password = basic_auth_password self.attempts = 1 + self.params: dict | None = None + self.data: dict | None = None + def _create_error_from_exception(self, exception: Exception, http_response: requests.Response | None) -> CommandErrorDict: return self._create_error( error_code=exception.__class__.__name__, http_response=http_response, additional_message=str(exception) @@ -68,7 +69,17 @@ def log(msg: str) -> None: try: log(f"Will call {self.url}") - http_response = request_function(self.url, self.params, headers=self.headers, auth=auth, timeout=300) + arguments = { + "url": self.url, + "headers": self.headers, + "auth": auth, + "timeout": 300, + } + if self.params is not None: + arguments["params"] = self.params + if self.data is not None: + arguments["json"] = self.data + http_response = request_function(**arguments) log(f"Did call {self.url}") log("Will parse http_response")