Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kotlin coroutines sample #3500

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions samples/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'java-library'
apply plugin: 'org.jetbrains.kotlin.jvm'

dependencies {
implementation project(':retrofit')
Expand All @@ -10,5 +11,6 @@ dependencies {
implementation deps.mockwebserver
implementation deps.guava
implementation deps.jsoup
implementation deps.kotlinCoroutines
compileOnly deps.findBugsAnnotations
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.retrofit

import kotlinx.coroutines.runBlocking
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path

class Contributor(val login: String, val contributions: Int)

interface GitHub {
@GET("/repos/{owner}/{repo}/contributors")
suspend fun contributors(@Path("owner") owner: String?, @Path("repo") repo: String?): List<Contributor>
}

fun main() {
// Create a very simple REST adapter which points the GitHub API.
val retrofit = Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.build()

// Create an instance of our GitHub API interface.
val github = retrofit.create(GitHub::class.java)

runBlocking {
// Fetch and print a list of the contributors to the library.
val contributors = github.contributors("square", "retrofit")
contributors.forEach { contributor ->
println(contributor.login + " (" + contributor.contributions + ")")
}
}
}