blob: e8849484eca431a28560bffa91b46545cd6625d2 [file] [log] [blame]
/*
* Copyright 2016-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import io.reactivex.Single
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.rx2.await
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path
interface GitHub {
@GET("/repos/{owner}/{repo}/contributors")
fun contributors(
@Path("owner") owner: String,
@Path("repo") repo: String
): Single<List<Contributor>>
@GET("users/{user}/repos")
fun listRepos(@Path("user") user: String): Single<List<Repo>>
}
data class Contributor(val login: String, val contributions: Int)
data class Repo(val name: String)
fun main(args: Array<String>) = runBlocking {
println("Making GitHub API request")
val retrofit = Retrofit.Builder().apply {
baseUrl("https://api.github.com")
addConverterFactory(GsonConverterFactory.create())
addCallAdapterFactory(RxJava2CallAdapterFactory.create())
}.build()
val github = retrofit.create(GitHub::class.java)
val contributors =
github.contributors("JetBrains", "Kotlin")
.await().take(10)
for ((name, contributions) in contributors) {
println("$name has $contributions contributions, other repos: ")
val otherRepos =
github.listRepos(name).await()
.map(Repo::name).joinToString(", ")
println(otherRepos)
}
}