Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
takahirom | 727331c | 2018-03-04 14:02:32 +0900 | [diff] [blame] | 17 | import io.reactivex.Single |
Roman Elizarov | 00560e8 | 2018-03-05 17:58:59 +0300 | [diff] [blame] | 18 | import kotlinx.coroutines.experimental.* |
takahirom | 727331c | 2018-03-04 14:02:32 +0900 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.rx2.await |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 20 | import retrofit2.Retrofit |
takahirom | 727331c | 2018-03-04 14:02:32 +0900 | [diff] [blame] | 21 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 22 | import retrofit2.converter.gson.GsonConverterFactory |
| 23 | import retrofit2.http.GET |
| 24 | import retrofit2.http.Path |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 25 | |
| 26 | interface GitHub { |
| 27 | @GET("/repos/{owner}/{repo}/contributors") |
| 28 | fun contributors( |
| 29 | @Path("owner") owner: String, |
| 30 | @Path("repo") repo: String |
takahirom | 727331c | 2018-03-04 14:02:32 +0900 | [diff] [blame] | 31 | ): Single<List<Contributor>> |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 32 | |
| 33 | @GET("users/{user}/repos") |
takahirom | 727331c | 2018-03-04 14:02:32 +0900 | [diff] [blame] | 34 | fun listRepos(@Path("user") user: String): Single<List<Repo>> |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | data class Contributor(val login: String, val contributions: Int) |
| 38 | data class Repo(val name: String) |
| 39 | |
Roman Elizarov | 00560e8 | 2018-03-05 17:58:59 +0300 | [diff] [blame] | 40 | fun main(args: Array<String>) = runBlocking { |
| 41 | println("Making GitHub API request") |
| 42 | |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 43 | val retrofit = Retrofit.Builder().apply { |
| 44 | baseUrl("https://api.github.com") |
| 45 | addConverterFactory(GsonConverterFactory.create()) |
takahirom | 727331c | 2018-03-04 14:02:32 +0900 | [diff] [blame] | 46 | addCallAdapterFactory(RxJava2CallAdapterFactory.create()) |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 47 | }.build() |
| 48 | |
| 49 | val github = retrofit.create(GitHub::class.java) |
| 50 | |
Roman Elizarov | 00560e8 | 2018-03-05 17:58:59 +0300 | [diff] [blame] | 51 | val contributors = |
| 52 | github.contributors("JetBrains", "Kotlin") |
| 53 | .await().take(10) |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 54 | |
Roman Elizarov | 00560e8 | 2018-03-05 17:58:59 +0300 | [diff] [blame] | 55 | for ((name, contributions) in contributors) { |
| 56 | println("$name has $contributions contributions, other repos: ") |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 57 | |
Roman Elizarov | 00560e8 | 2018-03-05 17:58:59 +0300 | [diff] [blame] | 58 | val otherRepos = |
| 59 | github.listRepos(name).await() |
| 60 | .map(Repo::name).joinToString(", ") |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 61 | |
Roman Elizarov | 00560e8 | 2018-03-05 17:58:59 +0300 | [diff] [blame] | 62 | println(otherRepos) |
Denis Zharkov | f013267 | 2016-07-06 18:55:34 +0300 | [diff] [blame] | 63 | } |
| 64 | } |