forked from platypush/platypush
Added HTTP requests plugin, #42
This commit is contained in:
parent
109805fd8d
commit
1df10bc97e
1 changed files with 47 additions and 0 deletions
47
platypush/plugins/http/request.py
Normal file
47
platypush/plugins/http/request.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
import requests
|
||||
|
||||
from platypush.message.response import Response
|
||||
|
||||
from .. import Plugin
|
||||
|
||||
class HttpRequestPlugin(Plugin):
|
||||
""" Plugin for executing custom HTTP requests """
|
||||
|
||||
def _exec(self, method, url, output='text', **kwargs):
|
||||
""" Available output types: text (default), json, binary """
|
||||
|
||||
method = getattr(requests, method)
|
||||
response = method(url, **kwargs)
|
||||
response.raise_for_status()
|
||||
|
||||
if output == 'json': return response.json()
|
||||
if output == 'binary': return response.content
|
||||
return Response(output=response.text, errors=[])
|
||||
|
||||
|
||||
def get(self, url, **kwargs):
|
||||
return self._exec(method='get', url=url, **kwargs)
|
||||
|
||||
|
||||
def post(self, url, **kwargs):
|
||||
return self._exec(method='post', url=url, **kwargs)
|
||||
|
||||
|
||||
def head(self, url, **kwargs):
|
||||
return self._exec(method='head', url=url, **kwargs)
|
||||
|
||||
|
||||
def put(self, url, **kwargs):
|
||||
return self._exec(method='put', url=url, **kwargs)
|
||||
|
||||
|
||||
def delete(self, url, **kwargs):
|
||||
return self._exec(method='delete', url=url, **kwargs)
|
||||
|
||||
|
||||
def options(self, url, **kwargs):
|
||||
return self._exec(method='options', url=url, **kwargs)
|
||||
|
||||
|
||||
# vim:sw=4:ts=4:et:
|
||||
|
Loading…
Reference in a new issue