@@ -29,38 +29,68 @@ async def send_multi_page_report(self, multi_page_report: MultiPageReportData) -
2929 logger .info ("No Bruni token provided, skipping multi-page report submission" )
3030 return None
3131
32- logger .info (f"Sending multi-page report to Bruni API with { len (multi_page_report ['reports' ])} pages..." )
32+ # Split the reports into smaller chunks if necessary
33+ max_chunk_size = 1 # 1 report per chunk to avoid large payloads
34+ reports = multi_page_report ['reports' ]
35+ chunks = [reports [i :i + max_chunk_size ] for i in range (0 , len (reports ), max_chunk_size )]
3336
34- logger .debug (f"Multi-page report JSON: { multi_page_report } " )
37+ all_responses = []
38+ test_id = None # Will be set after first chunk
3539
3640 async with aiohttp .ClientSession () as session :
37- try :
38- async with session .post (
39- self .api_url ,
40- json = multi_page_report ,
41- headers = {
42- "Content-Type" : "application/json" ,
43- "Authorization" : f"Bearer { self .token } "
44- }
45- ) as response :
46- if not response .ok :
47- response_text = await response .text ()
48- logger .error (f"API Error: { response .status } - { response_text } " )
49- raise ValueError (
50- f"Failed to send multi-page report: { response .status } - { response_text } "
51- )
52-
53- # Try to parse the response as JSON first
54- try :
55- response_data = await response .json ()
56- logger .info (f"API Response ({ response .status } ): { response_data } " )
57- return response_data
58- except :
59- # If response is not JSON, fall back to text
60- response_text = await response .text ()
61- logger .info (f"API Response ({ response .status } ): { response_text } " )
62- return {"message" : response_text }
63-
64- except aiohttp .ClientError as e :
65- logger .error (f"Error sending multi-page report to Bruni API: { e } " )
66- raise
41+ for chunk_index , chunk in enumerate (chunks ):
42+ logger .info (f"Sending chunk { chunk_index + 1 } /{ len (chunks )} with { len (chunk )} pages to Bruni API..." )
43+
44+ # Prepare payload
45+ payload = {
46+ "reports" : chunk ,
47+ "test_data" : multi_page_report ['test_data' ],
48+ "chunk_index" : chunk_index ,
49+ "total_chunks" : len (chunks )
50+ }
51+
52+ # Add test_id for subsequent chunks
53+ if test_id is not None :
54+ payload ["test_id" ] = test_id
55+
56+ try :
57+ async with session .post (
58+ self .api_url ,
59+ json = payload ,
60+ headers = {
61+ "Content-Type" : "application/json" ,
62+ "Authorization" : f"Bearer { self .token } "
63+ }
64+ ) as response :
65+ if not response .ok :
66+ response_text = await response .text ()
67+ logger .error (f"API Error: { response .status } - { response_text } " )
68+ raise ValueError (
69+ f"Failed to send multi-page report: { response .status } - { response_text } "
70+ )
71+
72+ # Try to parse the response as JSON first
73+ try :
74+ response_data = await response .json ()
75+ logger .info (f"API Response ({ response .status } ): { response_data } " )
76+ all_responses .append (response_data )
77+
78+ # Extract test_id from first chunk response
79+ if chunk_index == 0 and test_id is None :
80+ test_obj = response_data .get ('test' )
81+ if test_obj and isinstance (test_obj , dict ):
82+ test_id = test_obj .get ('id' )
83+ if test_id :
84+ logger .info (f"Extracted test_id: { test_id } " )
85+ except :
86+ # If response is not JSON, fall back to text
87+ response_text = await response .text ()
88+ logger .info (f"API Response ({ response .status } ): { response_text } " )
89+ all_responses .append ({"message" : response_text })
90+
91+ except aiohttp .ClientError as e :
92+ logger .error (f"Error sending multi-page report to Bruni API: { e } " )
93+ raise
94+
95+ return all_responses
96+
0 commit comments