Skip to content

Commit b1f7a39

Browse files
authored
Merge pull request #1 from Moesif/add-json-object-mock-request
Add: Mock request when json object in the body of request
2 parents ee849d4 + 7a747bd commit b1f7a39

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

moesifpythonrequest/data_preparation/transform_data.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@
66

77

88
class DataPreparation():
9+
10+
# Function to base64 encode data
11+
def base64_encode(self, data):
12+
try:
13+
if global_variables.DEBUG:
14+
print("about to parse request body as base64")
15+
encoded_body = base64.standard_b64encode(data)
16+
transfer_encoding = 'base64'
17+
if global_variables.DEBUG:
18+
print("base64 encoded body: " + encoded_body)
19+
except:
20+
if global_variables.DEBUG:
21+
print("Request body is of type other than json or base64")
22+
encoded_body = None
23+
transfer_encoding = None
24+
25+
return encoded_body, transfer_encoding
26+
927
# Function to prepare the data
1028
def transform_data(self, method, url, start_time, end_time, response, kwargs):
1129

@@ -20,25 +38,25 @@ def transform_data(self, method, url, start_time, end_time, response, kwargs):
2038
if kwargs.get('data', None):
2139
try:
2240
if global_variables.DEBUG:
23-
print("about to parse request body as json")
24-
req_body = json.loads(kwargs.get('data', None))
41+
print("about to parse request data as json")
42+
req_body = json.loads(json.dumps(kwargs.get('data', None)))
2543
if global_variables.DEBUG:
26-
print("Req body json parsed succesfully")
44+
print("Req body data parsed successfully")
2745
req_body = utility_function.mask_body(req_body, global_variables.moesif_options.get('REQUEST_BODY_MASKS'))
2846
req_body_transfer_encoding = 'json'
2947
except:
30-
try:
31-
if global_variables.DEBUG:
32-
print("about to parse request body as base64")
33-
req_body = base64.standard_b64encode(kwargs.get('data', None))
34-
req_body_transfer_encoding = 'base64'
35-
if global_variables.DEBUG:
36-
print("base64 encoded body: " + req_body)
37-
except:
38-
if global_variables.DEBUG:
39-
print("Request body is of type other than json or base64")
40-
req_body = None
41-
req_body_transfer_encoding = None
48+
req_body, req_body_transfer_encoding = self.base64_encode(json.dumps(kwargs.get('data', None)))
49+
elif kwargs.get('json', None):
50+
try:
51+
if global_variables.DEBUG:
52+
print('about to parse request json')
53+
req_body = json.loads(kwargs.get('json', None))
54+
if global_variables.DEBUG:
55+
print("Req body json parsed successfully")
56+
req_body = utility_function.mask_body(req_body, global_variables.moesif_options.get('REQUEST_BODY_MASKS'))
57+
req_body_transfer_encoding = 'json'
58+
except:
59+
req_body, req_body_transfer_encoding = self.base64_encode(kwargs.get('json', None))
4260
else:
4361
req_body = None
4462
req_body_transfer_encoding = None

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
requests==2.9.1
2-
moesifapi==1.2.1
2+
moesifapi==1.2.2

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# Versions should comply with PEP440. For a discussion on single-sourcing
2929
# the version across setup.py and the project code, see
3030
# https://packaging.python.org/en/latest/single_source_version.html
31-
version='0.1.0',
31+
version='0.1.1',
3232

3333
description='Moesif Python request',
3434
long_description=long_description,

0 commit comments

Comments
 (0)