18
18
import json
19
19
from base64 import b64decode
20
20
21
+ from common .util import is_json
21
22
from command .psql import Psql
22
23
from common .authorize import Authorize
23
24
from common .command_data import CommandData
26
27
class ApiError (Exception ):
27
28
pass
28
29
30
+
29
31
def build_error (message : str ):
30
32
return {
31
33
"statusCode" : 400 ,
32
34
"body" : message
33
35
}
34
36
35
- def build_response (data :CommandData ):
37
+
38
+ def build_response (data : CommandData ):
36
39
meta_data = data .get_metadata ()
37
- return {
40
+ result = {
38
41
"statusCode" : meta_data ['status' ],
39
42
"body" : meta_data ['result' ]
40
43
}
44
+ if is_json (meta_data ['result' ]):
45
+ result ['headers' ] = { 'Content-Type' : 'application/json' }
46
+ return result
47
+
41
48
42
49
def parse_body (args ):
43
50
try :
44
- return b64decode (args ['__ow_body' ]).decode ().strip ()
51
+ return b64decode (args ['__ow_body' ]).decode ().strip ()
45
52
except Exception as e :
46
53
print (e )
47
54
raise ApiError ("could not parse __ow_body as base64" )
48
55
56
+
49
57
def main (args ):
50
58
"""
51
59
Action implementing a generic command wrapper for the nuv devel plugin. The action must be called with a POST request receiving a JSON
@@ -64,10 +72,11 @@ def main(args):
64
72
65
73
if len (args ['__ow_body' ]) == 0 :
66
74
return build_error ("invalid request, no command payload received" )
67
-
68
- try :
69
- user_data = Authorize (args ['couchdb_host' ],args ['couchdb_user' ],args ['couchdb_password' ]).login (headers ['x-impersonate-auth' ])
75
+
76
+ try :
77
+ user_data = Authorize (args ['couchdb_host' ], args ['couchdb_user' ], args ['couchdb_password' ]).login (
78
+ headers ['x-impersonate-auth' ])
70
79
cmd = CommandData (json .loads (parse_body (args )))
71
80
return build_response (Psql (user_data ).execute (cmd ))
72
- except Exception as e :
73
- return build_error (f"failed to execute nuv devel psql. Reason: { str (e )} " )
81
+ except Exception as e :
82
+ return build_error (f"failed to execute nuv devel psql. Reason: { str (e )} " )
0 commit comments