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

Implemented Call Adapter #729

Merged
merged 3 commits into from
Dec 30, 2024
Merged

Conversation

farmery
Copy link
Contributor

@farmery farmery commented Dec 24, 2024

Introduced CallAdapters, a feature that allows you to adapt the return type of a network call from one type to another.
For example:
Future<User>Future<Result<User>>.

A CallAdapter takes the original return type R and transforms it into a new type T. This is particularly useful when working with response wrappers like Either, Result, or ApiResponse.

Sample Code:

class MyCallAdapter<T> extends CallAdapter<Future<T>, Future<Result<T>>> {
  @override
  Future<Result<T>> adapt(Future<T> Function() call) async {
    try {
      final response = await call();
      return Result<T>.ok(response);
    } catch (e) {
      return Result.err(e.toString());
    }
  }
}

@RestApi(callAdapter: MyCallAdapter)
abstract class RestClient {
  factory RestClient(Dio dio, {String? baseUrl}) = _RestClient;

  @GET('/')
  Future<Result<User>> getUser();
}

Generated Code Snippet:

  @override
  Future<Result<User>> getUser() {
    return MyCallAdapter<User>().adapt(
      () => _getUser(),
    );
  }

where the private method "_getUser()" is the method that makes the Api request

@trevorwang trevorwang merged commit ad250f7 into trevorwang:master Dec 30, 2024
2 checks passed
trevorwang pushed a commit that referenced this pull request Dec 30, 2024
* Implement Call Adapter

* fix deps

* rename callAdapterInterface to CallAdapter
@farmery farmery deleted the feat/callAdapter branch December 30, 2024 07:01
trevorwang added a commit that referenced this pull request Dec 30, 2024
trevorwang added a commit that referenced this pull request Dec 30, 2024
trevorwang added a commit that referenced this pull request Dec 30, 2024
trevorwang added a commit that referenced this pull request Dec 30, 2024
* feat: "Implemented Call Adapter (#729)" (#730)

This reverts commit e8bdb48.

* fix: version issue

* fix: fix test

* fix: remove deps overrides
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