Refactored HTTP request plugin and fixed bug on the plugin name

This commit is contained in:
Fabio Manganiello 2018-01-12 15:18:06 +01:00
parent b1f42c22ae
commit 76f7bbc529
5 changed files with 50 additions and 8 deletions

View file

@ -7,6 +7,7 @@ from platypush.message.event.http.ota.booking import NewReservationEvent
class GetReservationUpdates(JsonHttpRequest): class GetReservationUpdates(JsonHttpRequest):
""" Gets the reservation updates """
def __init__(self, hotel_id, token, *args, **kwargs): def __init__(self, hotel_id, token, *args, **kwargs):
self.hotel_id = hotel_id self.hotel_id = hotel_id
self.token = token self.token = token

View file

@ -66,10 +66,10 @@ def get_plugin(plugin_name, reload=False):
raise RuntimeError(e) raise RuntimeError(e)
# e.g. plugins.music.mpd main class: MusicMpdPlugin # e.g. plugins.music.mpd main class: MusicMpdPlugin
cls_name = functools.reduce( cls_name = ''
lambda a,b: a.title() + b.title(), for token in plugin_name.split('.'):
(plugin_name.title().split('.')) cls_name += token.title()
) + 'Plugin' cls_name += 'Plugin'
plugin_conf = Config.get_plugins()[plugin_name] \ plugin_conf = Config.get_plugins()[plugin_name] \
if plugin_name in Config.get_plugins() else {} if plugin_name in Config.get_plugins() else {}

View file

@ -2,7 +2,7 @@ import requests
from platypush.message.response import Response from platypush.message.response import Response
from .. import Plugin from platypush.plugins import Plugin
class HttpRequestPlugin(Plugin): class HttpRequestPlugin(Plugin):
""" Plugin for executing custom HTTP requests """ """ Plugin for executing custom HTTP requests """
@ -13,10 +13,11 @@ class HttpRequestPlugin(Plugin):
method = getattr(requests, method) method = getattr(requests, method)
response = method(url, **kwargs) response = method(url, **kwargs)
response.raise_for_status() response.raise_for_status()
output = response.text
if output == 'json': return response.json() if output == 'json': output = response.json()
if output == 'binary': return response.content if output == 'binary': output = response.content
return Response(output=response.text, errors=[]) return Response(output=output, errors=[])
def get(self, url, **kwargs): def get(self, url, **kwargs):

View file

@ -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: