Skip to content

Can not extend RetryClient because it's final #991

Closed
@techouse

Description

@techouse

Hi,

In 0.13.6 I used to extend the RetryClient in order to add caching support inside the send method, for example

@override
Future<StreamedResponse> send(BaseRequest request) async {
  if (request.method == 'GET') {
    /// Check if there is a cached response with this URL
    final CachedResponse? cachedResponse = store.getCachedResponse(request);

    /// In case we have the old cached response
    if (cachedResponse != null) {
      return cachedResponse.streamedResponse;
    }

    /// In case there is no cache send the request
    final StreamedResponse response = await super.send(request);

    /// If the response is OK cache it
    if (response.statusCode >= HttpStatus.ok &&
        response.statusCode < HttpStatus.multipleChoices) {
      /// Grab the stream
      final Uint8List body = await response.stream.toBytes();

      /// Cache it
      store.cacheResponse(request, response, body);

      /// Return a copy with a fresh stream
      return response.copyWith(
        stream: Stream<List<int>>.value(
          List<int>.from(body),
        ),
      );
    }

    /// Return the response
    return response;
  }

  /// Non-GET requests are not cached
  return super.send(request);
}

In #920 the class got the final modifier and the only way for me to add caching now would be to completely copy the RetryClient and maintain it.

Could you explain why this decision was taken to add the final modifier to the RetryClient but not to the IOClient which I can still extend with this kind of caching strategy?

Metadata

Metadata

Assignees

No one assigned

    Labels

    package:httptype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions