From 6c5862cd855765657bca3cd3a1aaa927a98ae7b8 Mon Sep 17 00:00:00 2001 From: Udhay-Adithya Date: Thu, 3 Jul 2025 12:12:18 +0530 Subject: [PATCH] refactor: move authModel to HttpRequestModel --- lib/models/request_model.dart | 1 - lib/models/request_model.freezed.dart | 42 +----------------- lib/models/request_model.g.dart | 5 --- lib/providers/collection_providers.dart | 8 ++-- .../request_pane/request_auth.dart | 13 +++--- .../request_pane/request_pane_graphql.dart | 2 +- .../request_pane/request_pane_rest.dart | 2 +- .../better_networking_example/pubspec.lock | 8 ++++ .../lib/models/http_request_model.dart | 2 + .../models/http_request_model.freezed.dart | 44 ++++++++++++++++++- .../lib/models/http_request_model.g.dart | 4 ++ 11 files changed, 71 insertions(+), 60 deletions(-) diff --git a/lib/models/request_model.dart b/lib/models/request_model.dart index a3eda0ba..4d63c2ad 100644 --- a/lib/models/request_model.dart +++ b/lib/models/request_model.dart @@ -15,7 +15,6 @@ class RequestModel with _$RequestModel { @Default(APIType.rest) APIType apiType, @Default("") String name, @Default("") String description, - @Default(AuthModel(type: APIAuthType.none)) AuthModel? authModel, @JsonKey(includeToJson: false) @Default(0) requestTabIndex, HttpRequestModel? httpRequestModel, int? responseStatus, diff --git a/lib/models/request_model.freezed.dart b/lib/models/request_model.freezed.dart index eca69ebe..72f607bd 100644 --- a/lib/models/request_model.freezed.dart +++ b/lib/models/request_model.freezed.dart @@ -24,7 +24,6 @@ mixin _$RequestModel { APIType get apiType => throw _privateConstructorUsedError; String get name => throw _privateConstructorUsedError; String get description => throw _privateConstructorUsedError; - AuthModel? get authModel => throw _privateConstructorUsedError; @JsonKey(includeToJson: false) dynamic get requestTabIndex => throw _privateConstructorUsedError; HttpRequestModel? get httpRequestModel => throw _privateConstructorUsedError; @@ -60,7 +59,6 @@ abstract class $RequestModelCopyWith<$Res> { APIType apiType, String name, String description, - AuthModel? authModel, @JsonKey(includeToJson: false) dynamic requestTabIndex, HttpRequestModel? httpRequestModel, int? responseStatus, @@ -71,7 +69,6 @@ abstract class $RequestModelCopyWith<$Res> { String? preRequestScript, String? postRequestScript}); - $AuthModelCopyWith<$Res>? get authModel; $HttpRequestModelCopyWith<$Res>? get httpRequestModel; $HttpResponseModelCopyWith<$Res>? get httpResponseModel; } @@ -95,7 +92,6 @@ class _$RequestModelCopyWithImpl<$Res, $Val extends RequestModel> Object? apiType = null, Object? name = null, Object? description = null, - Object? authModel = freezed, Object? requestTabIndex = freezed, Object? httpRequestModel = freezed, Object? responseStatus = freezed, @@ -123,10 +119,6 @@ class _$RequestModelCopyWithImpl<$Res, $Val extends RequestModel> ? _value.description : description // ignore: cast_nullable_to_non_nullable as String, - authModel: freezed == authModel - ? _value.authModel - : authModel // ignore: cast_nullable_to_non_nullable - as AuthModel?, requestTabIndex: freezed == requestTabIndex ? _value.requestTabIndex : requestTabIndex // ignore: cast_nullable_to_non_nullable @@ -166,20 +158,6 @@ class _$RequestModelCopyWithImpl<$Res, $Val extends RequestModel> ) as $Val); } - /// Create a copy of RequestModel - /// with the given fields replaced by the non-null parameter values. - @override - @pragma('vm:prefer-inline') - $AuthModelCopyWith<$Res>? get authModel { - if (_value.authModel == null) { - return null; - } - - return $AuthModelCopyWith<$Res>(_value.authModel!, (value) { - return _then(_value.copyWith(authModel: value) as $Val); - }); - } - /// Create a copy of RequestModel /// with the given fields replaced by the non-null parameter values. @override @@ -222,7 +200,6 @@ abstract class _$$RequestModelImplCopyWith<$Res> APIType apiType, String name, String description, - AuthModel? authModel, @JsonKey(includeToJson: false) dynamic requestTabIndex, HttpRequestModel? httpRequestModel, int? responseStatus, @@ -233,8 +210,6 @@ abstract class _$$RequestModelImplCopyWith<$Res> String? preRequestScript, String? postRequestScript}); - @override - $AuthModelCopyWith<$Res>? get authModel; @override $HttpRequestModelCopyWith<$Res>? get httpRequestModel; @override @@ -258,7 +233,6 @@ class __$$RequestModelImplCopyWithImpl<$Res> Object? apiType = null, Object? name = null, Object? description = null, - Object? authModel = freezed, Object? requestTabIndex = freezed, Object? httpRequestModel = freezed, Object? responseStatus = freezed, @@ -286,10 +260,6 @@ class __$$RequestModelImplCopyWithImpl<$Res> ? _value.description : description // ignore: cast_nullable_to_non_nullable as String, - authModel: freezed == authModel - ? _value.authModel - : authModel // ignore: cast_nullable_to_non_nullable - as AuthModel?, requestTabIndex: freezed == requestTabIndex ? _value.requestTabIndex! : requestTabIndex, @@ -338,7 +308,6 @@ class _$RequestModelImpl implements _RequestModel { this.apiType = APIType.rest, this.name = "", this.description = "", - this.authModel = const AuthModel(type: APIAuthType.none), @JsonKey(includeToJson: false) this.requestTabIndex = 0, this.httpRequestModel, this.responseStatus, @@ -364,9 +333,6 @@ class _$RequestModelImpl implements _RequestModel { @JsonKey() final String description; @override - @JsonKey() - final AuthModel? authModel; - @override @JsonKey(includeToJson: false) final dynamic requestTabIndex; @override @@ -390,7 +356,7 @@ class _$RequestModelImpl implements _RequestModel { @override String toString() { - return 'RequestModel(id: $id, apiType: $apiType, name: $name, description: $description, authModel: $authModel, requestTabIndex: $requestTabIndex, httpRequestModel: $httpRequestModel, responseStatus: $responseStatus, message: $message, httpResponseModel: $httpResponseModel, isWorking: $isWorking, sendingTime: $sendingTime, preRequestScript: $preRequestScript, postRequestScript: $postRequestScript)'; + return 'RequestModel(id: $id, apiType: $apiType, name: $name, description: $description, requestTabIndex: $requestTabIndex, httpRequestModel: $httpRequestModel, responseStatus: $responseStatus, message: $message, httpResponseModel: $httpResponseModel, isWorking: $isWorking, sendingTime: $sendingTime, preRequestScript: $preRequestScript, postRequestScript: $postRequestScript)'; } @override @@ -403,8 +369,6 @@ class _$RequestModelImpl implements _RequestModel { (identical(other.name, name) || other.name == name) && (identical(other.description, description) || other.description == description) && - (identical(other.authModel, authModel) || - other.authModel == authModel) && const DeepCollectionEquality() .equals(other.requestTabIndex, requestTabIndex) && (identical(other.httpRequestModel, httpRequestModel) || @@ -432,7 +396,6 @@ class _$RequestModelImpl implements _RequestModel { apiType, name, description, - authModel, const DeepCollectionEquality().hash(requestTabIndex), httpRequestModel, responseStatus, @@ -465,7 +428,6 @@ abstract class _RequestModel implements RequestModel { final APIType apiType, final String name, final String description, - final AuthModel? authModel, @JsonKey(includeToJson: false) final dynamic requestTabIndex, final HttpRequestModel? httpRequestModel, final int? responseStatus, @@ -488,8 +450,6 @@ abstract class _RequestModel implements RequestModel { @override String get description; @override - AuthModel? get authModel; - @override @JsonKey(includeToJson: false) dynamic get requestTabIndex; @override diff --git a/lib/models/request_model.g.dart b/lib/models/request_model.g.dart index 6c633726..d4f6ec20 100644 --- a/lib/models/request_model.g.dart +++ b/lib/models/request_model.g.dart @@ -12,10 +12,6 @@ _$RequestModelImpl _$$RequestModelImplFromJson(Map json) => _$RequestModelImpl( APIType.rest, name: json['name'] as String? ?? "", description: json['description'] as String? ?? "", - authModel: json['authModel'] == null - ? const AuthModel(type: APIAuthType.none) - : AuthModel.fromJson( - Map.from(json['authModel'] as Map)), requestTabIndex: json['requestTabIndex'] ?? 0, httpRequestModel: json['httpRequestModel'] == null ? null @@ -41,7 +37,6 @@ Map _$$RequestModelImplToJson(_$RequestModelImpl instance) => 'apiType': _$APITypeEnumMap[instance.apiType]!, 'name': instance.name, 'description': instance.description, - 'authModel': instance.authModel?.toJson(), 'httpRequestModel': instance.httpRequestModel?.toJson(), 'responseStatus': instance.responseStatus, 'message': instance.message, diff --git a/lib/providers/collection_providers.dart b/lib/providers/collection_providers.dart index 468e59e7..09434657 100644 --- a/lib/providers/collection_providers.dart +++ b/lib/providers/collection_providers.dart @@ -207,7 +207,7 @@ class CollectionStateNotifier String? id, HTTPVerb? method, APIType? apiType, - AuthModel? authData, + AuthModel? authModel, String? url, String? name, String? description, @@ -235,7 +235,6 @@ class CollectionStateNotifier var currentHttpRequestModel = currentModel.httpRequestModel; final newModel = currentModel.copyWith( apiType: apiType ?? currentModel.apiType, - authModel: authData ?? currentModel.authModel, name: name ?? currentModel.name, description: description ?? currentModel.description, requestTabIndex: requestTabIndex ?? currentModel.requestTabIndex, @@ -244,6 +243,7 @@ class CollectionStateNotifier url: url ?? currentHttpRequestModel.url, headers: headers ?? currentHttpRequestModel.headers, params: params ?? currentHttpRequestModel.params, + authModel: authModel ?? currentHttpRequestModel.authModel, isHeaderEnabledList: isHeaderEnabledList ?? currentHttpRequestModel.isHeaderEnabledList, isParamEnabledList: @@ -316,7 +316,7 @@ class CollectionStateNotifier var responseRec = await sendHttpRequest( requestId, apiType, - requestModel.authModel, + requestModel.httpRequestModel?.authModel, substitutedHttpRequestModel, defaultUriScheme: defaultUriScheme, noSSL: noSSL, @@ -358,7 +358,7 @@ class CollectionStateNotifier httpResponseModel: httpResponseModel, preRequestScript: requestModel.preRequestScript, postRequestScript: requestModel.postRequestScript, - authModel: requestModel.authModel, + authModel: requestModel.httpRequestModel?.authModel, ); ref.read(historyMetaStateNotifier.notifier).addHistoryRequest(model); diff --git a/lib/screens/home_page/editor_pane/details_card/request_pane/request_auth.dart b/lib/screens/home_page/editor_pane/details_card/request_pane/request_auth.dart index 1e54d46f..dd5cc1c7 100644 --- a/lib/screens/home_page/editor_pane/details_card/request_pane/request_auth.dart +++ b/lib/screens/home_page/editor_pane/details_card/request_pane/request_auth.dart @@ -33,10 +33,10 @@ class EditAuthType extends ConsumerWidget { } currentAuthType = ref.watch( - selectedRequestModelProvider - .select((request) => request?.authModel?.type ?? APIAuthType.none), + selectedRequestModelProvider.select((request) => + request?.httpRequestModel?.authModel?.type ?? APIAuthType.none), ); - currentAuthData = selectedRequest.authModel; + currentAuthData = selectedRequest.httpRequestModel?.authModel; } return SingleChildScrollView( child: Padding( @@ -93,7 +93,8 @@ class EditAuthType extends ConsumerWidget { ref.read(selectedRequestModelProvider); if (newType != null) { ref.read(collectionStateNotifierProvider.notifier).update( - authData: selectedRequest?.authModel + authModel: selectedRequest + ?.httpRequestModel?.authModel ?.copyWith(type: newType) ?? AuthModel(type: newType), ); @@ -117,11 +118,11 @@ class EditAuthType extends ConsumerWidget { void updateAuth(AuthModel? model) { if (model == null) { ref.read(collectionStateNotifierProvider.notifier).update( - authData: AuthModel(type: APIAuthType.none), + authModel: AuthModel(type: APIAuthType.none), ); } ref.read(collectionStateNotifierProvider.notifier).update( - authData: model, + authModel: model, ); } diff --git a/lib/screens/home_page/editor_pane/details_card/request_pane/request_pane_graphql.dart b/lib/screens/home_page/editor_pane/details_card/request_pane/request_pane_graphql.dart index f9e18078..91be2b7b 100644 --- a/lib/screens/home_page/editor_pane/details_card/request_pane/request_pane_graphql.dart +++ b/lib/screens/home_page/editor_pane/details_card/request_pane/request_pane_graphql.dart @@ -26,7 +26,7 @@ class EditGraphQLRequestPane extends ConsumerWidget { false; final hasAuth = ref.watch(selectedRequestModelProvider - .select((value) => value?.authModel?.type != APIAuthType.none)); + .select((value) => value?.httpRequestModel?.authModel?.type != APIAuthType.none)); final scriptsLength = ref.watch(selectedHistoryRequestModelProvider .select((value) => value?.preRequestScript?.length)) ?? diff --git a/lib/screens/home_page/editor_pane/details_card/request_pane/request_pane_rest.dart b/lib/screens/home_page/editor_pane/details_card/request_pane/request_pane_rest.dart index 68bf526f..aef0e67a 100644 --- a/lib/screens/home_page/editor_pane/details_card/request_pane/request_pane_rest.dart +++ b/lib/screens/home_page/editor_pane/details_card/request_pane/request_pane_rest.dart @@ -37,7 +37,7 @@ class EditRestRequestPane extends ConsumerWidget { 0; final hasAuth = ref.watch(selectedRequestModelProvider.select((value) => - value?.authModel?.type != APIAuthType.none)); + value?.httpRequestModel?.authModel?.type != APIAuthType.none)); return RequestPane( selectedId: selectedId, diff --git a/packages/better_networking/better_networking_example/pubspec.lock b/packages/better_networking/better_networking_example/pubspec.lock index 366394ab..2e6737af 100644 --- a/packages/better_networking/better_networking_example/pubspec.lock +++ b/packages/better_networking/better_networking_example/pubspec.lock @@ -48,6 +48,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.19.1" + crypto: + dependency: transitive + description: + name: crypto + sha256: "1e445881f28f22d6140f181e07737b22f1e099a5e1ff94b0af2f9e4a463f4855" + url: "https://pub.dev" + source: hosted + version: "3.0.6" cupertino_icons: dependency: "direct main" description: diff --git a/packages/better_networking/lib/models/http_request_model.dart b/packages/better_networking/lib/models/http_request_model.dart index 2d915cfa..7fbaa648 100644 --- a/packages/better_networking/lib/models/http_request_model.dart +++ b/packages/better_networking/lib/models/http_request_model.dart @@ -5,6 +5,7 @@ import '../extensions/extensions.dart'; import '../utils/utils.dart' show rowsToFormDataMapList, rowsToMap, getEnabledRows; import '../consts.dart'; +import 'auth/api_auth_model.dart'; part 'http_request_model.freezed.dart'; part 'http_request_model.g.dart'; @@ -19,6 +20,7 @@ class HttpRequestModel with _$HttpRequestModel { @Default("") String url, List? headers, List? params, + @Default(AuthModel(type: APIAuthType.none)) AuthModel? authModel, List? isHeaderEnabledList, List? isParamEnabledList, @Default(ContentType.json) ContentType bodyContentType, diff --git a/packages/better_networking/lib/models/http_request_model.freezed.dart b/packages/better_networking/lib/models/http_request_model.freezed.dart index 864ae7e0..871d6012 100644 --- a/packages/better_networking/lib/models/http_request_model.freezed.dart +++ b/packages/better_networking/lib/models/http_request_model.freezed.dart @@ -25,6 +25,7 @@ mixin _$HttpRequestModel { String get url => throw _privateConstructorUsedError; List? get headers => throw _privateConstructorUsedError; List? get params => throw _privateConstructorUsedError; + AuthModel? get authModel => throw _privateConstructorUsedError; List? get isHeaderEnabledList => throw _privateConstructorUsedError; List? get isParamEnabledList => throw _privateConstructorUsedError; ContentType get bodyContentType => throw _privateConstructorUsedError; @@ -54,6 +55,7 @@ abstract class $HttpRequestModelCopyWith<$Res> { String url, List? headers, List? params, + AuthModel? authModel, List? isHeaderEnabledList, List? isParamEnabledList, ContentType bodyContentType, @@ -61,6 +63,8 @@ abstract class $HttpRequestModelCopyWith<$Res> { String? query, List? formData, }); + + $AuthModelCopyWith<$Res>? get authModel; } /// @nodoc @@ -82,6 +86,7 @@ class _$HttpRequestModelCopyWithImpl<$Res, $Val extends HttpRequestModel> Object? url = null, Object? headers = freezed, Object? params = freezed, + Object? authModel = freezed, Object? isHeaderEnabledList = freezed, Object? isParamEnabledList = freezed, Object? bodyContentType = null, @@ -107,6 +112,10 @@ class _$HttpRequestModelCopyWithImpl<$Res, $Val extends HttpRequestModel> ? _value.params : params // ignore: cast_nullable_to_non_nullable as List?, + authModel: freezed == authModel + ? _value.authModel + : authModel // ignore: cast_nullable_to_non_nullable + as AuthModel?, isHeaderEnabledList: freezed == isHeaderEnabledList ? _value.isHeaderEnabledList : isHeaderEnabledList // ignore: cast_nullable_to_non_nullable @@ -135,6 +144,20 @@ class _$HttpRequestModelCopyWithImpl<$Res, $Val extends HttpRequestModel> as $Val, ); } + + /// Create a copy of HttpRequestModel + /// with the given fields replaced by the non-null parameter values. + @override + @pragma('vm:prefer-inline') + $AuthModelCopyWith<$Res>? get authModel { + if (_value.authModel == null) { + return null; + } + + return $AuthModelCopyWith<$Res>(_value.authModel!, (value) { + return _then(_value.copyWith(authModel: value) as $Val); + }); + } } /// @nodoc @@ -151,6 +174,7 @@ abstract class _$$HttpRequestModelImplCopyWith<$Res> String url, List? headers, List? params, + AuthModel? authModel, List? isHeaderEnabledList, List? isParamEnabledList, ContentType bodyContentType, @@ -158,6 +182,9 @@ abstract class _$$HttpRequestModelImplCopyWith<$Res> String? query, List? formData, }); + + @override + $AuthModelCopyWith<$Res>? get authModel; } /// @nodoc @@ -178,6 +205,7 @@ class __$$HttpRequestModelImplCopyWithImpl<$Res> Object? url = null, Object? headers = freezed, Object? params = freezed, + Object? authModel = freezed, Object? isHeaderEnabledList = freezed, Object? isParamEnabledList = freezed, Object? bodyContentType = null, @@ -203,6 +231,10 @@ class __$$HttpRequestModelImplCopyWithImpl<$Res> ? _value._params : params // ignore: cast_nullable_to_non_nullable as List?, + authModel: freezed == authModel + ? _value.authModel + : authModel // ignore: cast_nullable_to_non_nullable + as AuthModel?, isHeaderEnabledList: freezed == isHeaderEnabledList ? _value._isHeaderEnabledList : isHeaderEnabledList // ignore: cast_nullable_to_non_nullable @@ -241,6 +273,7 @@ class _$HttpRequestModelImpl extends _HttpRequestModel { this.url = "", final List? headers, final List? params, + this.authModel = const AuthModel(type: APIAuthType.none), final List? isHeaderEnabledList, final List? isParamEnabledList, this.bodyContentType = ContentType.json, @@ -283,6 +316,9 @@ class _$HttpRequestModelImpl extends _HttpRequestModel { return EqualUnmodifiableListView(value); } + @override + @JsonKey() + final AuthModel? authModel; final List? _isHeaderEnabledList; @override List? get isHeaderEnabledList { @@ -324,7 +360,7 @@ class _$HttpRequestModelImpl extends _HttpRequestModel { @override String toString() { - return 'HttpRequestModel(method: $method, url: $url, headers: $headers, params: $params, isHeaderEnabledList: $isHeaderEnabledList, isParamEnabledList: $isParamEnabledList, bodyContentType: $bodyContentType, body: $body, query: $query, formData: $formData)'; + return 'HttpRequestModel(method: $method, url: $url, headers: $headers, params: $params, authModel: $authModel, isHeaderEnabledList: $isHeaderEnabledList, isParamEnabledList: $isParamEnabledList, bodyContentType: $bodyContentType, body: $body, query: $query, formData: $formData)'; } @override @@ -336,6 +372,8 @@ class _$HttpRequestModelImpl extends _HttpRequestModel { (identical(other.url, url) || other.url == url) && const DeepCollectionEquality().equals(other._headers, _headers) && const DeepCollectionEquality().equals(other._params, _params) && + (identical(other.authModel, authModel) || + other.authModel == authModel) && const DeepCollectionEquality().equals( other._isHeaderEnabledList, _isHeaderEnabledList, @@ -359,6 +397,7 @@ class _$HttpRequestModelImpl extends _HttpRequestModel { url, const DeepCollectionEquality().hash(_headers), const DeepCollectionEquality().hash(_params), + authModel, const DeepCollectionEquality().hash(_isHeaderEnabledList), const DeepCollectionEquality().hash(_isParamEnabledList), bodyContentType, @@ -390,6 +429,7 @@ abstract class _HttpRequestModel extends HttpRequestModel { final String url, final List? headers, final List? params, + final AuthModel? authModel, final List? isHeaderEnabledList, final List? isParamEnabledList, final ContentType bodyContentType, @@ -411,6 +451,8 @@ abstract class _HttpRequestModel extends HttpRequestModel { @override List? get params; @override + AuthModel? get authModel; + @override List? get isHeaderEnabledList; @override List? get isParamEnabledList; diff --git a/packages/better_networking/lib/models/http_request_model.g.dart b/packages/better_networking/lib/models/http_request_model.g.dart index bd024acc..48e7a17a 100644 --- a/packages/better_networking/lib/models/http_request_model.g.dart +++ b/packages/better_networking/lib/models/http_request_model.g.dart @@ -18,6 +18,9 @@ _$HttpRequestModelImpl _$$HttpRequestModelImplFromJson( params: (json['params'] as List?) ?.map((e) => NameValueModel.fromJson(Map.from(e as Map))) .toList(), + authModel: json['authModel'] == null + ? const AuthModel(type: APIAuthType.none) + : AuthModel.fromJson(Map.from(json['authModel'] as Map)), isHeaderEnabledList: (json['isHeaderEnabledList'] as List?) ?.map((e) => e as bool) .toList(), @@ -41,6 +44,7 @@ Map _$$HttpRequestModelImplToJson( 'url': instance.url, 'headers': instance.headers?.map((e) => e.toJson()).toList(), 'params': instance.params?.map((e) => e.toJson()).toList(), + 'authModel': instance.authModel?.toJson(), 'isHeaderEnabledList': instance.isHeaderEnabledList, 'isParamEnabledList': instance.isParamEnabledList, 'bodyContentType': _$ContentTypeEnumMap[instance.bodyContentType]!,