blob: d0105adba1e4cfb9fd139a07eec24287cb3d3583 [file] [log] [blame]
Roman Elizarovf16fd272017-02-07 11:26:00 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarovf16fd272017-02-07 11:26:00 +03003 */
4
takahirom727331c2018-03-04 14:02:32 +09005import io.reactivex.Single
Roman Elizarov00560e82018-03-05 17:58:59 +03006import kotlinx.coroutines.experimental.*
takahirom727331c2018-03-04 14:02:32 +09007import kotlinx.coroutines.experimental.rx2.await
Denis Zharkovf0132672016-07-06 18:55:34 +03008import retrofit2.Retrofit
takahirom727331c2018-03-04 14:02:32 +09009import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
Denis Zharkovf0132672016-07-06 18:55:34 +030010import retrofit2.converter.gson.GsonConverterFactory
11import retrofit2.http.GET
12import retrofit2.http.Path
Denis Zharkovf0132672016-07-06 18:55:34 +030013
14interface GitHub {
15 @GET("/repos/{owner}/{repo}/contributors")
16 fun contributors(
17 @Path("owner") owner: String,
18 @Path("repo") repo: String
takahirom727331c2018-03-04 14:02:32 +090019 ): Single<List<Contributor>>
Denis Zharkovf0132672016-07-06 18:55:34 +030020
21 @GET("users/{user}/repos")
takahirom727331c2018-03-04 14:02:32 +090022 fun listRepos(@Path("user") user: String): Single<List<Repo>>
Denis Zharkovf0132672016-07-06 18:55:34 +030023}
24
25data class Contributor(val login: String, val contributions: Int)
26data class Repo(val name: String)
27
Roman Elizarov00560e82018-03-05 17:58:59 +030028fun main(args: Array<String>) = runBlocking {
29 println("Making GitHub API request")
30
Denis Zharkovf0132672016-07-06 18:55:34 +030031 val retrofit = Retrofit.Builder().apply {
32 baseUrl("https://api.github.com")
33 addConverterFactory(GsonConverterFactory.create())
takahirom727331c2018-03-04 14:02:32 +090034 addCallAdapterFactory(RxJava2CallAdapterFactory.create())
Denis Zharkovf0132672016-07-06 18:55:34 +030035 }.build()
36
37 val github = retrofit.create(GitHub::class.java)
38
Roman Elizarov00560e82018-03-05 17:58:59 +030039 val contributors =
40 github.contributors("JetBrains", "Kotlin")
41 .await().take(10)
Denis Zharkovf0132672016-07-06 18:55:34 +030042
Roman Elizarov00560e82018-03-05 17:58:59 +030043 for ((name, contributions) in contributors) {
44 println("$name has $contributions contributions, other repos: ")
Denis Zharkovf0132672016-07-06 18:55:34 +030045
Roman Elizarov00560e82018-03-05 17:58:59 +030046 val otherRepos =
47 github.listRepos(name).await()
48 .map(Repo::name).joinToString(", ")
Denis Zharkovf0132672016-07-06 18:55:34 +030049
Roman Elizarov00560e82018-03-05 17:58:59 +030050 println(otherRepos)
Denis Zharkovf0132672016-07-06 18:55:34 +030051 }
52}