mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 10:49:49 +08:00
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:
@@ -109,6 +109,7 @@ 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',
|
||||||
|
|||||||
@@ -158,6 +158,7 @@ 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,
|
||||||
@@ -165,6 +166,7 @@ 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,
|
||||||
@@ -204,7 +206,8 @@ http.Request prepareHttpRequest({
|
|||||||
Future<Stream<HttpStreamOutput>> streamHttpRequest(
|
Future<Stream<HttpStreamOutput>> streamHttpRequest(
|
||||||
String requestId,
|
String requestId,
|
||||||
APIType apiType,
|
APIType apiType,
|
||||||
HttpRequestModel requestModel, {
|
AuthModel? authData,
|
||||||
|
HttpRequestModel httpRequestModel, {
|
||||||
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
|
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
|
||||||
bool noSSL = false,
|
bool noSSL = false,
|
||||||
}) async {
|
}) async {
|
||||||
@@ -249,9 +252,24 @@ Future<Stream<HttpStreamOutput>> streamHttpRequest(
|
|||||||
}
|
}
|
||||||
|
|
||||||
final client = httpClientManager.createClient(requestId, noSSL: noSSL);
|
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(
|
final (uri, uriError) = getValidRequestUri(
|
||||||
requestModel.url,
|
authenticatedHttpRequestModel.url,
|
||||||
requestModel.enabledParams,
|
authenticatedHttpRequestModel.enabledParams,
|
||||||
defaultUriScheme: defaultUriScheme,
|
defaultUriScheme: defaultUriScheme,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -264,7 +282,7 @@ Future<Stream<HttpStreamOutput>> streamHttpRequest(
|
|||||||
final streamedResponse = await makeStreamedRequest(
|
final streamedResponse = await makeStreamedRequest(
|
||||||
client: client,
|
client: client,
|
||||||
uri: uri,
|
uri: uri,
|
||||||
requestModel: requestModel,
|
requestModel: authenticatedHttpRequestModel,
|
||||||
apiType: apiType,
|
apiType: apiType,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ void main() {
|
|||||||
final stream = await streamHttpRequest(
|
final stream = await streamHttpRequest(
|
||||||
'graphql_test',
|
'graphql_test',
|
||||||
APIType.graphql,
|
APIType.graphql,
|
||||||
|
null,
|
||||||
model,
|
model,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -54,6 +55,7 @@ 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;
|
||||||
@@ -76,6 +78,7 @@ 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');
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import 'dart:async';
|
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
@@ -19,6 +18,7 @@ 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 ?? '{}');
|
||||||
@@ -59,6 +59,7 @@ 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 ?? '{}');
|
||||||
@@ -86,7 +87,12 @@ void main() {
|
|||||||
NameValueModel(name: 'Accept', value: 'application/json'),
|
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;
|
final output = await stream.first;
|
||||||
expect(
|
expect(
|
||||||
output?.$2?.statusCode == 200,
|
output?.$2?.statusCode == 200,
|
||||||
@@ -106,7 +112,12 @@ void main() {
|
|||||||
body: '{"title": "foo", "body": "bar", "userId": 1}',
|
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;
|
final output = await stream.first;
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
@@ -124,6 +135,7 @@ 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;
|
||||||
@@ -139,6 +151,7 @@ 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;
|
||||||
@@ -159,6 +172,7 @@ 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;
|
||||||
@@ -181,7 +195,12 @@ void main() {
|
|||||||
NameValueModel(name: 'Accept', value: 'application/json'),
|
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');
|
cancelHttpRequest('get_test_c');
|
||||||
final output = await stream.first;
|
final output = await stream.first;
|
||||||
final errMsg = output?.$4;
|
final errMsg = output?.$4;
|
||||||
|
|||||||
@@ -13,7 +13,12 @@ void main() {
|
|||||||
method: HTTPVerb.get,
|
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 outputs = <HttpStreamOutput?>[];
|
||||||
final subscription = stream.listen(outputs.add);
|
final subscription = stream.listen(outputs.add);
|
||||||
@@ -39,7 +44,12 @@ void main() {
|
|||||||
method: HTTPVerb.get,
|
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 outputs = <HttpStreamOutput?>[];
|
||||||
final subscription = stream.listen(outputs.add);
|
final subscription = stream.listen(outputs.add);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user