retryUnary<R, Q> method

Future<R> retryUnary<R, Q>(
  1. ResponseFuture<R> rpc(
    1. Q request, {
    2. CallOptions? options,
    }),
  2. Q request, {
  3. dynamic onRetry(
    1. Exception
    )?,
  4. CallOptions? options,
})

Call rpc retrying so long as _shouldRefresh return true for the exception thrown.

At every retry the onRetry function will be called (if given). The function rpc will be invoked at-most this.attempts times.

Implementation

Future<R> retryUnary<R, Q>(
    $grpc.ResponseFuture<R> Function(Q request, {$grpc.CallOptions? options})
        rpc,
    Q request,
    {Function(Exception)? onRetry,
    $grpc.CallOptions? options}) async {
  final client = http.Client();
  return _retryOptions.retry(
    () => rpc.call(request, options: options),
    retryIf: (e) {
      return e is $grpc.GrpcError &&
          e.code == $grpc.StatusCode.unauthenticated;
    },
    onRetry: (e) async {
      onRetry?.call(e);
      await _refreshToken(_accessToken, null, client);
    },
  ).whenComplete(() => client.close());
}