Add authData parameter to HTTP request functions

Introduces an optional AuthModel parameter to sendHttpRequest and streamHttpRequest, allowing authentication handling for HTTP requests. Updates all usages and tests to pass null for the new parameter where authentication is not required.
This commit is contained in:
Ankit Mahato
2025-08-05 12:49:33 +05:30
parent 254530d9ae
commit 257b30a1e9
5 changed files with 61 additions and 10 deletions

View File

@@ -109,6 +109,7 @@ class _BetterNetworkingExampleState extends State<BetterNetworkingExample> {
final stream = await streamHttpRequest(
'S1',
APIType.rest,
null,
HttpRequestModel(
method: HTTPVerb.post,
url: 'http://localhost:11434/v1/chat/completions',

View File

@@ -158,6 +158,7 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequestV1(
Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
String requestId,
APIType apiType,
AuthModel? authData,
HttpRequestModel requestModel, {
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
bool noSSL = false,
@@ -165,6 +166,7 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
final stream = await streamHttpRequest(
requestId,
apiType,
authData,
requestModel,
defaultUriScheme: defaultUriScheme,
noSSL: noSSL,
@@ -204,7 +206,8 @@ http.Request prepareHttpRequest({
Future<Stream<HttpStreamOutput>> streamHttpRequest(
String requestId,
APIType apiType,
HttpRequestModel requestModel, {
AuthModel? authData,
HttpRequestModel httpRequestModel, {
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
bool noSSL = false,
}) async {
@@ -249,9 +252,24 @@ Future<Stream<HttpStreamOutput>> streamHttpRequest(
}
final client = httpClientManager.createClient(requestId, noSSL: noSSL);
HttpRequestModel authenticatedHttpRequestModel = httpRequestModel.copyWith();
try {
if (authData != null && authData.type != APIAuthType.none) {
authenticatedHttpRequestModel = await handleAuth(
httpRequestModel,
authData,
);
}
} catch (e) {
await _addErrorMessage(e.toString());
return controller.stream;
}
final (uri, uriError) = getValidRequestUri(
requestModel.url,
requestModel.enabledParams,
authenticatedHttpRequestModel.url,
authenticatedHttpRequestModel.enabledParams,
defaultUriScheme: defaultUriScheme,
);
@@ -264,7 +282,7 @@ Future<Stream<HttpStreamOutput>> streamHttpRequest(
final streamedResponse = await makeStreamedRequest(
client: client,
uri: uri,
requestModel: requestModel,
requestModel: authenticatedHttpRequestModel,
apiType: apiType,
);

View File

@@ -24,6 +24,7 @@ void main() {
final stream = await streamHttpRequest(
'graphql_test',
APIType.graphql,
null,
model,
);
@@ -54,6 +55,7 @@ void main() {
final stream = await streamHttpRequest(
'graphql_bad',
APIType.graphql,
null,
model,
);
final output = await stream.first;
@@ -76,6 +78,7 @@ void main() {
final stream = await streamHttpRequest(
'graphql_test_cancellation',
APIType.graphql,
null,
model,
);
httpClientManager.cancelRequest('graphql_test_cancellation');

View File

@@ -1,4 +1,3 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
@@ -19,6 +18,7 @@ void main() {
final (resp, dur, err) = await sendHttpRequest(
'get_test',
APIType.rest,
null,
model,
);
final output = jsonDecode(resp?.body ?? '{}');
@@ -59,6 +59,7 @@ void main() {
final (resp, dur, err) = await sendHttpRequest(
'mpreq',
APIType.rest,
null,
model,
);
final output = jsonDecode(resp?.body ?? '{}');
@@ -86,7 +87,12 @@ void main() {
NameValueModel(name: 'Accept', value: 'application/json'),
],
);
final stream = await streamHttpRequest('get_test', APIType.rest, model);
final stream = await streamHttpRequest(
'get_test',
APIType.rest,
null,
model,
);
final output = await stream.first;
expect(
output?.$2?.statusCode == 200,
@@ -106,7 +112,12 @@ void main() {
body: '{"title": "foo", "body": "bar", "userId": 1}',
);
final stream = await streamHttpRequest('post_test', APIType.rest, model);
final stream = await streamHttpRequest(
'post_test',
APIType.rest,
null,
model,
);
final output = await stream.first;
expect(
@@ -124,6 +135,7 @@ void main() {
final stream = await streamHttpRequest(
'empty_url_test',
APIType.rest,
null,
model,
);
final output = await stream.first;
@@ -139,6 +151,7 @@ void main() {
final stream = await streamHttpRequest(
'invalid_url_test',
APIType.rest,
null,
model,
);
final output = await stream.first;
@@ -159,6 +172,7 @@ void main() {
final stream = await streamHttpRequest(
'large_body_test',
APIType.rest,
null,
model,
);
final output = await stream.first;
@@ -181,7 +195,12 @@ void main() {
NameValueModel(name: 'Accept', value: 'application/json'),
],
);
final stream = await streamHttpRequest('get_test_c', APIType.rest, model);
final stream = await streamHttpRequest(
'get_test_c',
APIType.rest,
null,
model,
);
cancelHttpRequest('get_test_c');
final output = await stream.first;
final errMsg = output?.$4;

View File

@@ -13,7 +13,12 @@ void main() {
method: HTTPVerb.get,
);
final stream = await streamHttpRequest('sse_test', APIType.rest, model);
final stream = await streamHttpRequest(
'sse_test',
APIType.rest,
null,
model,
);
final outputs = <HttpStreamOutput?>[];
final subscription = stream.listen(outputs.add);
@@ -39,7 +44,12 @@ void main() {
method: HTTPVerb.get,
);
final stream = await streamHttpRequest('sse_test', APIType.rest, model);
final stream = await streamHttpRequest(
'sse_test',
APIType.rest,
null,
model,
);
final outputs = <HttpStreamOutput?>[];
final subscription = stream.listen(outputs.add);