From ac9b82236e252a5707579b96adc5170443a84e23 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 28 Feb 2024 01:43:32 +0100 Subject: [PATCH] Renamed `http.request` plugin as `http`. --- .../source/platypush/plugins/http.request.rst | 7 - docs/source/platypush/plugins/http.rst | 5 + docs/source/plugins.rst | 2 +- platypush/plugins/http/__init__.py | 184 ++++++++++++++++++ .../plugins/http/{request => }/manifest.yaml | 2 +- platypush/plugins/http/request/__init__.py | 184 ------------------ 6 files changed, 191 insertions(+), 193 deletions(-) delete mode 100644 docs/source/platypush/plugins/http.request.rst create mode 100644 docs/source/platypush/plugins/http.rst rename platypush/plugins/http/{request => }/manifest.yaml (59%) delete mode 100644 platypush/plugins/http/request/__init__.py diff --git a/docs/source/platypush/plugins/http.request.rst b/docs/source/platypush/plugins/http.request.rst deleted file mode 100644 index c84a3a768..000000000 --- a/docs/source/platypush/plugins/http.request.rst +++ /dev/null @@ -1,7 +0,0 @@ -``http.request`` -================================== - -.. automodule:: platypush.plugins.http.request - :members: - - diff --git a/docs/source/platypush/plugins/http.rst b/docs/source/platypush/plugins/http.rst new file mode 100644 index 000000000..1a2d3a7c9 --- /dev/null +++ b/docs/source/platypush/plugins/http.rst @@ -0,0 +1,5 @@ +``http`` +======== + +.. automodule:: platypush.plugins.http + :members: diff --git a/docs/source/plugins.rst b/docs/source/plugins.rst index 4cde2fb97..99e99c1ba 100644 --- a/docs/source/plugins.rst +++ b/docs/source/plugins.rst @@ -50,7 +50,7 @@ Plugins platypush/plugins/gps.rst platypush/plugins/graphite.rst platypush/plugins/hid.rst - platypush/plugins/http.request.rst + platypush/plugins/http.rst platypush/plugins/http.webpage.rst platypush/plugins/ifttt.rst platypush/plugins/inspect.rst diff --git a/platypush/plugins/http/__init__.py b/platypush/plugins/http/__init__.py index e69de29bb..6b26907b9 100644 --- a/platypush/plugins/http/__init__.py +++ b/platypush/plugins/http/__init__.py @@ -0,0 +1,184 @@ +import logging +import os +import requests + +from platypush.message import Message +from platypush.plugins import Plugin, action + +logger = logging.getLogger(__name__) + + +class HttpRequestPlugin(Plugin): + """ + Plugin for executing custom HTTP requests. + + Some example usages:: + + # Execute a GET request on a JSON endpoint + { + "type": "request", + "action": "http.get", + "args": { + "url": "http://remote-host/api/v1/entity", + "params": { + "start": "2000-01-01" + } + } + } + + # Execute an action on another Platypush host through HTTP interface + { + "type": "request", + "action": "http.post", + "args": { + "url": "http://remote-host:8008/execute", + "json": { + "type": "request", + "target": "remote-host", + "action": "music.mpd.play" + } + } + } + """ + + def __init__(self, **kwargs): + super().__init__(**kwargs) + + @staticmethod + def _exec(method, url, output='text', **kwargs): + """Available output types: text (default), json, binary""" + + if 'username' in kwargs and 'password' in kwargs: + kwargs['auth'] = (kwargs.pop('username'), kwargs.pop('password')) + + method = getattr(requests, method) + response = method(url, **kwargs) + response.raise_for_status() + + if output == 'json': + output = response.json() + if output == 'binary': + output = response.content + else: + output = response.text + + try: + # If the response is a Platypush JSON, extract it + output = Message.build(output) + except Exception as e: + logger.debug(e) + + return output + + @action + def get(self, url, **kwargs): + """ + Perform a GET request + + :param url: Target URL + :type url: str + + :param kwargs: Additional arguments that will be transparently provided to the ``requests`` object, including + but not limited to query params, data, JSON, headers etc. + (see https://docs.python-requests.org/en/master/user/quickstart/#make-a-request) + :type kwargs: dict + """ + + return self._exec(method='get', url=url, **kwargs) + + @action + def post(self, url, **kwargs): + """ + Perform a POST request + + :param url: Target URL + :type url: str + + :param kwargs: Additional arguments that will be transparently provided to the ``requests`` object, including + but not limited to query params, data, JSON, headers etc. + (see https://docs.python-requests.org/en/master/user/quickstart/#make-a-request) + :type kwargs: dict + """ + + return self._exec(method='post', url=url, **kwargs) + + @action + def head(self, url, **kwargs): + """ + Perform an HTTP HEAD request + + :param url: Target URL + :type url: str + + :param kwargs: Additional arguments that will be transparently provided to the ``requests`` object, including + but not limited to query params, data, JSON, headers etc. + (see https://docs.python-requests.org/en/master/user/quickstart/#make-a-request) + :type kwargs: dict + """ + + return self._exec(method='head', url=url, **kwargs) + + @action + def put(self, url, **kwargs): + """ + Perform a PUT request + + :param url: Target URL + :type url: str + + :param kwargs: Additional arguments that will be transparently provided to the ``requests`` object, including + but not limited to query params, data, JSON, headers etc. + (see https://docs.python-requests.org/en/master/user/quickstart/#make-a-request) + :type kwargs: dict + """ + + return self._exec(method='put', url=url, **kwargs) + + @action + def delete(self, url, **kwargs): + """ + Perform a DELETE request + + :param url: Target URL + :type url: str + + :param kwargs: Additional arguments that will be transparently provided to the ``requests`` object, including + but not limited to query params, data, JSON, headers etc. + (see https://docs.python-requests.org/en/master/user/quickstart/#make-a-request) + :type kwargs: dict + """ + + return self._exec(method='delete', url=url, **kwargs) + + @action + def options(self, url, **kwargs): + """ + Perform an HTTP OPTIONS request + + :param url: Target URL + :type url: str + + :param kwargs: Additional arguments that will be transparently provided to the ``requests`` object, including + but not limited to query params, data, JSON, headers etc. + (see https://docs.python-requests.org/en/master/user/quickstart/#make-a-request) + :type kwargs: dict + """ + + return self._exec(method='options', url=url, **kwargs) + + @action + def download(self, url: str, path: str, **kwargs): + """ + Locally download the content of a remote URL. + + :param url: URL to be downloaded. + :param path: Path where the content will be downloaded on the local filesystem - must be a file name. + """ + path = os.path.abspath(os.path.expanduser(path)) + content = self._exec(method='get', url=url, output='binary', **kwargs) + + with open(path, 'wb') as f: + f.write(content) + + +# vim:sw=4:ts=4:et: diff --git a/platypush/plugins/http/request/manifest.yaml b/platypush/plugins/http/manifest.yaml similarity index 59% rename from platypush/plugins/http/request/manifest.yaml rename to platypush/plugins/http/manifest.yaml index b37744cc5..551ceef37 100644 --- a/platypush/plugins/http/request/manifest.yaml +++ b/platypush/plugins/http/manifest.yaml @@ -2,5 +2,5 @@ manifest: events: {} install: pip: [] - package: platypush.plugins.http.request + package: platypush.plugins.http type: plugin diff --git a/platypush/plugins/http/request/__init__.py b/platypush/plugins/http/request/__init__.py deleted file mode 100644 index 3384259e9..000000000 --- a/platypush/plugins/http/request/__init__.py +++ /dev/null @@ -1,184 +0,0 @@ -import logging -import os -import requests - -from platypush.message import Message -from platypush.plugins import Plugin, action - -logger = logging.getLogger(__name__) - - -class HttpRequestPlugin(Plugin): - """ - Plugin for executing custom HTTP requests. - - Some example usages:: - - # Execute a GET request on a JSON endpoint - { - "type": "request", - "action": "http.request.get", - "args": { - "url": "http://remote-host/api/v1/entity", - "params": { - "start": "2000-01-01" - } - } - } - - # Execute an action on another Platypush host through HTTP interface - { - "type": "request", - "action": "http.request.post", - "args": { - "url": "http://remote-host:8008/execute", - "json": { - "type": "request", - "target": "remote-host", - "action": "music.mpd.play" - } - } - } - """ - - def __init__(self, **kwargs): - super().__init__(**kwargs) - - @staticmethod - def _exec(method, url, output='text', **kwargs): - """ Available output types: text (default), json, binary """ - - if 'username' in kwargs and 'password' in kwargs: - kwargs['auth'] = (kwargs.pop('username'), kwargs.pop('password')) - - method = getattr(requests, method) - response = method(url, **kwargs) - response.raise_for_status() - - if output == 'json': - output = response.json() - if output == 'binary': - output = response.content - else: - output = response.text - - try: - # If the response is a Platypush JSON, extract it - output = Message.build(output) - except Exception as e: - logger.debug(e) - - return output - - @action - def get(self, url, **kwargs): - """ - Perform a GET request - - :param url: Target URL - :type url: str - - :param kwargs: Additional arguments that will be transparently provided to the ``requests`` object, including - but not limited to query params, data, JSON, headers etc. - (see https://docs.python-requests.org/en/master/user/quickstart/#make-a-request) - :type kwargs: dict - """ - - return self._exec(method='get', url=url, **kwargs) - - @action - def post(self, url, **kwargs): - """ - Perform a POST request - - :param url: Target URL - :type url: str - - :param kwargs: Additional arguments that will be transparently provided to the ``requests`` object, including - but not limited to query params, data, JSON, headers etc. - (see https://docs.python-requests.org/en/master/user/quickstart/#make-a-request) - :type kwargs: dict - """ - - return self._exec(method='post', url=url, **kwargs) - - @action - def head(self, url, **kwargs): - """ - Perform an HTTP HEAD request - - :param url: Target URL - :type url: str - - :param kwargs: Additional arguments that will be transparently provided to the ``requests`` object, including - but not limited to query params, data, JSON, headers etc. - (see https://docs.python-requests.org/en/master/user/quickstart/#make-a-request) - :type kwargs: dict - """ - - return self._exec(method='head', url=url, **kwargs) - - @action - def put(self, url, **kwargs): - """ - Perform a PUT request - - :param url: Target URL - :type url: str - - :param kwargs: Additional arguments that will be transparently provided to the ``requests`` object, including - but not limited to query params, data, JSON, headers etc. - (see https://docs.python-requests.org/en/master/user/quickstart/#make-a-request) - :type kwargs: dict - """ - - return self._exec(method='put', url=url, **kwargs) - - @action - def delete(self, url, **kwargs): - """ - Perform a DELETE request - - :param url: Target URL - :type url: str - - :param kwargs: Additional arguments that will be transparently provided to the ``requests`` object, including - but not limited to query params, data, JSON, headers etc. - (see https://docs.python-requests.org/en/master/user/quickstart/#make-a-request) - :type kwargs: dict - """ - - return self._exec(method='delete', url=url, **kwargs) - - @action - def options(self, url, **kwargs): - """ - Perform an HTTP OPTIONS request - - :param url: Target URL - :type url: str - - :param kwargs: Additional arguments that will be transparently provided to the ``requests`` object, including - but not limited to query params, data, JSON, headers etc. - (see https://docs.python-requests.org/en/master/user/quickstart/#make-a-request) - :type kwargs: dict - """ - - return self._exec(method='options', url=url, **kwargs) - - @action - def download(self, url: str, path: str, **kwargs): - """ - Locally download the content of a remote URL. - - :param url: URL to be downloaded. - :param path: Path where the content will be downloaded on the local filesystem - must be a file name. - """ - path = os.path.abspath(os.path.expanduser(path)) - content = self._exec(method='get', url=url, output='binary', **kwargs) - - with open(path, 'wb') as f: - f.write(content) - - -# vim:sw=4:ts=4:et: