Added SSE ability to HTTPS method (fusion)

This commit is contained in:
Manas Hejmadi
2025-06-25 20:50:27 +05:30
parent e5b22a24fc
commit 97db38a42d
7 changed files with 202 additions and 16 deletions

View File

@@ -1,3 +1,5 @@
import 'dart:async';
import 'package:apidash_core/apidash_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
@@ -311,7 +313,14 @@ class CollectionStateNotifier
state = map;
bool noSSL = ref.read(settingsProvider).isSSLDisabled;
var responseRec = await sendHttpRequest(
(Response?, Duration?, String?) responseRec;
HttpResponseModel? respModel;
HistoryRequestModel? historyM;
RequestModel? newRequestModel;
responseRec = (null, null, null);
final stream = await streamHttpRequest(
requestId,
apiType,
substitutedHttpRequestModel,
@@ -319,7 +328,78 @@ class CollectionStateNotifier
noSSL: noSSL,
);
late RequestModel newRequestModel;
StreamSubscription? sub;
final completer = Completer();
bool isTextStream = false;
sub = stream.listen((d) async {
if (d == null) return;
isTextStream = ((d.$1 == null && isTextStream) ||
(d.$1 == 'text/event-stream' || d.$1 == 'application/x-ndjson'));
responseRec = (d.$2, d.$3, d.$4);
if (!isTextStream) {
if (completer.isCompleted) return;
completer.complete(responseRec);
await Future.delayed(Duration(milliseconds: 100));
}
if (responseRec.$1 != null) {
responseRec = (
HttpResponse(
responseRec.$1!.body,
responseRec.$1!.statusCode,
request: responseRec.$1!.request,
headers: {
...(responseRec.$1?.headers ?? {}),
'content-type': 'text/event-stream'
},
isRedirect: responseRec.$1!.isRedirect,
reasonPhrase: responseRec.$1!.reasonPhrase,
persistentConnection: responseRec.$1!.persistentConnection,
),
responseRec.$2,
responseRec.$3,
);
}
//----------- MAKE CHANGES --------------
respModel = respModel?.copyWith(sseOutput: [
...(respModel?.sseOutput ?? []),
responseRec.$1!.body,
]);
if (respModel != null) {
final nRM = newRequestModel!.copyWith(
httpResponseModel: respModel,
);
map = {...state!};
map[requestId] = nRM;
state = map;
unsave();
}
//Changing History
if (historyM != null && respModel != null) {
historyM = historyM!.copyWith(
httpResponseModel: respModel!,
);
ref
.read(historyMetaStateNotifier.notifier)
.editHistoryRequest(historyM!);
}
//----------- MAKE CHANGES --------------
if (completer.isCompleted) return;
completer.complete(responseRec);
}, onDone: () {
sub?.cancel();
}, onError: (e) {
print('err: $e');
});
responseRec = await completer.future;
if (responseRec.$1 == null) {
newRequestModel = requestModel.copyWith(
responseStatus: -1,
@@ -327,7 +407,7 @@ class CollectionStateNotifier
isWorking: false,
);
} else {
final httpResponseModel = baseHttpResponseModel.fromResponse(
respModel = baseHttpResponseModel.fromResponse(
response: responseRec.$1!,
time: responseRec.$2!,
);
@@ -335,11 +415,11 @@ class CollectionStateNotifier
newRequestModel = requestModel.copyWith(
responseStatus: statusCode,
message: kResponseCodeReasons[statusCode],
httpResponseModel: httpResponseModel,
httpResponseModel: respModel,
isWorking: false,
);
String newHistoryId = getNewUuid();
HistoryRequestModel model = HistoryRequestModel(
historyM = HistoryRequestModel(
historyId: newHistoryId,
metaData: HistoryMetaModel(
historyId: newHistoryId,
@@ -352,7 +432,7 @@ class CollectionStateNotifier
timeStamp: DateTime.now(),
),
httpRequestModel: substitutedHttpRequestModel,
httpResponseModel: httpResponseModel,
httpResponseModel: respModel!,
preRequestScript: requestModel.preRequestScript,
postRequestScript: requestModel.postRequestScript,
);
@@ -372,7 +452,7 @@ class CollectionStateNotifier
},
);
}
ref.read(historyMetaStateNotifier.notifier).addHistoryRequest(model);
ref.read(historyMetaStateNotifier.notifier).addHistoryRequest(historyM!);
}
// update state with response data

View File

@@ -90,6 +90,21 @@ class HistoryMetaStateNotifier
await loadHistoryRequest(id);
}
void editHistoryRequest(HistoryRequestModel model) async {
final id = model.historyId;
state = {
...state ?? {},
id: model.metaData,
};
final existingKeys = state?.keys.toList() ?? [];
if (!existingKeys.contains(id)) {
hiveHandler.setHistoryIds([...existingKeys, id]);
}
hiveHandler.setHistoryMeta(id, model.metaData.toJson());
await hiveHandler.setHistoryRequest(id, model.toJson());
await loadHistoryRequest(id);
}
Future<void> clearAllHistory() async {
await hiveHandler.clearAllHistory();
ref.read(selectedHistoryIdStateProvider.notifier).state = null;

View File

@@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:apidash_core/apidash_core.dart';
import 'package:apidash/models/models.dart';
@@ -54,6 +56,22 @@ class ResponseBody extends StatelessWidget {
options.remove(ResponseBodyView.code);
}
// print('reM -> ${responseModel.sseOutput}');
if (responseModel.sseOutput?.isNotEmpty ?? false) {
final modifiedBody = responseModel.sseOutput!.join('\n\n');
print(modifiedBody);
return ResponseBodySuccess(
key: Key("${selectedRequestModel!.id}-response"),
mediaType: mediaType,
options: options,
bytes: utf8.encode(modifiedBody),
body: modifiedBody,
formattedBody: modifiedBody,
highlightLanguage: highlightLanguage,
);
}
return ResponseBodySuccess(
key: Key("${selectedRequestModel!.id}-response"),
mediaType: mediaType,