Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
takahirom | 727331c | 2018-03-04 14:02:32 +0900 | [diff] [blame] | 5 | import io.reactivex.Single |
Roman Elizarov | 00560e8 | 2018-03-05 17:58:59 +0300 | [diff] [blame] | 6 | import kotlinx.coroutines.experimental.* |
takahirom | 727331c | 2018-03-04 14:02:32 +0900 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.rx2.await |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 8 | import retrofit2.Retrofit |
takahirom | 727331c | 2018-03-04 14:02:32 +0900 | [diff] [blame] | 9 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 10 | import retrofit2.converter.gson.GsonConverterFactory |
| 11 | import retrofit2.http.GET |
| 12 | import retrofit2.http.Path |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 13 | |
| 14 | interface GitHub { |
| 15 | @GET("/repos/{owner}/{repo}/contributors") |
| 16 | fun contributors( |
| 17 | @Path("owner") owner: String, |
| 18 | @Path("repo") repo: String |
takahirom | 727331c | 2018-03-04 14:02:32 +0900 | [diff] [blame] | 19 | ): Single<List<Contributor>> |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 20 | |
| 21 | @GET("users/{user}/repos") |
takahirom | 727331c | 2018-03-04 14:02:32 +0900 | [diff] [blame] | 22 | fun listRepos(@Path("user") user: String): Single<List<Repo>> |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | data class Contributor(val login: String, val contributions: Int) |
| 26 | data class Repo(val name: String) |
| 27 | |
Roman Elizarov | 00560e8 | 2018-03-05 17:58:59 +0300 | [diff] [blame] | 28 | fun main(args: Array<String>) = runBlocking { |
| 29 | println("Making GitHub API request") |
| 30 | |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 31 | val retrofit = Retrofit.Builder().apply { |
| 32 | baseUrl("https://api.github.com") |
| 33 | addConverterFactory(GsonConverterFactory.create()) |
takahirom | 727331c | 2018-03-04 14:02:32 +0900 | [diff] [blame] | 34 | addCallAdapterFactory(RxJava2CallAdapterFactory.create()) |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 35 | }.build() |
| 36 | |
| 37 | val github = retrofit.create(GitHub::class.java) |
| 38 | |
Roman Elizarov | 00560e8 | 2018-03-05 17:58:59 +0300 | [diff] [blame] | 39 | val contributors = |
| 40 | github.contributors("JetBrains", "Kotlin") |
| 41 | .await().take(10) |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 42 | |
Roman Elizarov | 00560e8 | 2018-03-05 17:58:59 +0300 | [diff] [blame] | 43 | for ((name, contributions) in contributors) { |
| 44 | println("$name has $contributions contributions, other repos: ") |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 45 | |
Roman Elizarov | 00560e8 | 2018-03-05 17:58:59 +0300 | [diff] [blame] | 46 | val otherRepos = |
| 47 | github.listRepos(name).await() |
| 48 | .map(Repo::name).joinToString(", ") |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 49 | |
Roman Elizarov | 00560e8 | 2018-03-05 17:58:59 +0300 | [diff] [blame] | 50 | println(otherRepos) |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 51 | } |
| 52 | } |