Refactored HTTP request plugin and fixed bug on the plugin name
This commit is contained in:
parent
b1f42c22ae
commit
76f7bbc529
5 changed files with 50 additions and 8 deletions
|
@ -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
|
||||||
|
|
|
@ -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 {}
|
||||||
|
|
|
@ -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):
|
0
platypush/plugins/http/request/ota/__init__.py
Normal file
0
platypush/plugins/http/request/ota/__init__.py
Normal file
40
platypush/plugins/http/request/ota/booking/__init__.py
Normal file
40
platypush/plugins/http/request/ota/booking/__init__.py
Normal 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:
|
||||||
|
|
Loading…
Reference in a new issue