Skip to content

Commit

Permalink
added head, put, and delete commands w/ burnettk
Browse files Browse the repository at this point in the history
  • Loading branch information
jasquat committed Feb 14, 2024
1 parent 5e6a675 commit 8e4b36f
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/connector_http/commands/delete_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import Any

import requests # type: ignore
from spiffworkflow_connector_command.command_interface import ConnectorCommand
from spiffworkflow_connector_command.command_interface import ConnectorProxyResponseDict

from connector_http.http_request_base import HttpRequestBase


class DeleteRequest(ConnectorCommand, HttpRequestBase):
def __init__(
self,
url: str,
headers: dict[str, str] | None = None,
params: 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.params = params
self.data = data

def execute(self, _config: Any, _task_data: dict) -> ConnectorProxyResponseDict:
return self.run_request(requests.delete)
31 changes: 31 additions & 0 deletions src/connector_http/commands/head_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Any

import requests # type: ignore
from spiffworkflow_connector_command.command_interface import ConnectorCommand
from spiffworkflow_connector_command.command_interface import ConnectorProxyResponseDict

from connector_http.http_request_base import HttpRequestBase


class HeadRequest(ConnectorCommand, 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,
attempts: int | None = None,
):
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

def execute(self, _config: Any, _task_data: dict) -> ConnectorProxyResponseDict:
return self.run_request(requests.head)
9 changes: 6 additions & 3 deletions src/connector_http/commands/post_request_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@


class PostRequestV2(ConnectorCommand, HttpRequestBase):
def __init__(self,
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)
HttpRequestBase.__init__(
self, url=url, headers=headers, basic_auth_username=basic_auth_username, basic_auth_password=basic_auth_password
)

self.data =data or {}
self.data = data or {}

def execute(self, _config: Any, _task_data: dict) -> ConnectorProxyResponseDict:
return self.run_request(requests.post)
26 changes: 26 additions & 0 deletions src/connector_http/commands/put_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Any

import requests # type: ignore
from spiffworkflow_connector_command.command_interface import ConnectorCommand
from spiffworkflow_connector_command.command_interface import ConnectorProxyResponseDict

from connector_http.http_request_base import HttpRequestBase


class PutRequest(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.put)
61 changes: 61 additions & 0 deletions tests/connector_http/unit/test_delete_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import json
from typing import Any
from unittest.mock import patch

from connector_http.commands.delete_request import DeleteRequest


class TestDeleteRequest:
def test_delete_html_from_url(self) -> None:
request = DeleteRequest(url="http://example.com")
return_html = "<html>Hey</html>"
with patch("requests.delete") as mock_request:
mock_request.return_value.status_code = 200
mock_request.return_value.ok = True
mock_request.return_value.text = return_html
response = request.execute(None, {})
assert mock_request.call_count == 1

assert response["command_response"]["body"] == json.dumps({"raw_response": return_html})
assert response["command_response"]["http_status"] == 200
assert response["command_response"]["mimetype"] == "application/json"
assert response["error"] is None
assert response["spiff__logs"] is not None
assert len(response["spiff__logs"]) > 0

def test_delete_json_from_url(self) -> None:
request = DeleteRequest(url="http://example.com")
return_json = {"hey": "we_return"}
with patch("requests.delete") as mock_request:
mock_request.return_value.status_code = 200
mock_request.return_value.ok = True
mock_request.return_value.headers = {"Content-Type": "application/json"}
mock_request.return_value.text = json.dumps(return_json)
response = request.execute(None, {})
assert mock_request.call_count == 1

assert response is not None
assert response["command_response"]["body"] == json.dumps(return_json)
assert response["command_response"]["http_status"] == 200
assert response["command_response"]["mimetype"] == "application/json"
assert response["error"] is None
assert response["spiff__logs"] is not None
assert len(response["spiff__logs"]) > 0

def test_delete_can_handle_500(self, sleepless: Any) -> None:
request = DeleteRequest(url="http://example.com")
return_json = {"error": "we_did_error"}
with patch("requests.delete") as mock_request:
mock_request.return_value.status_code = 500
mock_request.return_value.headers = {"Content-Type": "application/json"}
mock_request.return_value.text = json.dumps(return_json)
response = request.execute(None, {})
assert mock_request.call_count == 1

assert response is not None
assert response["command_response"]["body"] == json.dumps(return_json)
assert response["command_response"]["http_status"] == 500
assert response["command_response"]["mimetype"] == "application/json"
assert response["error"] is not None
assert response["spiff__logs"] is not None
assert len(response["spiff__logs"]) > 0
60 changes: 60 additions & 0 deletions tests/connector_http/unit/test_head_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import json
from typing import Any
from unittest.mock import patch

from connector_http.commands.head_request import HeadRequest


class TestHeadRequest:
def test_head_html_from_url(self) -> None:
request = HeadRequest(url="http://example.com")
return_html = "<html>Hey</html>"
with patch("requests.head") as mock_request:
mock_request.return_value.status_code = 200
mock_request.return_value.ok = True
mock_request.return_value.text = return_html
response = request.execute(None, {})

assert response is not None
assert response["command_response"]["body"] == json.dumps({"raw_response": return_html})
assert response["command_response"]["http_status"] == 200
assert response["command_response"]["mimetype"] == "application/json"
assert response["error"] is None
assert response["spiff__logs"] is not None
assert len(response["spiff__logs"]) > 0

def test_head_json_from_url(self) -> None:
request = HeadRequest(url="http://example.com")
return_json = {"hey": "we_return"}
with patch("requests.head") as mock_request:
mock_request.return_value.status_code = 200
mock_request.return_value.ok = True
mock_request.return_value.headers = {"Content-Type": "application/json"}
mock_request.return_value.text = json.dumps(return_json)
response = request.execute(None, {})

assert response is not None
assert response["command_response"]["body"] == json.dumps(return_json)
assert response["command_response"]["http_status"] == 200
assert response["command_response"]["mimetype"] == "application/json"
assert response["error"] is None
assert response["spiff__logs"] is not None
assert len(response["spiff__logs"]) > 0

def test_head_can_handle_500(self, sleepless: Any) -> None:
request = HeadRequest(url="http://example.com", attempts=3)
return_json = {"error": "we_did_error"}
with patch("requests.head") as mock_request:
mock_request.return_value.status_code = 500
mock_request.return_value.headers = {"Content-Type": "application/json"}
mock_request.return_value.text = json.dumps(return_json)
response = request.execute(None, {})
assert mock_request.call_count == 3

assert response is not None
assert response["command_response"]["body"] == json.dumps(return_json)
assert response["command_response"]["http_status"] == 500
assert response["command_response"]["mimetype"] == "application/json"
assert response["error"] is not None
assert response["spiff__logs"] is not None
assert len(response["spiff__logs"]) > 0
61 changes: 61 additions & 0 deletions tests/connector_http/unit/test_put_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import json
from typing import Any
from unittest.mock import patch

from connector_http.commands.put_request import PutRequest


class TestPutRequest:
def test_put_html_from_url(self) -> None:
request = PutRequest(url="http://example.com")
return_html = "<html>Hey</html>"
with patch("requests.put") as mock_request:
mock_request.return_value.status_code = 200
mock_request.return_value.ok = True
mock_request.return_value.text = return_html
response = request.execute(None, {})
assert mock_request.call_count == 1

assert response["command_response"]["body"] == json.dumps({"raw_response": return_html})
assert response["command_response"]["http_status"] == 200
assert response["command_response"]["mimetype"] == "application/json"
assert response["error"] is None
assert response["spiff__logs"] is not None
assert len(response["spiff__logs"]) > 0

def test_put_json_from_url(self) -> None:
request = PutRequest(url="http://example.com")
return_json = {"hey": "we_return"}
with patch("requests.put") as mock_request:
mock_request.return_value.status_code = 200
mock_request.return_value.ok = True
mock_request.return_value.headers = {"Content-Type": "application/json"}
mock_request.return_value.text = json.dumps(return_json)
response = request.execute(None, {})
assert mock_request.call_count == 1

assert response is not None
assert response["command_response"]["body"] == json.dumps(return_json)
assert response["command_response"]["http_status"] == 200
assert response["command_response"]["mimetype"] == "application/json"
assert response["error"] is None
assert response["spiff__logs"] is not None
assert len(response["spiff__logs"]) > 0

def test_put_can_handle_500(self, sleepless: Any) -> None:
request = PutRequest(url="http://example.com")
return_json = {"error": "we_did_error"}
with patch("requests.put") as mock_request:
mock_request.return_value.status_code = 500
mock_request.return_value.headers = {"Content-Type": "application/json"}
mock_request.return_value.text = json.dumps(return_json)
response = request.execute(None, {})
assert mock_request.call_count == 1

assert response is not None
assert response["command_response"]["body"] == json.dumps(return_json)
assert response["command_response"]["http_status"] == 500
assert response["command_response"]["mimetype"] == "application/json"
assert response["error"] is not None
assert response["spiff__logs"] is not None
assert len(response["spiff__logs"]) > 0

0 comments on commit 8e4b36f

Please sign in to comment.