Skip to content

Commit 9d0c1b0

Browse files
authored
Merge pull request #52 from d4rkstar/main
chore(operator): content-type json
2 parents 2bb6fa3 + 5f82b42 commit 9d0c1b0

File tree

10 files changed

+103
-28
lines changed

10 files changed

+103
-28
lines changed

actions/common/util.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# under the License.
1717
#
1818

19-
import mimetypes
19+
import json
2020

2121
def get_env_value(user_data, key):
2222
"""
@@ -32,4 +32,15 @@ def get_env_value(user_data, key):
3232
if env['key'] == key:
3333
return env['value']
3434

35-
return None
35+
return None
36+
37+
def is_json(value):
38+
if isinstance(value, (dict, list)): # Already a JSON-compatible structure
39+
return True
40+
if isinstance(value, str): # Try parsing if it's a string
41+
try:
42+
json.loads(value)
43+
return True
44+
except json.JSONDecodeError:
45+
return False
46+
return False

actions/devel/ferretdb/__main__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#
1818
import json
1919

20+
from common.util import is_json
2021
from common.authorize import Authorize
2122
from common.command_data import CommandData
2223
from command.ferretbd import FerretDB
@@ -31,12 +32,15 @@ def build_error(message: str):
3132
"body": message
3233
}
3334

34-
def build_response(data:CommandData):
35+
def build_response(data: CommandData):
3536
meta_data = data.get_metadata()
36-
return {
37+
result = {
3738
"statusCode": meta_data['status'],
3839
"body": meta_data['result']
3940
}
41+
if is_json(meta_data['result']):
42+
result['headers'] = { 'Content-Type': 'application/json' }
43+
return result
4044

4145
def parse_body(args):
4246
try:

actions/devel/ferretdb/common/util.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# under the License.
1717
#
1818

19-
import mimetypes
19+
import json
2020

2121
def get_env_value(user_data, key):
2222
"""
@@ -32,4 +32,15 @@ def get_env_value(user_data, key):
3232
if env['key'] == key:
3333
return env['value']
3434

35-
return None
35+
return None
36+
37+
def is_json(value):
38+
if isinstance(value, (dict, list)): # Already a JSON-compatible structure
39+
return True
40+
if isinstance(value, str): # Try parsing if it's a string
41+
try:
42+
json.loads(value)
43+
return True
44+
except json.JSONDecodeError:
45+
return False
46+
return False

actions/devel/minio/__main__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
#
18-
import nuvolaris.config as cfg
19-
import nuvolaris.couchdb_util as cu
20-
import logging, json
2118

19+
import json
20+
21+
from common.util import is_json
2222
from common.authorize import Authorize
2323
from command.minio import Minio
2424
from common.command_data import CommandData
@@ -33,12 +33,15 @@ def build_error(message: str):
3333
"body": message
3434
}
3535

36-
def build_response(data:CommandData):
36+
def build_response(data: CommandData):
3737
meta_data = data.get_metadata()
38-
return {
38+
result = {
3939
"statusCode": meta_data['status'],
4040
"body": meta_data['result']
4141
}
42+
if is_json(meta_data['result']):
43+
result['headers'] = { 'Content-Type': 'application/json' }
44+
return result
4245

4346
def parse_body(args):
4447
try:

actions/devel/minio/common/util.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# under the License.
1717
#
1818

19-
import mimetypes
19+
import json
2020

2121
def get_env_value(user_data, key):
2222
"""
@@ -32,4 +32,15 @@ def get_env_value(user_data, key):
3232
if env['key'] == key:
3333
return env['value']
3434

35-
return None
35+
return None
36+
37+
def is_json(value):
38+
if isinstance(value, (dict, list)): # Already a JSON-compatible structure
39+
return True
40+
if isinstance(value, str): # Try parsing if it's a string
41+
try:
42+
json.loads(value)
43+
return True
44+
except json.JSONDecodeError:
45+
return False
46+
return False

actions/devel/psql/__main__.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import json
1919
from base64 import b64decode
2020

21+
from common.util import is_json
2122
from command.psql import Psql
2223
from common.authorize import Authorize
2324
from common.command_data import CommandData
@@ -26,26 +27,33 @@
2627
class ApiError(Exception):
2728
pass
2829

30+
2931
def build_error(message: str):
3032
return {
3133
"statusCode": 400,
3234
"body": message
3335
}
3436

35-
def build_response(data:CommandData):
37+
38+
def build_response(data: CommandData):
3639
meta_data = data.get_metadata()
37-
return {
40+
result = {
3841
"statusCode": meta_data['status'],
3942
"body": meta_data['result']
4043
}
44+
if is_json(meta_data['result']):
45+
result['headers'] = { 'Content-Type': 'application/json' }
46+
return result
47+
4148

4249
def parse_body(args):
4350
try:
44-
return b64decode(args['__ow_body']).decode().strip()
51+
return b64decode(args['__ow_body']).decode().strip()
4552
except Exception as e:
4653
print(e)
4754
raise ApiError("could not parse __ow_body as base64")
4855

56+
4957
def main(args):
5058
"""
5159
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):
6472

6573
if len(args['__ow_body']) == 0:
6674
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'])
7079
cmd = CommandData(json.loads(parse_body(args)))
7180
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)}")

actions/devel/psql/command/psql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _query(self, input:CommandData):
5252
# Open a cursor to perform database operations
5353
with conn.cursor(row_factory=dict_row) as cur:
5454
cur.execute(query)
55-
result = cur.fetchall()
55+
result = cur.fetchall()
5656
input.result(json.dumps(result))
5757
input.status(200)
5858
return input

actions/devel/psql/common/util.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# under the License.
1717
#
1818

19-
import mimetypes
19+
import json
2020

2121
def get_env_value(user_data, key):
2222
"""
@@ -32,4 +32,15 @@ def get_env_value(user_data, key):
3232
if env['key'] == key:
3333
return env['value']
3434

35-
return None
35+
return None
36+
37+
def is_json(value):
38+
if isinstance(value, (dict, list)): # Already a JSON-compatible structure
39+
return True
40+
if isinstance(value, str): # Try parsing if it's a string
41+
try:
42+
json.loads(value)
43+
return True
44+
except json.JSONDecodeError:
45+
return False
46+
return False

actions/devel/redis/__main__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#
1818
import json
1919

20+
from common.util import is_json
2021
from common.authorize import Authorize
2122
from common.command_data import CommandData
2223
from command.redis import Redis
@@ -31,12 +32,15 @@ def build_error(message: str):
3132
"body": message
3233
}
3334

34-
def build_response(data:CommandData):
35+
def build_response(data: CommandData):
3536
meta_data = data.get_metadata()
36-
return {
37+
result = {
3738
"statusCode": meta_data['status'],
3839
"body": meta_data['result']
3940
}
41+
if is_json(meta_data['result']):
42+
result['headers'] = { 'Content-Type': 'application/json' }
43+
return result
4044

4145
def parse_body(args):
4246
try:

actions/devel/redis/common/util.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# under the License.
1717
#
1818

19-
import mimetypes
19+
import json
2020

2121
def get_env_value(user_data, key):
2222
"""
@@ -32,4 +32,15 @@ def get_env_value(user_data, key):
3232
if env['key'] == key:
3333
return env['value']
3434

35-
return None
35+
return None
36+
37+
def is_json(value):
38+
if isinstance(value, (dict, list)): # Already a JSON-compatible structure
39+
return True
40+
if isinstance(value, str): # Try parsing if it's a string
41+
try:
42+
json.loads(value)
43+
return True
44+
except json.JSONDecodeError:
45+
return False
46+
return False

0 commit comments

Comments
 (0)