fetchStreamed method

Stream<Response> fetchStreamed(
  1. String method,
  2. String path, {
  3. int? pages,
  4. Map<String, String>? headers,
  5. Map<String, dynamic>? params,
  6. String? body,
  7. int statusCode = 200,
})

Implementation

Stream<http.Response> fetchStreamed(String method, String path,
    {int? pages,
    Map<String, String>? headers,
    Map<String, dynamic>? params,
    String? body,
    int statusCode = 200}) async* {
  var count = 0;
  const serverErrorBackOff = Duration(seconds: 10);
  const maxServerErrors = 10;
  var serverErrors = 0;

  if (params == null) {
    params = {};
  } else {
    params = Map.from(params);
  }

  var page = params['page'] ?? 1;
  params['page'] = page;

  // ignore: literal_only_boolean_expressions
  while (true) {
    http.Response response;
    try {
      response = await github.request(method, path,
          headers: headers,
          params: params,
          body: body,
          statusCode: statusCode);
    } on ServerError {
      serverErrors += 1;
      if (serverErrors >= maxServerErrors) {
        break;
      }
      await Future.delayed(serverErrorBackOff);
      continue;
    }

    yield response;

    count++;

    if (pages != null && count >= pages) {
      break;
    }

    final link = response.headers['link'];

    if (link == null) {
      break;
    }

    final info = parseLinkHeader(link);

    final next = info['next'];

    if (next == null) {
      break;
    }

    params['page'] = ++page;
  }
}