-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added head, put, and delete commands w/ burnettk
- Loading branch information
Showing
7 changed files
with
273 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |