Skip to content

Commit 9a01056

Browse files
authored
add json encoding validation (#399)
1 parent 998dbc7 commit 9a01056

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

optimizely/odp/zaius_graphql_api_manager.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,21 @@ def fetch_segments(self, api_key: str, api_host: str, user_key: str,
131131
'x-api-key': str(api_key)}
132132

133133
segments_filter = self.make_subset_filter(segments_to_check)
134-
payload_dict = {
134+
query = {
135135
'query': 'query {customer(' + str(user_key) + ': "' + str(user_value) + '") '
136136
'{audiences' + segments_filter + ' {edges {node {name state}}}}}'
137137
}
138138

139+
try:
140+
payload_dict = json.dumps(query)
141+
except TypeError as err:
142+
self.logger.error(Errors.FETCH_SEGMENTS_FAILED.format(err))
143+
return None
144+
139145
try:
140146
response = requests.post(url=url,
141147
headers=request_headers,
142-
data=json.dumps(payload_dict),
148+
data=payload_dict,
143149
timeout=OdpGraphQLApiConfig.REQUEST_TIMEOUT)
144150

145151
response.raise_for_status()
@@ -166,7 +172,7 @@ def fetch_segments(self, api_key: str, api_host: str, user_key: str,
166172
return None
167173

168174
if error_class == 'InvalidIdentifierException':
169-
self.logger.error(Errors.INVALID_SEGMENT_IDENTIFIER)
175+
self.logger.warning(Errors.INVALID_SEGMENT_IDENTIFIER)
170176
return None
171177
else:
172178
self.logger.error(Errors.FETCH_SEGMENTS_FAILED.format(error_class))

tests/test_odp_zaius_graphql_api_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def test_fetch_qualified_segments__invalid_identifier(self):
122122
segments_to_check=[])
123123

124124
mock_request_post.assert_called_once()
125-
mock_logger.error.assert_called_once_with('Audience segments fetch failed (invalid identifier).')
125+
mock_logger.warning.assert_called_once_with('Audience segments fetch failed (invalid identifier).')
126126

127127
def test_fetch_qualified_segments__other_exception(self):
128128
with mock.patch('requests.post') as mock_request_post, \

0 commit comments

Comments
 (0)