Контекстуальная ясность: Если значение “authentication” используется только в нескольких местах и его смысл очевиден из контекста, вынесение в константу может быть излишним и даже увеличить сложность восприятия кода.
@Deprecated ( "Use HttpClient.newHttpClient instead" )
fun newHttpClient (baseUrl: String , options: Map < String , Any >): CloseableHttpClient {
val builder = HttpClients. custom (). useSystemProperties ()
. setRetryStrategy ( DefaultHttpRequestRetryStrategy ( 5 , TimeValue. ofMilliseconds ( 3000 )))
when {
options[ "authentication" ] is Auth -> {
when ( val auth = options[ "authentication" ] as Auth) {
is Auth.BasicAuthentication -> basicAuth (baseUrl, auth.username, auth.password, builder)
is Auth.BearerAuthentication -> {
builder. setDefaultHeaders ( listOf ( BasicHeader (auth.headerName, "Bearer " + auth.token)))
}
else -> {}
}
}
options[ "authentication" ] is List <*> -> {
val authentication = options[ "authentication" ] as List <*>
when ( val scheme = authentication. first (). toString (). toLowerCase ()) {
"basic" -> {
if (authentication.size > 2 ) {
basicAuth (baseUrl, authentication[ 1 ]. toString (), authentication[ 2 ]. toString (), builder)
} else {
logger. warn { "Basic authentication requires a username and password, ignoring." }
}
}
else -> logger. warn { "Only supports basic authentication, got ' $scheme ', ignoring." }
}
}
options. containsKey ( "authentication" ) -> {
logger. warn { "Authentication options needs to be a Auth class or a list of values, " +
"got '${options["authentication"]}', ignoring." }
}
}
return builder. build ()
}
development principle clean code