blob: e8849484eca431a28560bffa91b46545cd6625d2 [file] [log] [blame]
Roman Elizarovf16fd272017-02-07 11:26:00 +03001/*
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
takahirom727331c2018-03-04 14:02:32 +090017import io.reactivex.Single
Roman Elizarov00560e82018-03-05 17:58:59 +030018import kotlinx.coroutines.experimental.*
takahirom727331c2018-03-04 14:02:32 +090019import kotlinx.coroutines.experimental.rx2.await
Denis Zharkovf0132672016-07-06 18:55:34 +030020import retrofit2.Retrofit
takahirom727331c2018-03-04 14:02:32 +090021import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
Denis Zharkovf0132672016-07-06 18:55:34 +030022import retrofit2.converter.gson.GsonConverterFactory
23import retrofit2.http.GET
24import retrofit2.http.Path
Denis Zharkovf0132672016-07-06 18:55:34 +030025
26interface GitHub {
27 @GET("/repos/{owner}/{repo}/contributors")
28 fun contributors(
29 @Path("owner") owner: String,
30 @Path("repo") repo: String
takahirom727331c2018-03-04 14:02:32 +090031 ): Single<List<Contributor>>
Denis Zharkovf0132672016-07-06 18:55:34 +030032
33 @GET("users/{user}/repos")
takahirom727331c2018-03-04 14:02:32 +090034 fun listRepos(@Path("user") user: String): Single<List<Repo>>
Denis Zharkovf0132672016-07-06 18:55:34 +030035}
36
37data class Contributor(val login: String, val contributions: Int)
38data class Repo(val name: String)
39
Roman Elizarov00560e82018-03-05 17:58:59 +030040fun main(args: Array<String>) = runBlocking {
41 println("Making GitHub API request")
42
Denis Zharkovf0132672016-07-06 18:55:34 +030043 val retrofit = Retrofit.Builder().apply {
44 baseUrl("https://api.github.com")
45 addConverterFactory(GsonConverterFactory.create())
takahirom727331c2018-03-04 14:02:32 +090046 addCallAdapterFactory(RxJava2CallAdapterFactory.create())
Denis Zharkovf0132672016-07-06 18:55:34 +030047 }.build()
48
49 val github = retrofit.create(GitHub::class.java)
50
Roman Elizarov00560e82018-03-05 17:58:59 +030051 val contributors =
52 github.contributors("JetBrains", "Kotlin")
53 .await().take(10)
Denis Zharkovf0132672016-07-06 18:55:34 +030054
Roman Elizarov00560e82018-03-05 17:58:59 +030055 for ((name, contributions) in contributors) {
56 println("$name has $contributions contributions, other repos: ")
Denis Zharkovf0132672016-07-06 18:55:34 +030057
Roman Elizarov00560e82018-03-05 17:58:59 +030058 val otherRepos =
59 github.listRepos(name).await()
60 .map(Repo::name).joinToString(", ")
Denis Zharkovf0132672016-07-06 18:55:34 +030061
Roman Elizarov00560e82018-03-05 17:58:59 +030062 println(otherRepos)
Denis Zharkovf0132672016-07-06 18:55:34 +030063 }
64}