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

1 import json

2

3 from flask import current_app

4

5 from hookee.pluginmanager import RESPONSE_PLUGIN

6 from hookee.util import PrintUtil

7

8 __author__ = "Alex Laird"

9 __copyright__ = "Copyright 2023, Alex Laird"

10 __version__ = "1.2.2"

11

12 plugin_type = RESPONSE_PLUGIN

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

14

15 print_util = None # type: PrintUtil

16

17

18 def setup(hookee_manager):

19 global print_util

20

21 print_util = hookee_manager.print_util

22

23

24 def 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