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

Added support to specify request timeout and enable logging of resposes #105

Merged
merged 1 commit into from
Oct 7, 2023
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- now in `upload` and `export-to-md` commands you can enable logging of http requests/responses and configure request
timeout

### Changed

- dependency updates:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package com.github.zeldigas.text2confl.cli

import com.github.ajalt.clikt.core.ParameterHolder
import com.github.ajalt.clikt.core.PrintMessage
import com.github.ajalt.clikt.parameters.options.convert
import com.github.ajalt.clikt.parameters.options.help
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.options.*
import com.github.ajalt.clikt.parameters.types.enum
import com.github.ajalt.clikt.parameters.types.file
import com.github.ajalt.clikt.parameters.types.long
import com.github.zeldigas.confclient.ConfluenceAuth
import com.github.zeldigas.confclient.ConfluenceClientConfig
import com.github.zeldigas.confclient.PasswordAuth
import com.github.zeldigas.confclient.TokenAuth
import com.github.zeldigas.text2confl.convert.EditorVersion
import io.ktor.client.plugins.logging.*
import io.ktor.http.*

fun ParameterHolder.confluenceUrl() = option(
Expand Down Expand Up @@ -51,12 +51,24 @@ fun ParameterHolder.docsLocation() = option("--docs")
.file(canBeFile = true, canBeDir = true).required()
.help("File or directory with files to convert")

fun ParameterHolder.httpLoggingLevel() = option(
"--http-request-logging",
help = "Logging level for http requests. Useful for debugging purposes. Turned off by default"
).enum<LogLevel>().default(LogLevel.NONE)

fun ParameterHolder.httpRequestTimeout() = option(
"--http-request-timeout",
help = "Http request timeout in milliseconds. Default "
).long()

internal interface WithConfluenceServerOptions {
val confluenceUrl: Url?
val confluenceUser: String?
val confluencePassword: String?
val accessToken: String?
val skipSsl: Boolean?
val httpLogLevel: LogLevel
val httpRequestTimeout: Long?

val confluenceAuth: ConfluenceAuth
get() = when {
Expand All @@ -76,6 +88,13 @@ internal interface WithConfluenceServerOptions {
return PasswordAuth(username, effectivePassword)
}

fun httpClientConfig(server: Url, defaultSslSkip: Boolean = false) = ConfluenceClientConfig(
server = server,
skipSsl = skipSsl ?: defaultSslSkip,
auth = confluenceAuth,
httpLogLevel = httpLogLevel,
requestTimeout = httpRequestTimeout
)
fun askForSecret(prompt: String, requireConfirmation: Boolean = true): String?
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.file
import com.github.zeldigas.confclient.ConfluenceClientConfig
import com.github.zeldigas.text2confl.core.ServiceProvider
import io.ktor.client.plugins.logging.*
import io.ktor.http.*
import kotlinx.coroutines.runBlocking
import java.io.File
Expand All @@ -20,6 +20,9 @@ class DumpToMarkdown : CliktCommand(name = "export-to-md", help = "Exports confl
override val confluencePassword: String? by confluencePassword()
override val accessToken: String? by accessToken()
override val skipSsl: Boolean? by skipSsl()
override val httpLogLevel: LogLevel by httpLoggingLevel()
override val httpRequestTimeout: Long? by httpRequestTimeout()

val space: String? by confluenceSpace()
private val pageId: String? by option("--page-id", help = "Id of page that you want to dump")
private val pageTitle: String? by option(
Expand Down Expand Up @@ -47,11 +50,7 @@ class DumpToMarkdown : CliktCommand(name = "export-to-md", help = "Exports confl
}

private suspend fun dumpPage() {
val clientConfig = ConfluenceClientConfig(
confluenceUrl,
skipSsl ?: true,
confluenceAuth
)
val clientConfig = httpClientConfig(confluenceUrl)

val client = serviceProvider.createConfluenceClient(clientConfig, false)
val pageExporter = serviceProvider.createPageExporter(client, saveContentSource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.github.zeldigas.text2confl.convert.EditorVersion
import com.github.zeldigas.text2confl.core.ServiceProvider
import com.github.zeldigas.text2confl.core.config.*
import com.github.zeldigas.text2confl.core.upload.ChangeDetector
import io.ktor.client.plugins.logging.*
import io.ktor.http.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
Expand All @@ -27,6 +28,8 @@ class Upload : CliktCommand(name = "upload", help = "Converts source files and u
override val confluencePassword: String? by confluencePassword()
override val accessToken: String? by accessToken()
override val skipSsl: Boolean? by skipSsl()
override val httpLogLevel: LogLevel by httpLoggingLevel()
override val httpRequestTimeout: Long? by httpRequestTimeout()

override val spaceKey: String? by confluenceSpace()
private val parentId: String? by option("--parent-id", help = "Id of parent page where root pages should be added")
Expand Down Expand Up @@ -110,11 +113,7 @@ class Upload : CliktCommand(name = "upload", help = "Converts source files and u
val server = confluenceUrl ?: configuration.server?.let { Url(it) }
?: parameterMissing("Confluence url", "--confluence-url", "server")

return ConfluenceClientConfig(
server = server,
skipSsl = skipSsl ?: configuration.skipSsl,
auth = confluenceAuth
)
return httpClientConfig(server, configuration.skipSsl)
}

private fun passwordAuth(username: String, password: String?): PasswordAuth {
Expand Down
4 changes: 4 additions & 0 deletions confluence-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
<groupId>io.ktor</groupId>
<artifactId>ktor-client-auth-jvm</artifactId>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-client-logging-jvm</artifactId>
</dependency>

<dependency>
<groupId>io.github.oshai</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.zeldigas.confclient

import io.ktor.client.plugins.logging.*
import io.ktor.http.*

/**
Expand All @@ -8,5 +9,7 @@ import io.ktor.http.*
data class ConfluenceClientConfig(
val server: Url,
val skipSsl: Boolean,
val auth: ConfluenceAuth
val auth: ConfluenceAuth,
val httpLogLevel: LogLevel = LogLevel.NONE,
val requestTimeout: Long? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.auth.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import io.ktor.client.request.forms.*
import io.ktor.client.statement.*
Expand Down Expand Up @@ -327,8 +328,11 @@ fun confluenceClient(
config: ConfluenceClientConfig
): ConfluenceClient {
val client = HttpClient(CIO) {
if (config.skipSsl) {
engine {
engine {
if (config.requestTimeout != null) {
requestTimeout = config.requestTimeout
}
if (config.skipSsl) {
https {
trustManager = object : X509TrustManager {
override fun getAcceptedIssuers(): Array<X509Certificate>? = null
Expand All @@ -353,6 +357,14 @@ fun confluenceClient(
install(UserAgent) {
agent = "text2confl"
}
if (config.httpLogLevel != LogLevel.NONE) {
install(Logging) {
logger = Logger.DEFAULT
level = config.httpLogLevel
sanitizeHeader { header -> header == HttpHeaders.Authorization }
}
}

}

return ConfluenceClientImpl(config.server, "${config.server}/rest/api", client)
Expand Down
2 changes: 2 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

<!--test deps below-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down