From 1df10bc97e77019ba3565a396de77b77826cf89c Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 5 Jan 2018 20:00:11 +0100 Subject: [PATCH] Added HTTP requests plugin, #42 --- platypush/plugins/http/request.py | 47 +++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 platypush/plugins/http/request.py diff --git a/platypush/plugins/http/request.py b/platypush/plugins/http/request.py new file mode 100644 index 0000000000..ae6b6e0db3 --- /dev/null +++ b/platypush/plugins/http/request.py @@ -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: +