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

Removes khttp #92

Merged
merged 3 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ publishing {

dependencies {
implementation(kotlin("reflect"))
implementation("khttp:khttp:1.0.0")
implementation("com.google.code.gson:gson:2.8.6")
implementation("org.awaitility:awaitility-kotlin:4.0.3")
implementation("com.github.psxpaul:gradle-execfork-plugin:0.2.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.springdoc.openapi.gradle.plugin

import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import khttp.responses.Response
import org.awaitility.Durations
import org.awaitility.core.ConditionTimeoutException
import org.awaitility.kotlin.*
Expand All @@ -15,6 +14,8 @@ import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import java.net.ConnectException
import java.net.HttpURLConnection
import java.net.URL
import java.time.Duration
import java.time.temporal.ChronoUnit.SECONDS

Expand Down Expand Up @@ -63,19 +64,26 @@ open class OpenApiGeneratorTask : DefaultTask() {

private fun generateApiDocs(url: String, fileName: String) {
try {
val isYaml = url.toLowerCase().matches(Regex(".+[./]yaml(/.+)*"))
await ignoreException ConnectException::class withPollInterval Durations.ONE_SECOND atMost Duration.of(
waitTimeInSeconds.get().toLong(),
SECONDS
) until {
val statusCode = khttp.get(url).statusCode
val connection: HttpURLConnection = URL(url).openConnection() as HttpURLConnection
connection.requestMethod = "GET"
connection.connect()
val statusCode = connection.responseCode
logger.trace("apiDocsUrl = {} status code = {}", url, statusCode)
statusCode < MAX_HTTP_STATUS_CODE
}
logger.info("Generating OpenApi Docs..")
val response: Response = khttp.get(url)
val connection: HttpURLConnection = URL(url).openConnection() as HttpURLConnection
connection.requestMethod = "GET"
connection.connect()

val isYaml = url.toLowerCase().matches(Regex(".+[./]yaml(/.+)*"))
val apiDocs = if (isYaml) response.text else prettifyJson(response)
val response = String(connection.inputStream.readAllBytes(), Charsets.UTF_8)

val apiDocs = if (isYaml) response else prettifyJson(response)

val outputFile = outputDir.file(fileName).get().asFile
outputFile.writeText(apiDocs)
Expand All @@ -85,9 +93,9 @@ open class OpenApiGeneratorTask : DefaultTask() {
}
}

private fun prettifyJson(response: Response): String {
private fun prettifyJson(response: String): String {
val gson = GsonBuilder().setPrettyPrinting().create()
val googleJsonObject = gson.fromJson(response.text, JsonObject::class.java)
val googleJsonObject = gson.fromJson(response, JsonObject::class.java)
return gson.toJson(googleJsonObject)
}
}