Update version number in container-tag for F Maintenance Release
[sim/a1-interface.git] / near-rt-ric-simulator / test / KAFKA_DISPATCHER / src / payload_logging.py
1 #  ============LICENSE_START===============================================
2 #  Copyright (C) 2022 Nordix Foundation. All rights reserved.
3 #  ========================================================================
4 #  Licensed under the Apache License, Version 2.0 (the "License");
5 #  you may not use this file except in compliance with the License.
6 #  You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #  Unless required by applicable law or agreed to in writing, software
11 #  distributed under the License is distributed on an "AS IS" BASIS,
12 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #  See the License for the specific language governing permissions and
14 #  limitations under the License.
15 #  ============LICENSE_END=================================================
16 #
17
18 from var_declaration import app
19 from flask import Flask, request, Response
20
21 #Constants
22 TEXT_PLAIN='text/plain'
23
24 #Vars
25 payload_log=True
26
27 #Function to activate/deactivate http header and payload logging
28 @app.route('/payload_logging/<state>', methods=['POST', 'PUT'])
29 def set_payload_logging(state):
30   global payload_log
31   if (state == "on"):
32     payload_log=True
33   elif (state == "off"):
34     payload_log=False
35   else:
36     return Response("Unknown state: "+state+" - use 'on' or 'off'", 400, mimetype=TEXT_PLAIN)
37
38   return Response("Payload and header logging set to: "+state, 200, mimetype=TEXT_PLAIN)
39
40 # Generic function to log http header and payload - called before the request
41 @app.app.before_request
42 def log_request_info():
43     if (payload_log is True):
44         print('')
45         print('-----Request-----')
46         print('Req Headers: ', request.headers)
47         print('Req Body: ', request.get_data())
48
49 # Generic function to log http header and payload - called after the response
50 @app.app.after_request
51 def log_response_info(response):
52     if (payload_log is True):
53         print('-----Response-----')
54         print('Resp Headers: ', response.headers)
55         print('Resp Body: ', response.get_data())
56     return response
57
58 # Helper function to check loggin state
59 def is_payload_logging():
60   return payload_log