Skip to content

Commit

Permalink
Merge pull request #560 from Clasherzz/client_termination
Browse files Browse the repository at this point in the history
Closing client when request is removed .
  • Loading branch information
ashitaprasad authored Feb 15, 2025
2 parents bb2491e + b726000 commit c69c937
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
10 changes: 3 additions & 7 deletions lib/providers/collection_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,18 @@ final requestSequenceProvider = StateProvider<List<String>>((ref) {
return ids ?? [];
});

final httpClientManager = HttpClientManager();

final StateNotifierProvider<CollectionStateNotifier, Map<String, RequestModel>?>
collectionStateNotifierProvider =
StateNotifierProvider((ref) => CollectionStateNotifier(
ref,
hiveHandler,
httpClientManager,
));

class CollectionStateNotifier
extends StateNotifier<Map<String, RequestModel>?> {
CollectionStateNotifier(
this.ref,
this.hiveHandler,
this.httpClientManager,
) : super(null) {
var status = loadData();
Future.microtask(() {
Expand All @@ -57,7 +53,6 @@ class CollectionStateNotifier
final Ref ref;
final HiveHandler hiveHandler;
final baseHttpResponseModel = const HttpResponseModel();
final HttpClientManager httpClientManager;

bool hasId(String id) => state?.keys.contains(id) ?? false;

Expand Down Expand Up @@ -117,6 +112,7 @@ class CollectionStateNotifier
final rId = id ?? ref.read(selectedIdStateProvider);
var itemIds = ref.read(requestSequenceProvider);
int idx = itemIds.indexOf(rId!);
cancelHttpRequest(rId);
itemIds.remove(rId);
ref.read(requestSequenceProvider.notifier).state = [...itemIds];

Expand Down Expand Up @@ -293,7 +289,7 @@ class CollectionStateNotifier
state = map;

bool noSSL = ref.read(settingsProvider).isSSLDisabled;
(HttpResponse?, Duration?, String?)? responseRec = await request(
var responseRec = await sendHttpRequest(
requestId,
apiType,
substitutedHttpRequestModel,
Expand Down Expand Up @@ -349,7 +345,7 @@ class CollectionStateNotifier

void cancelRequest() {
final id = ref.read(selectedIdStateProvider);
httpClientManager.cancelRequest(id);
cancelHttpRequest(id);
unsave();
}

Expand Down
15 changes: 10 additions & 5 deletions packages/apidash_core/lib/services/http_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import 'http_client_manager.dart';

typedef HttpResponse = http.Response;

Future<(HttpResponse?, Duration?, String?)> request(
final httpClientManager = HttpClientManager();

Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
String requestId,
APIType apiType,
HttpRequestModel requestModel, {
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
bool noSSL = false,
}) async {
final clientManager = HttpClientManager();
final client = clientManager.createClient(requestId, noSSL: noSSL);
final client = httpClientManager.createClient(requestId, noSSL: noSSL);

(Uri?, String?) uriRec = getValidRequestUri(
requestModel.url,
Expand Down Expand Up @@ -123,14 +124,18 @@ Future<(HttpResponse?, Duration?, String?)> request(
stopwatch.stop();
return (response, stopwatch.elapsed, null);
} catch (e) {
if (clientManager.wasRequestCancelled(requestId)) {
if (httpClientManager.wasRequestCancelled(requestId)) {
return (null, null, kMsgRequestCancelled);
}
return (null, null, e.toString());
} finally {
clientManager.closeClient(requestId);
httpClientManager.closeClient(requestId);
}
} else {
return (null, null, uriRec.$2);
}
}

void cancelHttpRequest(String? requestId) {
httpClientManager.cancelRequest(requestId);
}
8 changes: 4 additions & 4 deletions test/models/response_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void main() {
});

test('Testing fromResponse', () async {
(HttpResponse?, Duration?, String?)? responseRec = await request(
var responseRec = await sendHttpRequest(
requestModelGet1.id,
requestModelGet1.apiType,
requestModelGet1.httpRequestModel!,
Expand All @@ -32,7 +32,7 @@ void main() {
});

test('Testing fromResponse for contentType not Json', () async {
(HttpResponse?, Duration?, String?)? responseRec = await request(
var responseRec = await sendHttpRequest(
requestModelGet13.id,
requestModelGet1.apiType,
requestModelGet13.httpRequestModel!,
Expand All @@ -48,7 +48,7 @@ void main() {
});

test('Testing fromResponse for Bad SSL with certificate check', () async {
(HttpResponse?, Duration?, String?)? responseRec = await request(
var responseRec = await sendHttpRequest(
requestModelGetBadSSL.id,
requestModelGet1.apiType,
requestModelGetBadSSL.httpRequestModel!,
Expand All @@ -60,7 +60,7 @@ void main() {
});

test('Testing fromResponse for Bad SSL with no certificate check', () async {
(HttpResponse?, Duration?, String?)? responseRec = await request(
var responseRec = await sendHttpRequest(
requestModelGetBadSSL.id,
requestModelGet1.apiType,
requestModelGetBadSSL.httpRequestModel!,
Expand Down

0 comments on commit c69c937

Please sign in to comment.