forked from kotlin-hands-on/intro-coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest8Rx.kt
31 lines (27 loc) · 889 Bytes
/
Request8Rx.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package tasks
import contributors.*
import io.reactivex.Observable
import io.reactivex.Scheduler
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
fun loadContributorsReactive(
service: GitHubService,
req: RequestData,
scheduler: Scheduler = Schedulers.io()
): Single<List<User>> {
val repos: Observable<Repo> = service
.getOrgReposRx(req.org)
.subscribeOn(scheduler)
.doOnNext { response -> logRepos(req, response) }
.flatMapIterable { it.bodyList() }
val allUsers: Single<List<User>> = repos
.flatMap { repo ->
service.getRepoContributorsRx(req.org, repo.name)
.subscribeOn(scheduler)
.doOnNext { response -> logUsers(repo, response) }
}
.flatMapIterable { it.bodyList() }
.toList()
return allUsers
.map { it.aggregate() }
}