Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor NetworkRequestExecutor; utilise Unit; remove need for dummy providers targeting dynamic types #503

Merged
merged 7 commits into from
Sep 5, 2023

Conversation

marfavi
Copy link
Member

@marfavi marfavi commented Aug 30, 2023

Overview:

This PR refactors the NetworkRequestExecutor class to improve its readability, maintainability, and testability. It also updates all dependent components to align with the new executor methods.

Notably, it also removes the need to provide dummy values for certain dynamic types, e.g. for the type Either<NetworkFailure, dynamic>, which triggered linter warnings.

Changes to NetworkRequestExecutor:

  1. Added New Typedefs: Introduced _NetworkRequest<BodyType> and _ExecutorResult<R> to avoid repetition.

  2. Method Renaming and Refactoring:

    • Renamed call to execute and updated its signature.
    • Renamed logResponse to _logResponse and slightly simplified its logic.
    • Added executeAndDiscard for requests where the response body is not needed.
      • This change prevents the need to provide dummy values for dynamic types as mentioned in the overview.
  3. New Mapping Extensions:

    • Added ExecutorMapX and ExecutorMapAllX to handle mapping of results. The call to map() or mapAll() should be chained after an execute() call.
    • Calling mapAll() is only possible when dealing with executor calls where the expected response body is an Iterable.
    • The aim of these are to provide a better DX, and to aid the transition into fpdart's TaskEither class in the future.
    • These are added to a new file network_request_executor_mapping.dart, which is a part of network_request_executor.dart.
  4. Added Documentation: Added doc comments to all methods above.

How it affects code

Take a look at how code is affected by this refactor:

leaderboard_remote_data_source.dart occupation_remote_data_source.dart
   Future<Either<NetworkFailure, List<LeaderboardUser>>> getLeaderboard(
     LeaderboardFilter category,
     int top,
-  ) async {
-    return executor(
-      () => apiV2.apiV2LeaderboardTopGet(preset: category.label, top: top),
-    ).bindFuture(
-      (result) => result.map(LeaderboardUserModel.fromDTO).toList(),
-    );
+  ) {
+    return executor
+        .execute(
+          () => apiV2.apiV2LeaderboardTopGet(preset: category.label, top: top),
+        )
+        .mapAll(LeaderboardUserModel.fromDTO);
   }
 
   Future<Either<NetworkFailure, LeaderboardUser>> getLeaderboardUser(
     LeaderboardFilter category,
-  ) async {
-    return executor(
-      () => apiV2.apiV2LeaderboardGet(preset: category.label),
-    ).bindFuture(LeaderboardUserModel.fromDTO);
+  ) {
+    return executor
+        .execute(() => apiV2.apiV2LeaderboardGet(preset: category.label))
+        .map(LeaderboardUserModel.fromDTO);
   }
 }
-  Future<Either<NetworkFailure, List<OccupationModel>>> getOccupations() async {
-    final result = await executor(
-      () => api.apiV1ProgrammesGet(),
-    );
-    return result
-        .map((result) => result.map(OccupationModel.fromDTOV1).toList());
+  Future<Either<NetworkFailure, List<OccupationModel>>> getOccupations() {
+    return executor
+        .execute(api.apiV1ProgrammesGet)
+        .mapAll(OccupationModel.fromDTOV1);
  }
  • No async needed: No await keyword in code means no async tag is needed in the method signature.
  • Return type of execute call visible: By renaming from call, the return type of execute can be easily accessed with your preferred code editor. This aims to improve DX and fits well alongside the new similarly named method executeAndDiscard.
  • Method call chaining format: By renaming from call, the Dart formatter will automatically format chained method calls in a way that better conveys what's happening.

Other changes

  1. Updates in Other Components:

    • Updated EnvironmentRemoteDataSource, OpeningHoursRemoteDataSource, ProductRemoteDataSource, etc., to use the new executor methods.
      • The map()/mapAll() extensions remove either_extensions.dart as a dependency to these files.
    • Removed OpeningHoursRepository (and Impl); replaced with new class OpeningHoursModel to better align with the rest of the codebase.
    • Changed OpeningHoursRemoteDataSource.getOpeningHours() to return an entity rather than a generated DTO class to better align the rest of the codebase.
  2. Test Updates: Updated tests to align with the new executor methods.

  3. Removed a barrel file that wasn't supposed to exist in feature Opening Hours

    • This accounts for ~9 files changed just for changed imports.
  4. ticket_remote_data_source.dart: Moved mapper method out of the large chain of calls to improve readability.

Why This is Necessary:

  1. Readability: Method renaming and new typedefs make the code easier to understand.

  2. Testability: The executeAndDiscard method simplifies testing for requests where the response body is not important.

  3. Maintainability: Improved documentation and more descriptive method names make the code easier to maintain.


Please review and let me know if any changes are required.

@ghost

This comment was marked as duplicate.

@codecov
Copy link

codecov bot commented Aug 30, 2023

Codecov Report

Patch coverage: 88.09% and project coverage change: +5.27% 🎉

Comparison is base (4aed248) 79.58% compared to head (03cc4a6) 84.85%.
Report is 2 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #503      +/-   ##
===========================================
+ Coverage    79.58%   84.85%   +5.27%     
===========================================
  Files          114      114              
  Lines         1195     1195              
===========================================
+ Hits           951     1014      +63     
+ Misses         244      181      -63     
Flag Coverage Δ
unittests 84.85% <88.09%> (+5.27%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
...ening_hours/domain/usecases/check_open_status.dart 100.00% <ø> (ø)
..._hours/presentation/cubit/opening_hours_cubit.dart 100.00% <ø> (ø)
...atures/register/domain/usecases/register_user.dart 80.00% <ø> (ø)
...user/domain/usecases/request_account_deletion.dart 100.00% <ø> (ø)
...user/data/datasources/user_remote_data_source.dart 56.25% <53.33%> (-2.58%) ⬇️
.../data/datasources/purchase_remote_data_source.dart 75.00% <70.00%> (+11.36%) ⬆️
...r/data/datasources/voucher_remote_data_source.dart 83.33% <75.00%> (+3.33%) ⬆️
...e/data/datasources/account_remote_data_source.dart 83.33% <81.25%> (+7.14%) ⬆️
...ta/datasources/leaderboard_remote_data_source.dart 64.28% <85.71%> (+7.14%) ⬆️
lib/core/network/network_request_executor.dart 100.00% <100.00%> (ø)
... and 11 more

... and 6 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@marfavi marfavi changed the title Fix dynamic in dummy providers Refactor NetworkRequestExecutor; utilise Unit; remove need for dummy providers targeting dynamic types Aug 30, 2023
@marfavi marfavi marked this pull request as ready for review August 30, 2023 13:06
@marfavi marfavi changed the title Refactor NetworkRequestExecutor; utilise Unit; remove need for dummy providers targeting dynamic types Refactor NetworkRequestExecutor; utilise Unit; remove need for dummy providers targeting dynamic types Aug 30, 2023
@marfavi marfavi requested a review from a team August 30, 2023 13:10
Copy link
Member

@fremartini fremartini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor things :)

@fremartini fremartini enabled auto-merge (squash) September 5, 2023 14:52
@fremartini fremartini merged commit ececdfb into develop Sep 5, 2023
7 checks passed
@fremartini fremartini deleted the fix-dynamic-in-dummy-providers branch September 5, 2023 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants