2018-01-09 18:44:45 +01:00
|
|
|
import importlib
|
|
|
|
import time
|
2018-01-09 01:26:51 +01:00
|
|
|
|
2018-01-09 18:44:45 +01:00
|
|
|
from platypush.bus import Bus
|
|
|
|
from platypush.backend import Backend
|
|
|
|
from platypush.backend.http.request import HttpRequest
|
2018-01-09 01:26:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HttpPollBackend(Backend):
|
|
|
|
"""
|
|
|
|
This backend will poll multiple HTTP endpoints/services and return events
|
|
|
|
the bus whenever something new happened. Example configuration:
|
|
|
|
|
|
|
|
backend.http.poll:
|
2018-01-09 18:44:45 +01:00
|
|
|
requests:
|
2018-01-09 01:26:51 +01:00
|
|
|
-
|
2018-01-09 18:44:45 +01:00
|
|
|
method: GET
|
|
|
|
type: platypush.backend.http.request.JsonHttpRequest
|
2018-01-09 01:26:51 +01:00
|
|
|
args:
|
2018-01-09 18:44:45 +01:00
|
|
|
url: https://hub-api.booking.com/v1/hotels/84326/reservations
|
|
|
|
headers:
|
|
|
|
X-Booking-Auth-Token: UXsYtIMJKCJB07/P/5Tz1iV8lzVY5kVVF0ZEnQRe+cg0
|
|
|
|
params:
|
|
|
|
updatedSince: 2018-01-09
|
|
|
|
|
|
|
|
poll_seconds: 10 # Check for updates on this endpoint every 10 seconds (default: 60)
|
|
|
|
timeout: 5 # Times out after 5 seconds (default)
|
2018-01-09 01:26:51 +01:00
|
|
|
"""
|
|
|
|
|
2018-01-09 18:44:45 +01:00
|
|
|
def __init__(self, requests, *args, **kwargs):
|
2018-01-09 01:26:51 +01:00
|
|
|
"""
|
|
|
|
Params:
|
2018-01-09 18:44:45 +01:00
|
|
|
requests -- List/iterable of HttpRequest objects
|
2018-01-09 01:26:51 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2018-01-09 18:44:45 +01:00
|
|
|
|
|
|
|
self.requests = []
|
|
|
|
self.http_bus = Bus()
|
|
|
|
|
|
|
|
for request in requests:
|
|
|
|
if isinstance(request, dict):
|
|
|
|
type = request['type']
|
|
|
|
(module, name) = ('.'.join(type.split('.')[:-1]), type.split('.')[-1])
|
|
|
|
module = importlib.import_module(module)
|
|
|
|
request = getattr(module, name)(bus=self.http_bus, **request)
|
|
|
|
elif isinstance(request, HttpRequest):
|
|
|
|
request.bus = self.http_bus
|
|
|
|
else:
|
|
|
|
raise RuntimeError('Request should either be a dict or a ' +
|
|
|
|
'HttpRequest object, {} found'.format(type(request)))
|
|
|
|
|
|
|
|
self.requests.append(request)
|
2018-01-09 01:26:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
super().run()
|
|
|
|
|
2018-01-09 18:44:45 +01:00
|
|
|
while not self.should_stop():
|
|
|
|
for request in self.requests:
|
|
|
|
if not request.is_alive() and (
|
|
|
|
request.last_call_timestamp is None or
|
|
|
|
time.time() - request.last_call_timestamp > request.poll_seconds):
|
|
|
|
response = request.execute()
|
|
|
|
print('**** RESPONSE: {}'.format(response))
|
2018-01-09 01:26:51 +01:00
|
|
|
|
2018-01-09 18:44:45 +01:00
|
|
|
time.sleep(0.1)
|
2018-01-09 01:26:51 +01:00
|
|
|
|
2018-01-09 18:44:45 +01:00
|
|
|
def send_message(self, msg):
|
|
|
|
self.http_bus.post(msg)
|
2018-01-09 01:26:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|