Merge pull request #560 from Clasherzz/client_termination

Closing client when request is removed .
This commit is contained in:
Ashita Prasad
2025-02-15 18:24:18 +05:30
committed by GitHub
3 changed files with 17 additions and 16 deletions

View File

@ -25,14 +25,11 @@ final requestSequenceProvider = StateProvider<List<String>>((ref) {
return ids ?? []; return ids ?? [];
}); });
final httpClientManager = HttpClientManager();
final StateNotifierProvider<CollectionStateNotifier, Map<String, RequestModel>?> final StateNotifierProvider<CollectionStateNotifier, Map<String, RequestModel>?>
collectionStateNotifierProvider = collectionStateNotifierProvider =
StateNotifierProvider((ref) => CollectionStateNotifier( StateNotifierProvider((ref) => CollectionStateNotifier(
ref, ref,
hiveHandler, hiveHandler,
httpClientManager,
)); ));
class CollectionStateNotifier class CollectionStateNotifier
@ -40,7 +37,6 @@ class CollectionStateNotifier
CollectionStateNotifier( CollectionStateNotifier(
this.ref, this.ref,
this.hiveHandler, this.hiveHandler,
this.httpClientManager,
) : super(null) { ) : super(null) {
var status = loadData(); var status = loadData();
Future.microtask(() { Future.microtask(() {
@ -57,7 +53,6 @@ class CollectionStateNotifier
final Ref ref; final Ref ref;
final HiveHandler hiveHandler; final HiveHandler hiveHandler;
final baseHttpResponseModel = const HttpResponseModel(); final baseHttpResponseModel = const HttpResponseModel();
final HttpClientManager httpClientManager;
bool hasId(String id) => state?.keys.contains(id) ?? false; bool hasId(String id) => state?.keys.contains(id) ?? false;
@ -117,6 +112,7 @@ class CollectionStateNotifier
final rId = id ?? ref.read(selectedIdStateProvider); final rId = id ?? ref.read(selectedIdStateProvider);
var itemIds = ref.read(requestSequenceProvider); var itemIds = ref.read(requestSequenceProvider);
int idx = itemIds.indexOf(rId!); int idx = itemIds.indexOf(rId!);
cancelHttpRequest(rId);
itemIds.remove(rId); itemIds.remove(rId);
ref.read(requestSequenceProvider.notifier).state = [...itemIds]; ref.read(requestSequenceProvider.notifier).state = [...itemIds];
@ -293,7 +289,7 @@ class CollectionStateNotifier
state = map; state = map;
bool noSSL = ref.read(settingsProvider).isSSLDisabled; bool noSSL = ref.read(settingsProvider).isSSLDisabled;
(HttpResponse?, Duration?, String?)? responseRec = await request( var responseRec = await sendHttpRequest(
requestId, requestId,
apiType, apiType,
substitutedHttpRequestModel, substitutedHttpRequestModel,
@ -349,7 +345,7 @@ class CollectionStateNotifier
void cancelRequest() { void cancelRequest() {
final id = ref.read(selectedIdStateProvider); final id = ref.read(selectedIdStateProvider);
httpClientManager.cancelRequest(id); cancelHttpRequest(id);
unsave(); unsave();
} }

View File

@ -10,15 +10,16 @@ import 'http_client_manager.dart';
typedef HttpResponse = http.Response; typedef HttpResponse = http.Response;
Future<(HttpResponse?, Duration?, String?)> request( final httpClientManager = HttpClientManager();
Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
String requestId, String requestId,
APIType apiType, APIType apiType,
HttpRequestModel requestModel, { HttpRequestModel requestModel, {
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme, SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
bool noSSL = false, bool noSSL = false,
}) async { }) async {
final clientManager = HttpClientManager(); final client = httpClientManager.createClient(requestId, noSSL: noSSL);
final client = clientManager.createClient(requestId, noSSL: noSSL);
(Uri?, String?) uriRec = getValidRequestUri( (Uri?, String?) uriRec = getValidRequestUri(
requestModel.url, requestModel.url,
@ -123,14 +124,18 @@ Future<(HttpResponse?, Duration?, String?)> request(
stopwatch.stop(); stopwatch.stop();
return (response, stopwatch.elapsed, null); return (response, stopwatch.elapsed, null);
} catch (e) { } catch (e) {
if (clientManager.wasRequestCancelled(requestId)) { if (httpClientManager.wasRequestCancelled(requestId)) {
return (null, null, kMsgRequestCancelled); return (null, null, kMsgRequestCancelled);
} }
return (null, null, e.toString()); return (null, null, e.toString());
} finally { } finally {
clientManager.closeClient(requestId); httpClientManager.closeClient(requestId);
} }
} else { } else {
return (null, null, uriRec.$2); return (null, null, uriRec.$2);
} }
} }
void cancelHttpRequest(String? requestId) {
httpClientManager.cancelRequest(requestId);
}

View File

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