From 625254b20f2f6f0b0b51cfe6c1628d6f18e12164 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Wed, 6 Aug 2025 02:42:23 +0530 Subject: [PATCH] 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. --- lib/providers/collection_providers.dart | 84 ++++++++----------- .../better_networking_example/lib/main.dart | 6 +- .../lib/models/http_response_model.dart | 7 +- .../lib/services/http_service.dart | 4 +- .../lib/utils/auth/handle_auth.dart | 1 - .../test/services/request_types/gql_test.dart | 3 - .../services/request_types/rest_test.dart | 26 +----- .../test/services/request_types/sse_test.dart | 14 +--- .../test/utils/auth/auth_handling_test.dart | 1 - test/models/response_model_test.dart | 8 -- 10 files changed, 52 insertions(+), 102 deletions(-) diff --git a/lib/providers/collection_providers.dart b/lib/providers/collection_providers.dart index 29c05819..96da88a0 100644 --- a/lib/providers/collection_providers.dart +++ b/lib/providers/collection_providers.dart @@ -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, diff --git a/packages/better_networking/better_networking_example/lib/main.dart b/packages/better_networking/better_networking_example/lib/main.dart index a6d4c6e4..6a7255ed 100644 --- a/packages/better_networking/better_networking_example/lib/main.dart +++ b/packages/better_networking/better_networking_example/lib/main.dart @@ -54,10 +54,10 @@ class _BetterNetworkingExampleState extends State { 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 { 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 { 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, diff --git a/packages/better_networking/lib/models/http_response_model.dart b/packages/better_networking/lib/models/http_response_model.dart index f8b779d7..83c96e6d 100644 --- a/packages/better_networking/lib/models/http_response_model.dart +++ b/packages/better_networking/lib/models/http_response_model.dart @@ -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, ); } } diff --git a/packages/better_networking/lib/services/http_service.dart b/packages/better_networking/lib/services/http_service.dart index 2b2da479..e5f3ff63 100644 --- a/packages/better_networking/lib/services/http_service.dart +++ b/packages/better_networking/lib/services/http_service.dart @@ -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> streamHttpRequest( String requestId, APIType apiType, - AuthModel? authData, HttpRequestModel httpRequestModel, { SupportedUriSchemes defaultUriScheme = kDefaultUriScheme, bool noSSL = false, }) async { + final authData = httpRequestModel.authModel; final controller = StreamController(); StreamSubscription?>? subscription; final stopwatch = Stopwatch()..start(); diff --git a/packages/better_networking/lib/utils/auth/handle_auth.dart b/packages/better_networking/lib/utils/auth/handle_auth.dart index b18c3dab..ffd09686 100644 --- a/packages/better_networking/lib/utils/auth/handle_auth.dart +++ b/packages/better_networking/lib/utils/auth/handle_auth.dart @@ -109,7 +109,6 @@ Future handleAuth( final httpResult = await sendHttpRequest( "digest-${Random.secure()}", APIType.rest, - null, httpRequestModel, ); final httpResponse = httpResult.$1; diff --git a/packages/better_networking/test/services/request_types/gql_test.dart b/packages/better_networking/test/services/request_types/gql_test.dart index 47adda43..e90a8d59 100644 --- a/packages/better_networking/test/services/request_types/gql_test.dart +++ b/packages/better_networking/test/services/request_types/gql_test.dart @@ -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'); diff --git a/packages/better_networking/test/services/request_types/rest_test.dart b/packages/better_networking/test/services/request_types/rest_test.dart index 8c8b472b..d8e7492a 100644 --- a/packages/better_networking/test/services/request_types/rest_test.dart +++ b/packages/better_networking/test/services/request_types/rest_test.dart @@ -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; diff --git a/packages/better_networking/test/services/request_types/sse_test.dart b/packages/better_networking/test/services/request_types/sse_test.dart index b7116b8c..e9084937 100644 --- a/packages/better_networking/test/services/request_types/sse_test.dart +++ b/packages/better_networking/test/services/request_types/sse_test.dart @@ -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 = []; 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 = []; final subscription = stream.listen(outputs.add); diff --git a/packages/better_networking/test/utils/auth/auth_handling_test.dart b/packages/better_networking/test/utils/auth/auth_handling_test.dart index 4448da5a..40f2cb2a 100644 --- a/packages/better_networking/test/utils/auth/auth_handling_test.dart +++ b/packages/better_networking/test/utils/auth/auth_handling_test.dart @@ -14,7 +14,6 @@ void main() { final result = await sendHttpRequest( 'test-request', APIType.rest, - null, httpRequestModel, ); diff --git a/test/models/response_model_test.dart b/test/models/response_model_test.dart index 56ba1e2a..48ec8f13 100644 --- a/test/models/response_model_test.dart +++ b/test/models/response_model_test.dart @@ -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,