mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 10:49:49 +08:00
SSE: SSEDisplay Implemented along with SSE ResponseBodyView Type
This commit is contained in:
@@ -59,16 +59,13 @@ class ResponseBody extends StatelessWidget {
|
||||
// print('reM -> ${responseModel.sseOutput}');
|
||||
|
||||
if (responseModel.sseOutput?.isNotEmpty ?? false) {
|
||||
final modifiedBody = responseModel.sseOutput!.join('\n\n');
|
||||
print(modifiedBody);
|
||||
// final modifiedBody = responseModel.sseOutput!.join('\n\n');
|
||||
return ResponseBodySuccess(
|
||||
key: Key("${selectedRequestModel!.id}-response"),
|
||||
mediaType: mediaType,
|
||||
options: options,
|
||||
bytes: utf8.encode(modifiedBody),
|
||||
body: modifiedBody,
|
||||
formattedBody: modifiedBody,
|
||||
highlightLanguage: highlightLanguage,
|
||||
mediaType: MediaType('text', 'event-stream'),
|
||||
options: [ResponseBodyView.sse],
|
||||
bytes: utf8.encode((responseModel.sseOutput!).toString()),
|
||||
body: jsonEncode(responseModel.sseOutput!),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:apidash_core/apidash_core.dart';
|
||||
import 'package:apidash_design_system/apidash_design_system.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
@@ -144,6 +146,17 @@ class _ResponseBodySuccessState extends State<ResponseBodySuccess> {
|
||||
),
|
||||
),
|
||||
),
|
||||
ResponseBodyView.sse => Expanded(
|
||||
child: Container(
|
||||
width: double.maxFinite,
|
||||
padding: kP8,
|
||||
decoration: textContainerdecoration,
|
||||
child: SingleChildScrollView(
|
||||
child: SSEDisplay(
|
||||
sseOutput: widget.body,
|
||||
)),
|
||||
),
|
||||
),
|
||||
}
|
||||
],
|
||||
),
|
||||
@@ -152,3 +165,78 @@ class _ResponseBodySuccessState extends State<ResponseBodySuccess> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//MOVE THIS SOMEWHERE ELSE
|
||||
class SSEDisplay extends StatelessWidget {
|
||||
final String sseOutput;
|
||||
const SSEDisplay({super.key, required this.sseOutput});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
List<dynamic> sse;
|
||||
try {
|
||||
sse = jsonDecode(sseOutput);
|
||||
} catch (e) {
|
||||
return Text(
|
||||
'Invalid SSE output',
|
||||
style: theme.textTheme.bodyMedium?.copyWith(color: Colors.red),
|
||||
);
|
||||
}
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: sse.map<Widget>((chunk) {
|
||||
Map<String, dynamic>? parsedJson;
|
||||
try {
|
||||
parsedJson = jsonDecode(chunk);
|
||||
} catch (_) {
|
||||
// Not a JSON object
|
||||
}
|
||||
|
||||
return Card(
|
||||
margin: const EdgeInsets.symmetric(vertical: 6, horizontal: 8),
|
||||
elevation: 2,
|
||||
shape:
|
||||
RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: parsedJson != null
|
||||
? Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: parsedJson.entries.map((entry) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 2.0),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'${entry.key}: ',
|
||||
style: theme.textTheme.bodyMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: kColorGQL),
|
||||
),
|
||||
SizedBox(width: 4),
|
||||
Expanded(
|
||||
child: Text(
|
||||
entry.value.toString(),
|
||||
style: theme.textTheme.bodyMedium
|
||||
?.copyWith(fontFamily: 'monospace'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
)
|
||||
: Text(
|
||||
chunk.toString(),
|
||||
style: theme.textTheme.bodyMedium
|
||||
?.copyWith(fontFamily: 'monospace'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user