mirror of
https://github.com/foss42/apidash.git
synced 2025-08-19 01:41:08 +08:00
Refactor auth handling in HTTP request functions
Removed redundant AuthModel parameter from sendHttpRequest and streamHttpRequest functions, now using authModel from HttpRequestModel directly. Updated all usages, tests, and related logic to reflect this change. Improved streaming response handling in CollectionStateNotifier and HttpResponseModel.
This commit is contained in:
@ -321,7 +321,6 @@ class CollectionStateNotifier
|
|||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest(
|
||||||
requestId,
|
requestId,
|
||||||
apiType,
|
apiType,
|
||||||
requestModel.httpRequestModel?.authModel,
|
|
||||||
substitutedHttpRequestModel,
|
substitutedHttpRequestModel,
|
||||||
defaultUriScheme: defaultUriScheme,
|
defaultUriScheme: defaultUriScheme,
|
||||||
noSSL: noSSL,
|
noSSL: noSSL,
|
||||||
@ -330,26 +329,20 @@ class CollectionStateNotifier
|
|||||||
HttpResponseModel? httpResponseModel;
|
HttpResponseModel? httpResponseModel;
|
||||||
HistoryRequestModel? historyModel;
|
HistoryRequestModel? historyModel;
|
||||||
RequestModel newRequestModel = requestModel;
|
RequestModel newRequestModel = requestModel;
|
||||||
bool? isTextStream;
|
bool isStreamingResponse = false;
|
||||||
final completer = Completer<(Response?, Duration?, String?)>();
|
final completer = Completer<(Response?, Duration?, String?)>();
|
||||||
|
|
||||||
StreamSubscription? sub;
|
StreamSubscription? sub;
|
||||||
|
|
||||||
sub = stream.listen((d) async {
|
sub = stream.listen((rec) async {
|
||||||
if (d == null) return;
|
if (rec == null) return;
|
||||||
|
|
||||||
isTextStream = d.$1;
|
isStreamingResponse = rec.$1 ?? false;
|
||||||
final response = d.$2;
|
final response = rec.$2;
|
||||||
final duration = d.$3;
|
final duration = rec.$3;
|
||||||
final errorMessage = d.$4;
|
final errorMessage = rec.$4;
|
||||||
|
|
||||||
if (isTextStream == false) {
|
|
||||||
if (!completer.isCompleted) {
|
|
||||||
completer.complete((response, duration, errorMessage));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (isStreamingResponse) {
|
||||||
httpResponseModel = httpResponseModel?.copyWith(
|
httpResponseModel = httpResponseModel?.copyWith(
|
||||||
time: duration,
|
time: duration,
|
||||||
sseOutput: [
|
sseOutput: [
|
||||||
@ -375,6 +368,7 @@ class CollectionStateNotifier
|
|||||||
.read(historyMetaStateNotifier.notifier)
|
.read(historyMetaStateNotifier.notifier)
|
||||||
.editHistoryRequest(historyModel!);
|
.editHistoryRequest(historyModel!);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!completer.isCompleted) {
|
if (!completer.isCompleted) {
|
||||||
completer.complete((response, duration, errorMessage));
|
completer.complete((response, duration, errorMessage));
|
||||||
@ -403,14 +397,10 @@ class CollectionStateNotifier
|
|||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
final statusCode = response.statusCode;
|
final statusCode = response.statusCode;
|
||||||
|
httpResponseModel = baseHttpResponseModel.fromResponse(
|
||||||
httpResponseModel = baseHttpResponseModel
|
|
||||||
.fromResponse(
|
|
||||||
response: response,
|
response: response,
|
||||||
time: duration,
|
time: duration,
|
||||||
)
|
isStreamingResponse: isStreamingResponse,
|
||||||
.copyWith(
|
|
||||||
sseOutput: (isTextStream == true) ? [response.body] : [],
|
|
||||||
);
|
);
|
||||||
|
|
||||||
newRequestModel = newRequestModel.copyWith(
|
newRequestModel = newRequestModel.copyWith(
|
||||||
|
@ -54,10 +54,10 @@ class _BetterNetworkingExampleState extends State<BetterNetworkingExample> {
|
|||||||
final (resp, duration, err) = await sendHttpRequest(
|
final (resp, duration, err) = await sendHttpRequest(
|
||||||
'G1',
|
'G1',
|
||||||
APIType.rest,
|
APIType.rest,
|
||||||
AuthModel(type: APIAuthType.none),
|
|
||||||
HttpRequestModel(
|
HttpRequestModel(
|
||||||
url: 'https://reqres.in/api/users/2',
|
url: 'https://reqres.in/api/users/2',
|
||||||
method: HTTPVerb.get,
|
method: HTTPVerb.get,
|
||||||
|
authModel: AuthModel(type: APIAuthType.none),
|
||||||
headers: [
|
headers: [
|
||||||
NameValueModel(
|
NameValueModel(
|
||||||
name: 'x-api-key',
|
name: 'x-api-key',
|
||||||
@ -81,10 +81,10 @@ class _BetterNetworkingExampleState extends State<BetterNetworkingExample> {
|
|||||||
final (resp, duration, err) = await sendHttpRequest(
|
final (resp, duration, err) = await sendHttpRequest(
|
||||||
'P1',
|
'P1',
|
||||||
APIType.rest,
|
APIType.rest,
|
||||||
AuthModel(type: APIAuthType.none),
|
|
||||||
HttpRequestModel(
|
HttpRequestModel(
|
||||||
url: 'https://reqres.in/api/users',
|
url: 'https://reqres.in/api/users',
|
||||||
method: HTTPVerb.post,
|
method: HTTPVerb.post,
|
||||||
|
authModel: AuthModel(type: APIAuthType.none),
|
||||||
headers: [
|
headers: [
|
||||||
NameValueModel(
|
NameValueModel(
|
||||||
name: 'x-api-key',
|
name: 'x-api-key',
|
||||||
@ -109,10 +109,10 @@ class _BetterNetworkingExampleState extends State<BetterNetworkingExample> {
|
|||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest(
|
||||||
'S1',
|
'S1',
|
||||||
APIType.rest,
|
APIType.rest,
|
||||||
null,
|
|
||||||
HttpRequestModel(
|
HttpRequestModel(
|
||||||
method: HTTPVerb.post,
|
method: HTTPVerb.post,
|
||||||
url: 'http://localhost:11434/v1/chat/completions',
|
url: 'http://localhost:11434/v1/chat/completions',
|
||||||
|
authModel: null,
|
||||||
body: jsonEncode({
|
body: jsonEncode({
|
||||||
'model': 'gemma3:latest',
|
'model': 'gemma3:latest',
|
||||||
'stream': true,
|
'stream': true,
|
||||||
|
@ -62,7 +62,11 @@ class HttpResponseModel with _$HttpResponseModel {
|
|||||||
String? get contentType => headers?.getValueContentType();
|
String? get contentType => headers?.getValueContentType();
|
||||||
MediaType? get mediaType => getMediaTypeFromHeaders(headers);
|
MediaType? get mediaType => getMediaTypeFromHeaders(headers);
|
||||||
|
|
||||||
HttpResponseModel fromResponse({required Response response, Duration? time}) {
|
HttpResponseModel fromResponse({
|
||||||
|
required Response response,
|
||||||
|
Duration? time,
|
||||||
|
bool isStreamingResponse = false,
|
||||||
|
}) {
|
||||||
final responseHeaders = mergeMaps({
|
final responseHeaders = mergeMaps({
|
||||||
HttpHeaders.contentLengthHeader: response.contentLength.toString(),
|
HttpHeaders.contentLengthHeader: response.contentLength.toString(),
|
||||||
}, response.headers);
|
}, response.headers);
|
||||||
@ -80,6 +84,7 @@ class HttpResponseModel with _$HttpResponseModel {
|
|||||||
formattedBody: formatBody(body, mediaType),
|
formattedBody: formatBody(body, mediaType),
|
||||||
bodyBytes: response.bodyBytes,
|
bodyBytes: response.bodyBytes,
|
||||||
time: time,
|
time: time,
|
||||||
|
sseOutput: isStreamingResponse ? [body] : null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -158,7 +158,6 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequestV1(
|
|||||||
Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
|
Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
|
||||||
String requestId,
|
String requestId,
|
||||||
APIType apiType,
|
APIType apiType,
|
||||||
AuthModel? authData,
|
|
||||||
HttpRequestModel requestModel, {
|
HttpRequestModel requestModel, {
|
||||||
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
|
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
|
||||||
bool noSSL = false,
|
bool noSSL = false,
|
||||||
@ -166,7 +165,6 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
|
|||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest(
|
||||||
requestId,
|
requestId,
|
||||||
apiType,
|
apiType,
|
||||||
authData,
|
|
||||||
requestModel,
|
requestModel,
|
||||||
defaultUriScheme: defaultUriScheme,
|
defaultUriScheme: defaultUriScheme,
|
||||||
noSSL: noSSL,
|
noSSL: noSSL,
|
||||||
@ -206,11 +204,11 @@ http.Request prepareHttpRequest({
|
|||||||
Future<Stream<HttpStreamOutput>> streamHttpRequest(
|
Future<Stream<HttpStreamOutput>> streamHttpRequest(
|
||||||
String requestId,
|
String requestId,
|
||||||
APIType apiType,
|
APIType apiType,
|
||||||
AuthModel? authData,
|
|
||||||
HttpRequestModel httpRequestModel, {
|
HttpRequestModel httpRequestModel, {
|
||||||
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
|
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
|
||||||
bool noSSL = false,
|
bool noSSL = false,
|
||||||
}) async {
|
}) async {
|
||||||
|
final authData = httpRequestModel.authModel;
|
||||||
final controller = StreamController<HttpStreamOutput>();
|
final controller = StreamController<HttpStreamOutput>();
|
||||||
StreamSubscription<List<int>?>? subscription;
|
StreamSubscription<List<int>?>? subscription;
|
||||||
final stopwatch = Stopwatch()..start();
|
final stopwatch = Stopwatch()..start();
|
||||||
|
@ -109,7 +109,6 @@ Future<HttpRequestModel> handleAuth(
|
|||||||
final httpResult = await sendHttpRequest(
|
final httpResult = await sendHttpRequest(
|
||||||
"digest-${Random.secure()}",
|
"digest-${Random.secure()}",
|
||||||
APIType.rest,
|
APIType.rest,
|
||||||
null,
|
|
||||||
httpRequestModel,
|
httpRequestModel,
|
||||||
);
|
);
|
||||||
final httpResponse = httpResult.$1;
|
final httpResponse = httpResult.$1;
|
||||||
|
@ -24,7 +24,6 @@ void main() {
|
|||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest(
|
||||||
'graphql_test',
|
'graphql_test',
|
||||||
APIType.graphql,
|
APIType.graphql,
|
||||||
null,
|
|
||||||
model,
|
model,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -55,7 +54,6 @@ void main() {
|
|||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest(
|
||||||
'graphql_bad',
|
'graphql_bad',
|
||||||
APIType.graphql,
|
APIType.graphql,
|
||||||
null,
|
|
||||||
model,
|
model,
|
||||||
);
|
);
|
||||||
final output = await stream.first;
|
final output = await stream.first;
|
||||||
@ -78,7 +76,6 @@ void main() {
|
|||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest(
|
||||||
'graphql_test_cancellation',
|
'graphql_test_cancellation',
|
||||||
APIType.graphql,
|
APIType.graphql,
|
||||||
null,
|
|
||||||
model,
|
model,
|
||||||
);
|
);
|
||||||
httpClientManager.cancelRequest('graphql_test_cancellation');
|
httpClientManager.cancelRequest('graphql_test_cancellation');
|
||||||
|
@ -19,7 +19,6 @@ void main() {
|
|||||||
final (resp, dur, err) = await sendHttpRequest(
|
final (resp, dur, err) = await sendHttpRequest(
|
||||||
'get_test',
|
'get_test',
|
||||||
APIType.rest,
|
APIType.rest,
|
||||||
null,
|
|
||||||
model,
|
model,
|
||||||
);
|
);
|
||||||
final output = jsonDecode(resp?.body ?? '{}');
|
final output = jsonDecode(resp?.body ?? '{}');
|
||||||
@ -60,7 +59,6 @@ void main() {
|
|||||||
final (resp, dur, err) = await sendHttpRequest(
|
final (resp, dur, err) = await sendHttpRequest(
|
||||||
'mpreq',
|
'mpreq',
|
||||||
APIType.rest,
|
APIType.rest,
|
||||||
null,
|
|
||||||
model,
|
model,
|
||||||
);
|
);
|
||||||
final output = jsonDecode(resp?.body ?? '{}');
|
final output = jsonDecode(resp?.body ?? '{}');
|
||||||
@ -88,12 +86,7 @@ void main() {
|
|||||||
NameValueModel(name: 'Accept', value: 'application/json'),
|
NameValueModel(name: 'Accept', value: 'application/json'),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest('get_test', APIType.rest, model);
|
||||||
'get_test',
|
|
||||||
APIType.rest,
|
|
||||||
null,
|
|
||||||
model,
|
|
||||||
);
|
|
||||||
final output = await stream.first;
|
final output = await stream.first;
|
||||||
expect(
|
expect(
|
||||||
output?.$2?.statusCode == 200,
|
output?.$2?.statusCode == 200,
|
||||||
@ -115,12 +108,7 @@ void main() {
|
|||||||
}""",
|
}""",
|
||||||
);
|
);
|
||||||
|
|
||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest('post_test', APIType.rest, model);
|
||||||
'post_test',
|
|
||||||
APIType.rest,
|
|
||||||
null,
|
|
||||||
model,
|
|
||||||
);
|
|
||||||
final output = await stream.first;
|
final output = await stream.first;
|
||||||
|
|
||||||
expect(output?.$2?.statusCode, equals(200), reason: 'Expected 200 Ok');
|
expect(output?.$2?.statusCode, equals(200), reason: 'Expected 200 Ok');
|
||||||
@ -133,7 +121,6 @@ void main() {
|
|||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest(
|
||||||
'empty_url_test',
|
'empty_url_test',
|
||||||
APIType.rest,
|
APIType.rest,
|
||||||
null,
|
|
||||||
model,
|
model,
|
||||||
);
|
);
|
||||||
final output = await stream.first;
|
final output = await stream.first;
|
||||||
@ -149,7 +136,6 @@ void main() {
|
|||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest(
|
||||||
'invalid_url_test',
|
'invalid_url_test',
|
||||||
APIType.rest,
|
APIType.rest,
|
||||||
null,
|
|
||||||
model,
|
model,
|
||||||
);
|
);
|
||||||
final output = await stream.first;
|
final output = await stream.first;
|
||||||
@ -170,7 +156,6 @@ void main() {
|
|||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest(
|
||||||
'large_body_test',
|
'large_body_test',
|
||||||
APIType.rest,
|
APIType.rest,
|
||||||
null,
|
|
||||||
model,
|
model,
|
||||||
);
|
);
|
||||||
final output = await stream.first;
|
final output = await stream.first;
|
||||||
@ -199,12 +184,7 @@ void main() {
|
|||||||
cancelHttpRequest('get_test_c');
|
cancelHttpRequest('get_test_c');
|
||||||
});
|
});
|
||||||
debugPrint("Stream start");
|
debugPrint("Stream start");
|
||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest('get_test_c', APIType.rest, model);
|
||||||
'get_test_c',
|
|
||||||
APIType.rest,
|
|
||||||
null,
|
|
||||||
model,
|
|
||||||
);
|
|
||||||
debugPrint("Stream get output");
|
debugPrint("Stream get output");
|
||||||
final output = await stream.first;
|
final output = await stream.first;
|
||||||
final errMsg = output?.$4;
|
final errMsg = output?.$4;
|
||||||
|
@ -13,12 +13,7 @@ void main() {
|
|||||||
method: HTTPVerb.get,
|
method: HTTPVerb.get,
|
||||||
);
|
);
|
||||||
|
|
||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest('sse_test', APIType.rest, model);
|
||||||
'sse_test',
|
|
||||||
APIType.rest,
|
|
||||||
null,
|
|
||||||
model,
|
|
||||||
);
|
|
||||||
|
|
||||||
final outputs = <HttpStreamOutput?>[];
|
final outputs = <HttpStreamOutput?>[];
|
||||||
final subscription = stream.listen(outputs.add);
|
final subscription = stream.listen(outputs.add);
|
||||||
@ -44,12 +39,7 @@ void main() {
|
|||||||
method: HTTPVerb.get,
|
method: HTTPVerb.get,
|
||||||
);
|
);
|
||||||
|
|
||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest('sse_test', APIType.rest, model);
|
||||||
'sse_test',
|
|
||||||
APIType.rest,
|
|
||||||
null,
|
|
||||||
model,
|
|
||||||
);
|
|
||||||
final outputs = <HttpStreamOutput?>[];
|
final outputs = <HttpStreamOutput?>[];
|
||||||
final subscription = stream.listen(outputs.add);
|
final subscription = stream.listen(outputs.add);
|
||||||
|
|
||||||
|
@ -14,7 +14,6 @@ void main() {
|
|||||||
final result = await sendHttpRequest(
|
final result = await sendHttpRequest(
|
||||||
'test-request',
|
'test-request',
|
||||||
APIType.rest,
|
APIType.rest,
|
||||||
null,
|
|
||||||
httpRequestModel,
|
httpRequestModel,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@ void main() {
|
|||||||
var responseRec = await sendHttpRequest(
|
var responseRec = await sendHttpRequest(
|
||||||
requestModelGet1.id,
|
requestModelGet1.id,
|
||||||
requestModelGet1.apiType,
|
requestModelGet1.apiType,
|
||||||
AuthModel(type: APIAuthType.none),
|
|
||||||
requestModelGet1.httpRequestModel!,
|
requestModelGet1.httpRequestModel!,
|
||||||
defaultUriScheme: kDefaultUriScheme,
|
defaultUriScheme: kDefaultUriScheme,
|
||||||
noSSL: false,
|
noSSL: false,
|
||||||
@ -36,7 +35,6 @@ void main() {
|
|||||||
var responseRec = await sendHttpRequest(
|
var responseRec = await sendHttpRequest(
|
||||||
requestModelGet13.id,
|
requestModelGet13.id,
|
||||||
requestModelGet13.apiType,
|
requestModelGet13.apiType,
|
||||||
AuthModel(type: APIAuthType.none),
|
|
||||||
requestModelGet13.httpRequestModel!,
|
requestModelGet13.httpRequestModel!,
|
||||||
defaultUriScheme: kDefaultUriScheme,
|
defaultUriScheme: kDefaultUriScheme,
|
||||||
noSSL: false,
|
noSSL: false,
|
||||||
@ -54,7 +52,6 @@ void main() {
|
|||||||
var responseRec = await sendHttpRequest(
|
var responseRec = await sendHttpRequest(
|
||||||
requestModelPost11.id,
|
requestModelPost11.id,
|
||||||
requestModelPost11.apiType,
|
requestModelPost11.apiType,
|
||||||
AuthModel(type: APIAuthType.none),
|
|
||||||
requestModelPost11.httpRequestModel!,
|
requestModelPost11.httpRequestModel!,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -69,7 +66,6 @@ void main() {
|
|||||||
var responseRec = await sendHttpRequest(
|
var responseRec = await sendHttpRequest(
|
||||||
requestModelPost12.id,
|
requestModelPost12.id,
|
||||||
requestModelPost12.apiType,
|
requestModelPost12.apiType,
|
||||||
AuthModel(type: APIAuthType.none),
|
|
||||||
requestModelPost12.httpRequestModel!,
|
requestModelPost12.httpRequestModel!,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -83,7 +79,6 @@ void main() {
|
|||||||
var responseRec = await sendHttpRequest(
|
var responseRec = await sendHttpRequest(
|
||||||
requestModelPost13.id,
|
requestModelPost13.id,
|
||||||
requestModelPost13.apiType,
|
requestModelPost13.apiType,
|
||||||
AuthModel(type: APIAuthType.none),
|
|
||||||
requestModelPost13.httpRequestModel!,
|
requestModelPost13.httpRequestModel!,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -97,7 +92,6 @@ void main() {
|
|||||||
var responseRec = await sendHttpRequest(
|
var responseRec = await sendHttpRequest(
|
||||||
requestModelGetBadSSL.id,
|
requestModelGetBadSSL.id,
|
||||||
requestModelGetBadSSL.apiType,
|
requestModelGetBadSSL.apiType,
|
||||||
AuthModel(type: APIAuthType.none),
|
|
||||||
requestModelGetBadSSL.httpRequestModel!,
|
requestModelGetBadSSL.httpRequestModel!,
|
||||||
defaultUriScheme: kDefaultUriScheme,
|
defaultUriScheme: kDefaultUriScheme,
|
||||||
noSSL: false,
|
noSSL: false,
|
||||||
@ -110,7 +104,6 @@ void main() {
|
|||||||
var responseRec = await sendHttpRequest(
|
var responseRec = await sendHttpRequest(
|
||||||
requestModelGetBadSSL.id,
|
requestModelGetBadSSL.id,
|
||||||
requestModelGetBadSSL.apiType,
|
requestModelGetBadSSL.apiType,
|
||||||
AuthModel(type: APIAuthType.none),
|
|
||||||
requestModelGetBadSSL.httpRequestModel!,
|
requestModelGetBadSSL.httpRequestModel!,
|
||||||
defaultUriScheme: kDefaultUriScheme,
|
defaultUriScheme: kDefaultUriScheme,
|
||||||
noSSL: true,
|
noSSL: true,
|
||||||
@ -131,7 +124,6 @@ void main() {
|
|||||||
var responseRec = await sendHttpRequest(
|
var responseRec = await sendHttpRequest(
|
||||||
requestModelOptions1.id,
|
requestModelOptions1.id,
|
||||||
requestModelOptions1.apiType,
|
requestModelOptions1.apiType,
|
||||||
AuthModel(type: APIAuthType.none),
|
|
||||||
requestModelOptions1.httpRequestModel!,
|
requestModelOptions1.httpRequestModel!,
|
||||||
defaultUriScheme: kDefaultUriScheme,
|
defaultUriScheme: kDefaultUriScheme,
|
||||||
noSSL: false,
|
noSSL: false,
|
||||||
|
Reference in New Issue
Block a user