From e12a23e676d15411443e3ee2f6ea5485cf968fe2 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Tue, 16 Jan 2024 16:38:59 +0100 Subject: HttpCommand --- automato/command/__init__.py | 2 +- automato/command/http.py | 16 ++++++++++++++++ automato/transport/http.py | 16 ++++++++-------- 3 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 automato/command/http.py diff --git a/automato/command/__init__.py b/automato/command/__init__.py index ea8edca..8b9e8c4 100644 --- a/automato/command/__init__.py +++ b/automato/command/__init__.py @@ -13,7 +13,7 @@ MUST implement: execute(self, **kwargs) CAN implement: - _init(self, ...) + _init(self, transport, ...) SHOULDNT implement: __init__(self, endpoint_info: dict, **kwargs): diff --git a/automato/command/http.py b/automato/command/http.py new file mode 100644 index 0000000..efe3578 --- /dev/null +++ b/automato/command/http.py @@ -0,0 +1,16 @@ +import logging +from automato.transport.http import HttpTransport +from automato.command import Command + +logger = logging.getLogger(__name__) + +class HttpCommand(Command): + def _init(self, transport: HttpTransport): + self._transport = transport + + def execute(self, **kwargs): + req = self._transport.request(**kwargs) + if not req.ok: + logger.error(f'HttpCommand request failed with code {req.status_code}') + else: + logger.debug(f'HttpCommand exeuted with result {req.ok} Code {req.status_code}') diff --git a/automato/transport/http.py b/automato/transport/http.py index ca27e82..22570dd 100644 --- a/automato/transport/http.py +++ b/automato/transport/http.py @@ -35,13 +35,13 @@ class HttpTransport(Transport): # TODO Here we could maybe also perform a more complex login # with Cookies? - ret = self._request('HEAD', self._validation_path) + ret = self.request('HEAD', self._validation_path) logger.debug(f'{self._validation_path} checked {ret.ok} with code {ret.status_code}') return ret.ok - def _request(self, method:str, path:str, headers:[None,dict] = None, + def request(self, method:str, path:str, headers:[None,dict] = None, data = None, params:[None,str] = None): full_path = self._address.rstrip('/') + '/' + path.lstrip('/') logger.debug(f'requested {method} for {full_path}') @@ -66,14 +66,14 @@ class HttpTransport(Transport): def get(self, **kwargs): - return self._request('GET', **kwargs) + return self.request('GET', **kwargs) def head(self, **kwargs): - return self._request('HEAD', **kwargs) + return self.request('HEAD', **kwargs) def put(self, **kwargs): - return self._request('PUT', **kwargs) + return self.request('PUT', **kwargs) def post(self, **kwargs): - return self._request('POST', **kwargs) + return self.request('POST', **kwargs) def patch(self, **kwargs): - return self._request('PATCH', **kwargs) + return self.request('PATCH', **kwargs) def delete(self, **kwargs): - return self._request('DELETE', **kwargs) + return self.request('DELETE', **kwargs) -- cgit v1.2.3