From 76f7bbc529da8fff5579c04abb3951362cb0cbcb Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 12 Jan 2018 15:18:06 +0100 Subject: [PATCH] Refactored HTTP request plugin and fixed bug on the plugin name --- .../http/request/ota/booking/__init__.py | 1 + platypush/context/__init__.py | 8 ++-- .../http/{request.py => request/__init__.py} | 9 +++-- .../plugins/http/request/ota/__init__.py | 0 .../http/request/ota/booking/__init__.py | 40 +++++++++++++++++++ 5 files changed, 50 insertions(+), 8 deletions(-) rename platypush/plugins/http/{request.py => request/__init__.py} (81%) create mode 100644 platypush/plugins/http/request/ota/__init__.py create mode 100644 platypush/plugins/http/request/ota/booking/__init__.py diff --git a/platypush/backend/http/request/ota/booking/__init__.py b/platypush/backend/http/request/ota/booking/__init__.py index 2fb9c6574..ea92e5455 100644 --- a/platypush/backend/http/request/ota/booking/__init__.py +++ b/platypush/backend/http/request/ota/booking/__init__.py @@ -7,6 +7,7 @@ from platypush.message.event.http.ota.booking import NewReservationEvent class GetReservationUpdates(JsonHttpRequest): + """ Gets the reservation updates """ def __init__(self, hotel_id, token, *args, **kwargs): self.hotel_id = hotel_id self.token = token diff --git a/platypush/context/__init__.py b/platypush/context/__init__.py index e0e3455b8..d5d9829fc 100644 --- a/platypush/context/__init__.py +++ b/platypush/context/__init__.py @@ -66,10 +66,10 @@ def get_plugin(plugin_name, reload=False): raise RuntimeError(e) # e.g. plugins.music.mpd main class: MusicMpdPlugin - cls_name = functools.reduce( - lambda a,b: a.title() + b.title(), - (plugin_name.title().split('.')) - ) + 'Plugin' + cls_name = '' + for token in plugin_name.split('.'): + cls_name += token.title() + cls_name += 'Plugin' plugin_conf = Config.get_plugins()[plugin_name] \ if plugin_name in Config.get_plugins() else {} diff --git a/platypush/plugins/http/request.py b/platypush/plugins/http/request/__init__.py similarity index 81% rename from platypush/plugins/http/request.py rename to platypush/plugins/http/request/__init__.py index ae6b6e0db..b0ed179d4 100644 --- a/platypush/plugins/http/request.py +++ b/platypush/plugins/http/request/__init__.py @@ -2,7 +2,7 @@ import requests from platypush.message.response import Response -from .. import Plugin +from platypush.plugins import Plugin class HttpRequestPlugin(Plugin): """ Plugin for executing custom HTTP requests """ @@ -13,10 +13,11 @@ class HttpRequestPlugin(Plugin): method = getattr(requests, method) response = method(url, **kwargs) response.raise_for_status() + output = response.text - if output == 'json': return response.json() - if output == 'binary': return response.content - return Response(output=response.text, errors=[]) + if output == 'json': output = response.json() + if output == 'binary': output = response.content + return Response(output=output, errors=[]) def get(self, url, **kwargs): diff --git a/platypush/plugins/http/request/ota/__init__.py b/platypush/plugins/http/request/ota/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/platypush/plugins/http/request/ota/booking/__init__.py b/platypush/plugins/http/request/ota/booking/__init__.py new file mode 100644 index 000000000..da810ee8a --- /dev/null +++ b/platypush/plugins/http/request/ota/booking/__init__.py @@ -0,0 +1,40 @@ +import datetime +import dateutil.parser + +from platypush.plugins.http.request import HttpRequestPlugin + +class HttpRequestOtaBookingPlugin(HttpRequestPlugin): + """ Plugin to send requests to the Booking Hub API """ + + def __init__(self, hotel_id, token, timeout=5, **kwargs): + self.hotel_id = hotel_id + self.token = token + self.timeout = timeout + + + def get_reservations(self, day='today'): + url = 'https://hub-api.booking.com/v1/hotels/{}/reservations' \ + .format(self.hotel_id) + + today = datetime.date.today().isoformat() + if day == 'today': day = today + + headers = { 'X-Booking-Auth-Token': self.token } + params = { 'checkin': day } + + response = self.get(url, headers=headers, params=params, + output='json', timeout=self.timeout) + + reservations = [res for res in response.output + if res['status'] != 'CANCELLED'] + + response.output = { + 'reservations': reservations, + 'n_reservations': len(reservations), + } + + return response + + +# vim:sw=4:ts=4:et: +