772. Create questions with various configuration options
883. List existing questions in your account
994. Use questions for security monitoring and compliance
10+
11+ NOTE: This example includes robust error handling for the compliance field
12+ to handle potential GraphQL schema mismatches where compliance data might
13+ be returned as a list instead of a dictionary.
1014"""
1115
1216import os
@@ -150,6 +154,10 @@ def advanced_question_examples(j1):
150154
151155 # 1. Question with compliance metadata
152156 print ("1. Creating a question with compliance metadata:" )
157+ # Note: The compliance field access has been made robust to handle potential
158+ # GraphQL schema mismatches where compliance data might be returned as a list
159+ # instead of a dictionary. This was causing the original error:
160+ # "'list' object has no attribute 'get'"
153161 try :
154162 compliance_mapped_question = j1 .create_question (
155163 title = "CIS AWS Foundations Benchmark 2.3" ,
@@ -170,11 +178,47 @@ def advanced_question_examples(j1):
170178 )
171179
172180 print (f"Created compliance-mapped question: { compliance_mapped_question ['title' ]} " )
173- if 'compliance' in compliance_mapped_question :
174- print (f"Compliance standard: { compliance_mapped_question ['compliance' ].get ('standard' )} " )
181+
182+ # Debug: Show the entire response structure
183+ print (f"Full question response keys: { list (compliance_mapped_question .keys ())} " )
184+ print (f"Question ID: { compliance_mapped_question .get ('id' , 'No ID' )} " )
185+
186+ try :
187+ if 'compliance' in compliance_mapped_question :
188+ compliance_data = compliance_mapped_question ['compliance' ]
189+ # Debug: Show the actual structure
190+ print (f"Compliance data structure: { type (compliance_data )} " )
191+ print (f"Compliance data content: { compliance_data } " )
192+
193+ # Handle both list and dictionary responses for compliance
194+ if isinstance (compliance_data , dict ):
195+ print (f"Compliance standard: { compliance_data .get ('standard' , 'Not specified' )} " )
196+ if 'requirements' in compliance_data :
197+ reqs = compliance_data ['requirements' ]
198+ if isinstance (reqs , list ):
199+ print (f"Compliance requirements: { ', ' .join (map (str , reqs ))} " )
200+ else :
201+ print (f"Compliance requirements (unexpected type): { type (reqs )} - { reqs } " )
202+ elif isinstance (compliance_data , list ):
203+ print (f"Compliance data returned as list with { len (compliance_data )} items" )
204+ # If it's a list, try to access the first item if it exists
205+ if compliance_data and isinstance (compliance_data [0 ], dict ):
206+ print (f"First compliance item: { compliance_data [0 ]} " )
207+ else :
208+ print (f"Compliance data type: { type (compliance_data )} " )
209+ else :
210+ print ("No compliance field found in response" )
211+ except Exception as compliance_error :
212+ print (f"Error accessing compliance data: { compliance_error } " )
213+ print (f"Compliance field type: { type (compliance_mapped_question .get ('compliance' , 'Not present' ))} " )
214+ # Show more debugging info
215+ print (f"Full response for debugging: { compliance_mapped_question } " )
175216 print ()
176217 except Exception as e :
177- print (f"Error creating compliance-mapped question: { e } \n " )
218+ print (f"Error creating compliance-mapped question: { e } " )
219+ print (f"Error type: { type (e ).__name__ } " )
220+ print (f"Error details: { str (e )} " )
221+ print ()
178222
179223 # 2. Question with variables (parameterized queries)
180224 print ("2. Creating a parameterized question with variables:" )
0 commit comments