mirror of
https://github.com/foss42/apidash.git
synced 2025-12-03 03:17:00 +08:00
Added SSE ability to HTTPS method (fusion)
This commit is contained in:
@@ -53,6 +53,7 @@ class HttpResponseModel with _$HttpResponseModel {
|
||||
String? formattedBody,
|
||||
@Uint8ListConverter() Uint8List? bodyBytes,
|
||||
@DurationConverter() Duration? time,
|
||||
List<String>? sseOutput,
|
||||
}) = _HttpResponseModel;
|
||||
|
||||
factory HttpResponseModel.fromJson(Map<String, Object?> json) =>
|
||||
|
||||
@@ -30,6 +30,7 @@ mixin _$HttpResponseModel {
|
||||
Uint8List? get bodyBytes => throw _privateConstructorUsedError;
|
||||
@DurationConverter()
|
||||
Duration? get time => throw _privateConstructorUsedError;
|
||||
List<String>? get sseOutput => throw _privateConstructorUsedError;
|
||||
|
||||
/// Serializes this HttpResponseModel to a JSON map.
|
||||
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
|
||||
@@ -56,6 +57,7 @@ abstract class $HttpResponseModelCopyWith<$Res> {
|
||||
String? formattedBody,
|
||||
@Uint8ListConverter() Uint8List? bodyBytes,
|
||||
@DurationConverter() Duration? time,
|
||||
List<String>? sseOutput,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -81,6 +83,7 @@ class _$HttpResponseModelCopyWithImpl<$Res, $Val extends HttpResponseModel>
|
||||
Object? formattedBody = freezed,
|
||||
Object? bodyBytes = freezed,
|
||||
Object? time = freezed,
|
||||
Object? sseOutput = freezed,
|
||||
}) {
|
||||
return _then(
|
||||
_value.copyWith(
|
||||
@@ -112,6 +115,10 @@ class _$HttpResponseModelCopyWithImpl<$Res, $Val extends HttpResponseModel>
|
||||
? _value.time
|
||||
: time // ignore: cast_nullable_to_non_nullable
|
||||
as Duration?,
|
||||
sseOutput: freezed == sseOutput
|
||||
? _value.sseOutput
|
||||
: sseOutput // ignore: cast_nullable_to_non_nullable
|
||||
as List<String>?,
|
||||
)
|
||||
as $Val,
|
||||
);
|
||||
@@ -135,6 +142,7 @@ abstract class _$$HttpResponseModelImplCopyWith<$Res>
|
||||
String? formattedBody,
|
||||
@Uint8ListConverter() Uint8List? bodyBytes,
|
||||
@DurationConverter() Duration? time,
|
||||
List<String>? sseOutput,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -159,6 +167,7 @@ class __$$HttpResponseModelImplCopyWithImpl<$Res>
|
||||
Object? formattedBody = freezed,
|
||||
Object? bodyBytes = freezed,
|
||||
Object? time = freezed,
|
||||
Object? sseOutput = freezed,
|
||||
}) {
|
||||
return _then(
|
||||
_$HttpResponseModelImpl(
|
||||
@@ -190,6 +199,10 @@ class __$$HttpResponseModelImplCopyWithImpl<$Res>
|
||||
? _value.time
|
||||
: time // ignore: cast_nullable_to_non_nullable
|
||||
as Duration?,
|
||||
sseOutput: freezed == sseOutput
|
||||
? _value._sseOutput
|
||||
: sseOutput // ignore: cast_nullable_to_non_nullable
|
||||
as List<String>?,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -207,8 +220,10 @@ class _$HttpResponseModelImpl extends _HttpResponseModel {
|
||||
this.formattedBody,
|
||||
@Uint8ListConverter() this.bodyBytes,
|
||||
@DurationConverter() this.time,
|
||||
final List<String>? sseOutput,
|
||||
}) : _headers = headers,
|
||||
_requestHeaders = requestHeaders,
|
||||
_sseOutput = sseOutput,
|
||||
super._();
|
||||
|
||||
factory _$HttpResponseModelImpl.fromJson(Map<String, dynamic> json) =>
|
||||
@@ -246,10 +261,19 @@ class _$HttpResponseModelImpl extends _HttpResponseModel {
|
||||
@override
|
||||
@DurationConverter()
|
||||
final Duration? time;
|
||||
final List<String>? _sseOutput;
|
||||
@override
|
||||
List<String>? get sseOutput {
|
||||
final value = _sseOutput;
|
||||
if (value == null) return null;
|
||||
if (_sseOutput is EqualUnmodifiableListView) return _sseOutput;
|
||||
// ignore: implicit_dynamic_type
|
||||
return EqualUnmodifiableListView(value);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'HttpResponseModel(statusCode: $statusCode, headers: $headers, requestHeaders: $requestHeaders, body: $body, formattedBody: $formattedBody, bodyBytes: $bodyBytes, time: $time)';
|
||||
return 'HttpResponseModel(statusCode: $statusCode, headers: $headers, requestHeaders: $requestHeaders, body: $body, formattedBody: $formattedBody, bodyBytes: $bodyBytes, time: $time, sseOutput: $sseOutput)';
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -268,7 +292,11 @@ class _$HttpResponseModelImpl extends _HttpResponseModel {
|
||||
(identical(other.formattedBody, formattedBody) ||
|
||||
other.formattedBody == formattedBody) &&
|
||||
const DeepCollectionEquality().equals(other.bodyBytes, bodyBytes) &&
|
||||
(identical(other.time, time) || other.time == time));
|
||||
(identical(other.time, time) || other.time == time) &&
|
||||
const DeepCollectionEquality().equals(
|
||||
other._sseOutput,
|
||||
_sseOutput,
|
||||
));
|
||||
}
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@@ -282,6 +310,7 @@ class _$HttpResponseModelImpl extends _HttpResponseModel {
|
||||
formattedBody,
|
||||
const DeepCollectionEquality().hash(bodyBytes),
|
||||
time,
|
||||
const DeepCollectionEquality().hash(_sseOutput),
|
||||
);
|
||||
|
||||
/// Create a copy of HttpResponseModel
|
||||
@@ -310,6 +339,7 @@ abstract class _HttpResponseModel extends HttpResponseModel {
|
||||
final String? formattedBody,
|
||||
@Uint8ListConverter() final Uint8List? bodyBytes,
|
||||
@DurationConverter() final Duration? time,
|
||||
final List<String>? sseOutput,
|
||||
}) = _$HttpResponseModelImpl;
|
||||
const _HttpResponseModel._() : super._();
|
||||
|
||||
@@ -332,6 +362,8 @@ abstract class _HttpResponseModel extends HttpResponseModel {
|
||||
@override
|
||||
@DurationConverter()
|
||||
Duration? get time;
|
||||
@override
|
||||
List<String>? get sseOutput;
|
||||
|
||||
/// Create a copy of HttpResponseModel
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
|
||||
@@ -21,6 +21,9 @@ _$HttpResponseModelImpl _$$HttpResponseModelImplFromJson(Map json) =>
|
||||
json['bodyBytes'] as List<int>?,
|
||||
),
|
||||
time: const DurationConverter().fromJson((json['time'] as num?)?.toInt()),
|
||||
sseOutput: (json['sseOutput'] as List<dynamic>?)
|
||||
?.map((e) => e as String)
|
||||
.toList(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$$HttpResponseModelImplToJson(
|
||||
@@ -33,4 +36,5 @@ Map<String, dynamic> _$$HttpResponseModelImplToJson(
|
||||
'formattedBody': instance.formattedBody,
|
||||
'bodyBytes': const Uint8ListConverter().toJson(instance.bodyBytes),
|
||||
'time': const DurationConverter().toJson(instance.time),
|
||||
'sseOutput': instance.sseOutput,
|
||||
};
|
||||
|
||||
@@ -165,14 +165,18 @@ http.Request prepareHttpRequest({
|
||||
return request;
|
||||
}
|
||||
|
||||
Future<Stream<(String?, Duration?, String?)?>> streamHttpRequest(
|
||||
Future<Stream<(String? cT, HttpResponse? resp, Duration? dur, String? err)?>>
|
||||
streamHttpRequest(
|
||||
String requestId,
|
||||
APIType apiType,
|
||||
HttpRequestModel requestModel, {
|
||||
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
|
||||
bool noSSL = false,
|
||||
}) async {
|
||||
final controller = StreamController<(String?, Duration?, String?)?>();
|
||||
final controller =
|
||||
StreamController<
|
||||
(String? cT, HttpResponse? resp, Duration? dur, String? err)?
|
||||
>();
|
||||
StreamSubscription<String?>? subscription;
|
||||
final stopwatch = Stopwatch()..start();
|
||||
|
||||
@@ -186,10 +190,10 @@ Future<Stream<(String?, Duration?, String?)?>> streamHttpRequest(
|
||||
Future<void> handleError(dynamic error) async {
|
||||
await Future.microtask(() {});
|
||||
if (httpClientManager.wasRequestCancelled(requestId)) {
|
||||
controller.add((null, null, kMsgRequestCancelled));
|
||||
controller.add((null, null, null, kMsgRequestCancelled));
|
||||
httpClientManager.removeCancelledRequest(requestId);
|
||||
} else {
|
||||
controller.add((null, null, error.toString()));
|
||||
controller.add((null, null, null, error.toString()));
|
||||
}
|
||||
await cleanup();
|
||||
}
|
||||
@@ -200,7 +204,7 @@ Future<Stream<(String?, Duration?, String?)?>> streamHttpRequest(
|
||||
};
|
||||
|
||||
if (httpClientManager.wasRequestCancelled(requestId)) {
|
||||
controller.add((null, null, kMsgRequestCancelled));
|
||||
controller.add((null, null, null, kMsgRequestCancelled));
|
||||
httpClientManager.removeCancelledRequest(requestId);
|
||||
controller.close();
|
||||
return controller.stream;
|
||||
@@ -243,8 +247,25 @@ Future<Stream<(String?, Duration?, String?)?>> streamHttpRequest(
|
||||
final streamedResponse = await client.send(multipart);
|
||||
final stream = streamTextResponse(streamedResponse);
|
||||
|
||||
print(streamedResponse.headers['content-type']);
|
||||
|
||||
subscription = stream.listen(
|
||||
(data) => controller.add((data, stopwatch.elapsed, null)),
|
||||
(data) => controller.add((
|
||||
streamedResponse.headers['content-type'].toString(),
|
||||
data == null
|
||||
? null
|
||||
: HttpResponse.bytes(
|
||||
utf8.encode(data),
|
||||
streamedResponse.statusCode,
|
||||
request: streamedResponse.request,
|
||||
headers: streamedResponse.headers,
|
||||
isRedirect: streamedResponse.isRedirect,
|
||||
persistentConnection: streamedResponse.persistentConnection,
|
||||
reasonPhrase: streamedResponse.reasonPhrase,
|
||||
),
|
||||
stopwatch.elapsed,
|
||||
null,
|
||||
)),
|
||||
onDone: () => cleanup(),
|
||||
onError: handleError,
|
||||
);
|
||||
@@ -279,7 +300,22 @@ Future<Stream<(String?, Duration?, String?)?>> streamHttpRequest(
|
||||
subscription = stream.listen(
|
||||
(data) {
|
||||
if (!controller.isClosed) {
|
||||
controller.add((data, stopwatch.elapsed, null));
|
||||
controller.add((
|
||||
streamedResponse.headers['content-type'].toString(),
|
||||
data == null
|
||||
? null
|
||||
: HttpResponse.bytes(
|
||||
utf8.encode(data),
|
||||
streamedResponse.statusCode,
|
||||
request: streamedResponse.request,
|
||||
headers: streamedResponse.headers,
|
||||
isRedirect: streamedResponse.isRedirect,
|
||||
persistentConnection: streamedResponse.persistentConnection,
|
||||
reasonPhrase: streamedResponse.reasonPhrase,
|
||||
),
|
||||
stopwatch.elapsed,
|
||||
null,
|
||||
));
|
||||
}
|
||||
},
|
||||
onDone: () => cleanup(),
|
||||
|
||||
Reference in New Issue
Block a user