Skip to content

Commit

Permalink
HTTP client impr
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Jun 16, 2021
1 parent 0aea4d7 commit beb4fbd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.cognifide.gradle.common.http.HttpClient
import com.cognifide.gradle.common.net.NetUtils
import com.cognifide.gradle.common.utils.Formats
import org.apache.http.HttpStatus
import org.apache.http.client.methods.HttpRequestBase
import java.util.concurrent.TimeUnit

class HealthChecker(val common: CommonExtension) {
Expand All @@ -18,7 +19,7 @@ class HealthChecker(val common: CommonExtension) {

private val checks = mutableListOf<HealthCheck>()

private var httpOptions: HttpClient.() -> Unit = {
private var httpClientOptions: HttpClient.() -> Unit = {
authorizationPreemptive.set(true)
connectionRetries.apply {
convention(false)
Expand All @@ -30,6 +31,10 @@ class HealthChecker(val common: CommonExtension) {
}
}

private var httpRequestOptions: HttpRequestBase.() -> Unit = {
prop.string("healthChecker.http.userAgent")?.let { addHeader("user-agent", it) }
}

val verbose = common.obj.boolean {
convention(true)
common.prop.boolean("healthChecker.verbose")?.let { set(it) }
Expand Down Expand Up @@ -159,12 +164,12 @@ class HealthChecker(val common: CommonExtension) {
fun http(checkName: String, url: String, criteria: HttpCheck.() -> Unit) = check(checkName) {
var result: Any? = null
common.http {
apply(httpOptions)
apply(httpClientOptions)

val check = HttpCheck(url).apply(criteria)
apply(check.options)

request(check.method, check.url) { response ->
request(check.method, check.url, httpRequestOptions) { response ->
result = "${check.method} ${check.url} -> ${response.statusLine}"
check.checks.forEach { it(response) }
}
Expand All @@ -176,7 +181,7 @@ class HealthChecker(val common: CommonExtension) {
var result: Any? = null
common.http {
val check = HttpCheck(url).apply(criteria)
apply(httpOptions)
apply(httpClientOptions)
apply(check.options)
var responds = false
try {
Expand Down Expand Up @@ -211,7 +216,11 @@ class HealthChecker(val common: CommonExtension) {

// Default options

fun httpOptions(options: HttpClient.() -> Unit) {
this.httpOptions = options
fun httpClient(options: HttpClient.() -> Unit) {
this.httpClientOptions = options
}

fun httpRequest(options: HttpRequestBase.() -> Unit) {
this.httpRequestOptions = options
}
}
16 changes: 9 additions & 7 deletions src/main/kotlin/com/cognifide/gradle/common/http/HttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,15 @@ open class HttpClient(private val common: CommonExtension) {

private var responseChecker: (HttpResponse) -> Unit = { checkStatus(it) }

fun <T> request(method: String, uri: String, handler: HttpClient.(HttpResponse) -> T) = when (method.toLowerCase()) {
"get" -> get(uri, handler)
"post" -> post(uri, handler)
"put" -> put(uri, handler)
"patch" -> patch(uri, handler)
"head" -> head(uri, handler)
"delete" -> delete(uri, handler)
fun <T> request(method: String, uri: String, handler: HttpClient.(HttpResponse) -> T) = request(method, uri, {}, handler)

fun <T> request(method: String, uri: String, options: HttpRequestBase.() -> Unit, handler: HttpClient.(HttpResponse) -> T) = when (method.toLowerCase()) {
"get" -> get(uri, handler, options)
"post" -> post(uri, handler, options)
"put" -> put(uri, handler, options)
"patch" -> patch(uri, handler, options)
"head" -> head(uri, handler, options)
"delete" -> delete(uri, handler, options)
else -> throw RequestException("Invalid HTTP client method: '$method'")
}

Expand Down

0 comments on commit beb4fbd

Please sign in to comment.