startRequest method

Future<Response> startRequest()

Performs the request.

The resulting Response will either be obtained from a valid cache file or a web request. If a valid web Response has been sent, it will be saved in the cache.

Implementation

Future<Response> startRequest() async {
  final currentlyHasCachedDocument = await hasCachedDocument();
  if (currentlyHasCachedDocument) {
    final WebCacheDocument cacheDocument = await loadCachedDocument();
    if (webCacheType.useCachedDocument(cacheDocument)) {
      return Response(cacheDocument.responseValue, 200);
    }
  }

  final uri = Uri.tryParse(url);
  if (uri == null) {
    return Response('Invalid URL format.', 500);
  }

  try {
    Response response = await responseFunction.call(Uri.parse(url), headers: headers);
    if (isValidResponse(response)) {
      await saveNewCacheResponse(response);
    } else if (currentlyHasCachedDocument) {
      // if the response was not valid and there are save-data, use them anyways
      response = Response((await loadCachedDocument()).responseValue, 200);
    }

    return response;
  } catch (e) {
    return Response('An error occurred while performing the request.', 500);
  }
}