LINT fixes
This commit is contained in:
parent
08119add97
commit
0f0f8f8a94
1 changed files with 31 additions and 21 deletions
|
@ -3,6 +3,7 @@ import requests
|
||||||
from platypush.message import Message
|
from platypush.message import Message
|
||||||
from platypush.plugins import Plugin, action
|
from platypush.plugins import Plugin, action
|
||||||
|
|
||||||
|
|
||||||
class HttpRequestPlugin(Plugin):
|
class HttpRequestPlugin(Plugin):
|
||||||
"""
|
"""
|
||||||
Plugin for executing custom HTTP requests.
|
Plugin for executing custom HTTP requests.
|
||||||
|
@ -40,10 +41,11 @@ class HttpRequestPlugin(Plugin):
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
def _exec(self, method, url, output='text', **kwargs):
|
@staticmethod
|
||||||
|
def _exec(method, url, output='text', **kwargs):
|
||||||
""" Available output types: text (default), json, binary """
|
""" Available output types: text (default), json, binary """
|
||||||
|
|
||||||
if 'username' in kwargs and 'password' in kwargs:
|
if 'username' in kwargs and 'password' in kwargs:
|
||||||
|
@ -53,19 +55,22 @@ class HttpRequestPlugin(Plugin):
|
||||||
response = method(url, **kwargs)
|
response = method(url, **kwargs)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
if output == 'json': output = response.json()
|
if output == 'json':
|
||||||
if output == 'binary': output = response.content
|
output = response.json()
|
||||||
else: output = response.text
|
if output == 'binary':
|
||||||
|
output = response.content
|
||||||
|
else:
|
||||||
|
output = response.text
|
||||||
|
|
||||||
|
# noinspection PyBroadException
|
||||||
try:
|
try:
|
||||||
# If the response is a Platypush JSON, extract it
|
# If the response is a Platypush JSON, extract it
|
||||||
output = Message.build(output)
|
output = Message.build(output)
|
||||||
except Exception as e:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def get(self, url, **kwargs):
|
def get(self, url, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -74,13 +79,14 @@ class HttpRequestPlugin(Plugin):
|
||||||
:param url: Target URL
|
:param url: Target URL
|
||||||
:type url: str
|
: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 http://docs.python-requests.org/en/master/user/quickstart/#make-a-request)
|
: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)
|
||||||
:type kwargs: dict
|
:type kwargs: dict
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self._exec(method='get', url=url, **kwargs)
|
return self._exec(method='get', url=url, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def post(self, url, **kwargs):
|
def post(self, url, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -89,13 +95,14 @@ class HttpRequestPlugin(Plugin):
|
||||||
:param url: Target URL
|
:param url: Target URL
|
||||||
:type url: str
|
: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 http://docs.python-requests.org/en/master/user/quickstart/#make-a-request)
|
: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)
|
||||||
:type kwargs: dict
|
:type kwargs: dict
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self._exec(method='post', url=url, **kwargs)
|
return self._exec(method='post', url=url, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def head(self, url, **kwargs):
|
def head(self, url, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -104,13 +111,14 @@ class HttpRequestPlugin(Plugin):
|
||||||
:param url: Target URL
|
:param url: Target URL
|
||||||
:type url: str
|
: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 http://docs.python-requests.org/en/master/user/quickstart/#make-a-request)
|
: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)
|
||||||
:type kwargs: dict
|
:type kwargs: dict
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self._exec(method='head', url=url, **kwargs)
|
return self._exec(method='head', url=url, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def put(self, url, **kwargs):
|
def put(self, url, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -119,13 +127,14 @@ class HttpRequestPlugin(Plugin):
|
||||||
:param url: Target URL
|
:param url: Target URL
|
||||||
:type url: str
|
: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 http://docs.python-requests.org/en/master/user/quickstart/#make-a-request)
|
: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)
|
||||||
:type kwargs: dict
|
:type kwargs: dict
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self._exec(method='put', url=url, **kwargs)
|
return self._exec(method='put', url=url, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def delete(self, url, **kwargs):
|
def delete(self, url, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -134,13 +143,14 @@ class HttpRequestPlugin(Plugin):
|
||||||
:param url: Target URL
|
:param url: Target URL
|
||||||
:type url: str
|
: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 http://docs.python-requests.org/en/master/user/quickstart/#make-a-request)
|
: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)
|
||||||
:type kwargs: dict
|
:type kwargs: dict
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self._exec(method='delete', url=url, **kwargs)
|
return self._exec(method='delete', url=url, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def options(self, url, **kwargs):
|
def options(self, url, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -149,12 +159,12 @@ class HttpRequestPlugin(Plugin):
|
||||||
:param url: Target URL
|
:param url: Target URL
|
||||||
:type url: str
|
: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 http://docs.python-requests.org/en/master/user/quickstart/#make-a-request)
|
: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)
|
||||||
:type kwargs: dict
|
:type kwargs: dict
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self._exec(method='options', url=url, **kwargs)
|
return self._exec(method='options', url=url, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue