Repository navigation
Expand file tree
/
Copy pathGitHubExample.kt
More file actions
135 lines (122 loc) · 4.51 KB
/
Copy pathGitHubExample.kt
File metadata and controls
135 lines (122 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright © 2012 The Feign Authors (feign@commonhaus.dev)
*
* 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.
*/
package example.github
import feign.Logger
import feign.Logger.ErrorLogger
import feign.Param
import feign.Request
import feign.RequestLine
import feign.Response
import feign.codec.Decoder
import feign.codec.Encoder
import feign.codec.ErrorDecoder
import feign.gson.GsonEncoder
import feign.kotlin.CoroutineFeign
import java.io.IOException
import java.util.concurrent.TimeUnit
suspend fun main() {
val github = GitHub.connect()
println("Let's fetch and print a list of the contributors to this org.")
val contributors = github.contributors("openfeign")
for (contributor in contributors) {
println(contributor)
}
println("Now, let's cause an error.")
try {
github.contributors("openfeign", "some-unknown-project")
} catch (e: GitHubClientError) {
println(e.message)
}
println("Now, try to create an issue - which will also cause an error.")
try {
val issue = GitHub.Issue(
title = "The title",
body = "Some Text",
)
github.createIssue(issue, "OpenFeign", "SomeRepo")
} catch (e: GitHubClientError) {
println(e.message)
}
}
/**
* Inspired by `com.example.retrofit.GitHubClient`
*/
interface GitHub {
data class Repository(
val name: String
)
data class Contributor(
val login: String
)
data class Issue(
val title: String,
val body: String,
val assignees: List<String> = emptyList(),
val milestone: Int = 0,
val labels: List<String> = emptyList(),
)
@RequestLine("GET /users/{username}/repos?sort=full_name")
suspend fun repos(@Param("username") owner: String): List<Repository>
@RequestLine("GET /repos/{owner}/{repo}/contributors")
suspend fun contributors(@Param("owner") owner: String, @Param("repo") repo: String): List<Contributor>
@RequestLine("POST /repos/{owner}/{repo}/issues")
suspend fun createIssue(issue: Issue, @Param("owner") owner: String, @Param("repo") repo: String)
companion object {
fun connect(): GitHub {
val decoder: Decoder = feign.gson.GsonDecoder()
val encoder: Encoder = GsonEncoder()
return CoroutineFeign.builder<Unit>()
.encoder(encoder)
.decoder(decoder)
.errorDecoder(GitHubErrorDecoder(decoder))
.logger(ErrorLogger())
.logLevel(Logger.Level.BASIC)
.requestInterceptor { template ->
template.header(
// not available when building PRs...
// https://docs.travis-ci.com/user/environment-variables/#defining-encrypted-variables-in-travisyml
"Authorization",
"token 383f1c1b474d8f05a21e7964976ab0d403fee071");
}
.options(Request.Options(10, TimeUnit.SECONDS, 60, TimeUnit.SECONDS, true))
.target(GitHub::class.java, "https://api.github.com")
}
}
}
/** Lists all contributors for all repos owned by a user. */
suspend fun GitHub.contributors(owner: String): List<String> {
return repos(owner)
.flatMap { contributors(owner, it.name) }
.map { it.login }
.distinct()
}
internal class GitHubClientError() : RuntimeException() {
override val message: String? = null
}
internal class GitHubErrorDecoder(
private val decoder: Decoder
) : ErrorDecoder {
private val defaultDecoder: ErrorDecoder = ErrorDecoder.Default()
override fun decode(methodKey: String, response: Response): Exception {
return try {
// must replace status by 200 other GSONDecoder returns null
val response = response.toBuilder().status(200).build()
decoder.decode(response, GitHubClientError::class.java) as Exception
} catch (fallbackToDefault: IOException) {
defaultDecoder.decode(methodKey, response)
}
}
}