platypush/platypush/plugins/http/request/__init__.py

189 lines
5.6 KiB
Python
Raw Normal View History

2021-04-06 21:10:48 +02:00
import logging
2020-01-01 15:40:42 +01:00
import os
2018-01-05 20:00:11 +01:00
import requests
from platypush.message import Message
from platypush.plugins import Plugin, action
2018-01-05 20:00:11 +01:00
2021-04-06 21:10:48 +02:00
logger = logging.getLogger(__name__)
2019-12-05 00:37:36 +01:00
2018-01-05 20:00:11 +01:00
class HttpRequestPlugin(Plugin):
2018-06-25 00:49:45 +02:00
"""
Plugin for executing custom HTTP requests.
Requires:
* **requests** (``pip install 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"
}
}
}
"""
2018-01-05 20:00:11 +01:00
2019-12-05 00:37:36 +01:00
def __init__(self, **kwargs):
super().__init__(**kwargs)
2019-12-05 00:37:36 +01:00
@staticmethod
def _exec(method, url, output='text', **kwargs):
2018-01-05 20:00:11 +01:00
""" Available output types: text (default), json, binary """
if 'username' in kwargs and 'password' in kwargs:
kwargs['auth'] = (kwargs.pop('username'), kwargs.pop('password'))
2018-01-05 20:00:11 +01:00
method = getattr(requests, method)
response = method(url, **kwargs)
response.raise_for_status()
2019-12-05 00:37:36 +01:00
if output == 'json':
output = response.json()
if output == 'binary':
output = response.content
else:
output = response.text
2018-07-18 20:23:11 +02:00
try:
# If the response is a Platypush JSON, extract it
output = Message.build(output)
2021-04-06 21:10:48 +02:00
except Exception as e:
logger.debug(e)
return output
2018-01-05 20:00:11 +01:00
@action
2018-01-05 20:00:11 +01:00
def get(self, url, **kwargs):
2018-06-25 00:49:45 +02:00
"""
Perform a GET request
:param url: Target URL
:type url: str
2019-12-05 00:37:36 +01:00
: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 http://docs.python-requests.org/en/master/user/quickstart/#make-a-request)
2018-06-25 00:49:45 +02:00
:type kwargs: dict
"""
2018-01-05 20:00:11 +01:00
return self._exec(method='get', url=url, **kwargs)
@action
2018-01-05 20:00:11 +01:00
def post(self, url, **kwargs):
2018-06-25 00:49:45 +02:00
"""
Perform a POST request
:param url: Target URL
:type url: str
2019-12-05 00:37:36 +01:00
: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 http://docs.python-requests.org/en/master/user/quickstart/#make-a-request)
2018-06-25 00:49:45 +02:00
:type kwargs: dict
"""
2018-01-05 20:00:11 +01:00
return self._exec(method='post', url=url, **kwargs)
@action
2018-01-05 20:00:11 +01:00
def head(self, url, **kwargs):
2018-06-25 00:49:45 +02:00
"""
Perform an HTTP HEAD request
:param url: Target URL
:type url: str
2019-12-05 00:37:36 +01:00
: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 http://docs.python-requests.org/en/master/user/quickstart/#make-a-request)
2018-06-25 00:49:45 +02:00
:type kwargs: dict
"""
2018-01-05 20:00:11 +01:00
return self._exec(method='head', url=url, **kwargs)
@action
2018-01-05 20:00:11 +01:00
def put(self, url, **kwargs):
2018-06-25 00:49:45 +02:00
"""
Perform a PUT request
:param url: Target URL
:type url: str
2019-12-05 00:37:36 +01:00
: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 http://docs.python-requests.org/en/master/user/quickstart/#make-a-request)
2018-06-25 00:49:45 +02:00
:type kwargs: dict
"""
2018-01-05 20:00:11 +01:00
return self._exec(method='put', url=url, **kwargs)
@action
2018-01-05 20:00:11 +01:00
def delete(self, url, **kwargs):
2018-06-25 00:49:45 +02:00
"""
Perform a DELETE request
:param url: Target URL
:type url: str
2019-12-05 00:37:36 +01:00
: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 http://docs.python-requests.org/en/master/user/quickstart/#make-a-request)
2018-06-25 00:49:45 +02:00
:type kwargs: dict
"""
2018-01-05 20:00:11 +01:00
return self._exec(method='delete', url=url, **kwargs)
@action
2018-01-05 20:00:11 +01:00
def options(self, url, **kwargs):
2018-06-25 00:49:45 +02:00
"""
Perform an HTTP OPTIONS request
:param url: Target URL
:type url: str
2019-12-05 00:37:36 +01:00
: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 http://docs.python-requests.org/en/master/user/quickstart/#make-a-request)
2018-06-25 00:49:45 +02:00
:type kwargs: dict
"""
2018-01-05 20:00:11 +01:00
return self._exec(method='options', url=url, **kwargs)
2020-01-01 15:40:42 +01:00
@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)
2018-01-05 20:00:11 +01:00
# vim:sw=4:ts=4:et: