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