-
Notifications
You must be signed in to change notification settings - Fork 12
/
flask_receiver.py
96 lines (67 loc) · 2.88 KB
/
flask_receiver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (c) 2020 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately 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.
"""
__author__ = "Gabriel Zapodeanu TME, ENB"
__email__ = "gzapodea@cisco.com"
__version__ = "0.1.0"
__copyright__ = "Copyright (c) 2020 Cisco and/or its affiliates."
__license__ = "Cisco Sample Code License, Version 1.1"
import urllib3
import json
import os
import time
from flask import Flask, request, abort, send_from_directory
from flask_basicauth import BasicAuth
from urllib3.exceptions import InsecureRequestWarning # for insecure https warnings
os.environ['TZ'] = 'America/Los_Angeles' # define the timezone for PST
time.tzset() # adjust the timezone, more info https://help.pythonanywhere.com/pages/SettingTheTimezone/
urllib3.disable_warnings(InsecureRequestWarning) # disable insecure https warnings
from config import WEBHOOK_URL, WEBHOOK_USERNAME, WEBHOOK_PASSWORD
def pprint(json_data):
"""
Pretty print JSON formatted data
:param json_data:
:return:
"""
print(json.dumps(json_data, indent=4, separators=(' , ', ' : ')))
app = Flask(__name__)
app.config['BASIC_AUTH_USERNAME'] = WEBHOOK_USERNAME
app.config['BASIC_AUTH_PASSWORD'] = WEBHOOK_PASSWORD
# app.config['BASIC_AUTH_FORCE'] = True
basic_auth = BasicAuth(app)
@app.route('/') # create a page for testing the flask framework
@basic_auth.required
def index():
return '<h1>Flask Receiver App is Up!</h1>', 200
@app.route('/webhook', methods=['POST']) # create a route for /webhook, method POST
@basic_auth.required
def webhook():
if request.method == 'POST':
print('Webhook Received')
request_json = request.json
# print the received notification
print('Payload: ')
pprint(request_json)
# save as a file, create new file if not existing, append to existing file
# full details of each notification to file 'all_webhooks_detailed.json'
with open('all_webhooks_detailed.json', 'a') as filehandle:
filehandle.write('%s\n' % json.dumps(request_json))
# steps required by the notification, to be developed for end user use cases
notification = request_json
return 'Webhook notification received', 202
else:
return 'POST Method not supported', 405
if __name__ == '__main__':
app.run(port=5443, ssl_context='adhoc', debug=True)