Coverage for hookee/plugins/response_echo.py: 100.00%

23 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-04 15:06 +0000

1import json 

2 

3from flask import current_app 

4 

5from hookee.pluginmanager import RESPONSE_PLUGIN 

6from hookee.util import PrintUtil 

7 

8__author__ = "Alex Laird" 

9__copyright__ = "Copyright 2023, Alex Laird" 

10__version__ = "1.2.2" 

11 

12plugin_type = RESPONSE_PLUGIN 

13description = "If the `response` object has not been initialized, create a response that echo's back the request data." 

14 

15print_util = None # type: PrintUtil 

16 

17 

18def setup(hookee_manager): 

19 global print_util 

20 

21 print_util = hookee_manager.print_util 

22 

23 

24def run(request, response): 

25 if not response: 

26 data = "" 

27 content_type = request.headers.get("Content-Type") 

28 if request.form and not request.data: 

29 data = json.dumps(dict(request.form)) 

30 content_type = "application/json" 

31 elif request.data: 

32 data = request.data.decode("utf-8") 

33 

34 response = current_app.response_class( 

35 data, 

36 mimetype=content_type, 

37 ) 

38 

39 return response