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):
""" Gets the reservation updates """
def __init__(self, hotel_id, token, *args, **kwargs):
self.hotel_id = hotel_id
self.token = token

View File

@ -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 {}

View File

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

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: