zaqar/samples/zaqar/subscriber_service_sample.py
wangxiyuan 69c799734b Subscription Confirmation Support-1
The subscription confirmation feature will contain four patches:
1. webhook with mongoDB
2. email with mongoDB
3. webhook with redis
4. email with redis

This patch is the first part of subscription confirmation feature for
webhook with MongoDB. Others will be achieved in follow patches.

This patch did:
1. Add v2/queue/<queue_name>/subscription/<subscription_id>/confirm
endpoint.
2. Add a new config option: "require_confirmation".
3. Add a new property "confirmed" to subscription resource for
MongoDB driver.
4. Add a new policy "subscription: confirm".
5. Add a new property "message type" for notification.
6. Use the pre-signed url in confirm request.
8. Re-use POST subscription to allow re-confirm.
9. Update notification for webhook subscription with mongoDB.
10. Support unsubscrib the subscription
11. Add tests for the feature.
12. Add doc and sample.
Docimpact
APIimpact

Change-Id: Id38d4a5b4f9303b12e22e2b5c248facda4c00143
Implements: blueprint subscription-confirmation-support
2016-08-16 17:23:45 +08:00

80 lines
2.4 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import json
import logging
import requests
import sys
import uuid
try:
import SimpleHTTPServer
import SocketServer
except Exception:
from http import server as SimpleHTTPServer
import socketserver as SocketServer
_AUTO_CONFIRM = False
for arg in sys.argv:
if arg == '--auto-confirm':
_AUTO_CONFIRM = True
sys.argv.remove(arg)
break
if len(sys.argv) > 2:
PORT = int(sys.argv[2])
elif len(sys.argv) > 1:
PORT = int(sys.argv[1])
else:
PORT = 5678
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
"""This is the sample service for wsgi subscription.
"""
# TODO(wangxiyuan): support websocket.
def do_POST(self):
logging.warning('=================== POST =====================')
data_string = str(
self.rfile.read(int(self.headers['Content-Length'])))
self.data = json.loads(data_string)
if _AUTO_CONFIRM:
self._send_confirm_request()
message = 'OK'
self.send_response(200)
self.end_headers()
self.wfile.write(message)
logging.warning(self.headers)
logging.warning(self.data)
return
def _send_confirm_request(self):
url = self.data['WSGISubscribeURL']
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Project-ID': self.data['X-Project-ID'],
'Client-ID': str(uuid.uuid4()),
'URL-Methods': self.data['URL-Methods'],
'URL-Signature': self.data['URL-Signature'],
'URL-Paths': self.data['URL-Paths'],
'URL-Expires': self.data['URL-Expires'],
}
data = {'confirmed': True}
requests.put(url=url, data=json.dumps(data), headers=headers)
Handler = ServerHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
httpd.serve_forever()