From 5fcf5c1b32b8669124259a56cbf849dd559b8738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Szot?= Date: Wed, 31 Jul 2019 20:48:12 +0200 Subject: [PATCH 1/2] fix error handling in search --- lib/src/common/search_service.dart | 36 ++++++++++++++---------------- test/experiment/search.dart | 16 ++++++++----- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/lib/src/common/search_service.dart b/lib/src/common/search_service.dart index b9bc5da5..adfc0fac 100644 --- a/lib/src/common/search_service.dart +++ b/lib/src/common/search_service.dart @@ -20,11 +20,14 @@ class SearchService extends Service { var controller = StreamController(); var isFirst = true; - - PaginationHelper(_github) - .fetchStreamed("GET", "/search/repositories", - params: params, pages: pages) - .listen((response) { + final stream = PaginationHelper(_github).fetchStreamed( + "GET", + "/search/repositories", + params: params, + pages: pages, + ); + + return stream.expand((response) { if (response.statusCode == 403 && response.body.contains("rate limit") && isFirst) { @@ -36,15 +39,13 @@ class SearchService extends Service { var input = jsonDecode(response.body); if (input['items'] == null) { - return; + return []; } var items = input['items'] as List; - items.map((item) => Repository.fromJSON(item)).forEach(controller.add); - }).onDone(controller.close); - - return controller.stream; + return items.map((item) => Repository.fromJSON(item)); + }); } /// Search through code for a given [query]. @@ -142,10 +143,10 @@ class SearchService extends Service { var controller = StreamController(); var isFirst = true; + final stream = PaginationHelper(_github) + .fetchStreamed("GET", "/search/users", params: params, pages: pages); - PaginationHelper(_github) - .fetchStreamed("GET", "/search/users", params: params, pages: pages) - .listen((response) { + return stream.expand((response) { if (response.statusCode == 403 && response.body.contains("rate limit") && isFirst) { @@ -157,14 +158,11 @@ class SearchService extends Service { var input = jsonDecode(response.body); if (input['items'] == null) { - return; + return []; } var items = input['items'] as List; - - items.map((item) => User.fromJson(item)).forEach(controller.add); - }).onDone(controller.close); - - return controller.stream; + return items.map((item) => User.fromJson(item)); + }); } } diff --git a/test/experiment/search.dart b/test/experiment/search.dart index 84b5f366..cddd1328 100755 --- a/test/experiment/search.dart +++ b/test/experiment/search.dart @@ -1,10 +1,16 @@ import "package:github/server.dart"; -void main() { +void main() async { var github = createGitHubClient(); - github.search.repositories("github").listen((repo) { - print( - "${repo.fullName}: ${repo.description.isNotEmpty ? repo.description : "No Description"}"); - }).onDone(() => github.dispose()); + try { + for (var i = 0; i < 10; i++) { + // keep executing until we reach the rate limiter + final stream = github.search.repositories("github"); + final all = await stream.toList(); + print(all.length); + } + } catch (e) { + print(e); + } } From 7221393436516f5d6375bfd19e7626dbf74b1a38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Szot?= Date: Wed, 31 Jul 2019 20:52:21 +0200 Subject: [PATCH 2/2] fix error handling in search --- lib/src/common/search_service.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/src/common/search_service.dart b/lib/src/common/search_service.dart index adfc0fac..d3c451b8 100644 --- a/lib/src/common/search_service.dart +++ b/lib/src/common/search_service.dart @@ -140,8 +140,6 @@ class SearchService extends Service { params["per_page"] = perPage.toString(); - var controller = StreamController(); - var isFirst = true; final stream = PaginationHelper(_github) .fetchStreamed("GET", "/search/users", params: params, pages: pages); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy