User: joe Date: 15 Nov 26 06:46 Revision: d9788239160383d194f5784f1dc2640ef14cc912 Summary: Use McdevHttp in annotater TeamCity URL: http://ci.mcdev.io:80/viewModification.html?tab=vcsModificationFiles&modId=10790&personal=false Index: src/main/kotlin/platform/fabric/annotater/github/AnnotaterGitHubPullRequests.kt =================================================================== --- src/main/kotlin/platform/fabric/annotater/github/AnnotaterGitHubPullRequests.kt (revision ab9b113818031149dd375e4440236b96bc90b475) +++ src/main/kotlin/platform/fabric/annotater/github/AnnotaterGitHubPullRequests.kt (revision d9788239160383d194f5784f1dc2640ef14cc912) @@ -26,6 +26,7 @@ import com.demonwav.mcdev.platform.fabric.annotater.AnnotaterPullRequests import com.demonwav.mcdev.platform.fabric.annotater.git.AnnotaterGitVcs import com.demonwav.mcdev.platform.fabric.annotater.javadoc.AnnotaterJavadocContributions +import com.demonwav.mcdev.util.McdevHttp import com.google.gson.Gson import com.google.gson.JsonElement import com.google.gson.JsonObject @@ -272,10 +273,14 @@ private class GitHubApi(private val baseUrl: String, private val token: String) { private val gson = Gson() + private val headers = mapOf( + "Authorization" to "Bearer $token", + "Accept" to "application/vnd.github+json", + "X-GitHub-Api-Version" to "2022-11-28", + ) fun post(path: String, body: JsonObject): JsonObject { - return HttpRequests.post(baseUrl + path, "application/json") - .tuner(::authorize) + return McdevHttp.post(baseUrl + path, "application/json", headers) .throwStatusCodeException(false) .connect { request -> request.write(gson.toJson(body)) @@ -284,8 +289,7 @@ } fun get(path: String): JsonElement { - return HttpRequests.request(baseUrl + path) - .tuner(::authorize) + return McdevHttp.request(baseUrl + path, headers) .throwStatusCodeException(false) .connect { request -> JsonParser.parseString(readResponse(request)) } } @@ -333,13 +337,7 @@ }.orEmpty() return if (details.isEmpty()) message else "$message: ${details.joinToString("; ")}" } - - private fun authorize(connection: java.net.URLConnection) { - connection.setRequestProperty("Authorization", "Bearer $token") - connection.setRequestProperty("Accept", "application/vnd.github+json") - connection.setRequestProperty("X-GitHub-Api-Version", "2022-11-28") - } + } - } companion object { private const val FORK_POLL_ATTEMPTS = 30 Index: src/main/kotlin/util/McdevHttp.kt =================================================================== --- src/main/kotlin/util/McdevHttp.kt (revision ab9b113818031149dd375e4440236b96bc90b475) +++ src/main/kotlin/util/McdevHttp.kt (revision d9788239160383d194f5784f1dc2640ef14cc912) @@ -65,9 +65,19 @@ return "$base/$authority$path$query" } - fun request(url: String, headers: Map = emptyMap()): RequestBuilder { + fun request(url: String, headers: Map = emptyMap()): RequestBuilder = + build(url, headers) { HttpRequests.request(it) } + + fun post(url: String, contentType: String, headers: Map = emptyMap()): RequestBuilder = + build(url, headers) { HttpRequests.post(it, contentType) } + + private inline fun build( + url: String, + headers: Map, + create: (String) -> RequestBuilder, + ): RequestBuilder { val resolved = resolve(url) - val builder = HttpRequests.request(resolved).userAgent(PluginUtil.useragent) + val builder = create(resolved).userAgent(PluginUtil.useragent) if (resolved != url) { builder.useProxy(false) }