Skip to content

Commit

Permalink
feat: update code
Browse files Browse the repository at this point in the history
  • Loading branch information
anssari1 committed May 28, 2024
1 parent 44bf03c commit 3f32fe2
Show file tree
Hide file tree
Showing 296 changed files with 6,151 additions and 4,099 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Expedia, Inc.
* Copyright (C) 2022 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,15 +32,14 @@ import io.ktor.client.engine.HttpClientEngine
abstract class BaseRapidClient(
namespace: String,
clientConfiguration: RapidClientConfiguration,
httpClientEngine: HttpClientEngine = DEFAULT_HTTP_CLIENT_ENGINE,
httpClientEngine: HttpClientEngine = DEFAULT_HTTP_CLIENT_ENGINE
) : Client(namespace) {
private val _configurationProvider: ConfigurationProvider =
ConfigurationCollector.create(
clientConfiguration.toProvider(),
RapidConfigurationProvider,
RapidConfigurationProvider
)
private val _httpClient: HttpClient =
buildHttpClient(_configurationProvider, AuthenticationStrategy.AuthenticationType.SIGNATURE, httpClientEngine)
private val _httpClient: HttpClient = buildHttpClient(_configurationProvider, AuthenticationStrategy.AuthenticationType.SIGNATURE, httpClientEngine)

init {
finalize()
Expand Down
67 changes: 15 additions & 52 deletions code/src/main/kotlin/com/expediagroup/sdk/core/client/Client.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Expedia, Inc.
* Copyright (C) 2022 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -63,7 +63,7 @@ val DEFAULT_HTTP_CLIENT_ENGINE: HttpClientEngine =
*/
abstract class Client(
namespace: String,
environmentProvider: EnvironmentProvider = DefaultEnvironmentProvider(namespace),
environmentProvider: EnvironmentProvider = DefaultEnvironmentProvider(namespace)
) : EnvironmentProvider by environmentProvider {
private val httpHandler = DefaultHttpHandler(environmentProvider)

Expand All @@ -80,7 +80,7 @@ abstract class Client(
internal fun buildHttpClient(
configurationProvider: ConfigurationProvider,
authenticationType: AuthenticationStrategy.AuthenticationType,
httpClientEngine: HttpClientEngine = DEFAULT_HTTP_CLIENT_ENGINE,
httpClientEngine: HttpClientEngine = DEFAULT_HTTP_CLIENT_ENGINE
): HttpClient =
HttpClient(httpClientEngine) {
val httpClientConfig = this
Expand All @@ -89,18 +89,9 @@ abstract class Client(
val secret: String = configurationProvider.secret ?: fireMissingConfigurationIssue(ConfigurationName.SECRET)
val endpoint: String = configurationProvider.endpoint ?: fireMissingConfigurationIssue(ConfigurationName.ENDPOINT)
val authEndpoint: String = configurationProvider.authEndpoint ?: fireMissingConfigurationIssue(ConfigurationName.AUTH_ENDPOINT)
val requestTimeout: Long =
configurationProvider.requestTimeout ?: fireMissingConfigurationIssue(
ConfigurationName.REQUEST_TIMEOUT_MILLIS,
)
val connectionTimeout: Long =
configurationProvider.connectionTimeout ?: fireMissingConfigurationIssue(
ConfigurationName.CONNECTION_TIMEOUT_MILLIS,
)
val socketTimeout: Long =
configurationProvider.socketTimeout ?: fireMissingConfigurationIssue(
ConfigurationName.SOCKET_TIMEOUT_MILLIS,
)
val requestTimeout: Long = configurationProvider.requestTimeout ?: fireMissingConfigurationIssue(ConfigurationName.REQUEST_TIMEOUT_MILLIS)
val connectionTimeout: Long = configurationProvider.connectionTimeout ?: fireMissingConfigurationIssue(ConfigurationName.CONNECTION_TIMEOUT_MILLIS)
val socketTimeout: Long = configurationProvider.socketTimeout ?: fireMissingConfigurationIssue(ConfigurationName.SOCKET_TIMEOUT_MILLIS)
val maskedLoggingHeaders: Set<String> = configurationProvider.maskedLoggingHeaders ?: setOf()
val maskedLoggingBodyFields: Set<String> = configurationProvider.maskedLoggingBodyFields ?: setOf()

Expand All @@ -109,7 +100,7 @@ abstract class Client(
httpClientConfig,
Credentials.from(key, secret),
authEndpoint,
authenticationType,
authenticationType
)

plugins {
Expand All @@ -118,9 +109,7 @@ abstract class Client(
use(AuthenticationPlugin).with(authenticationConfiguration)
use(DefaultRequestPlugin).with(DefaultRequestConfiguration.from(httpClientConfig, endpoint))
use(EncodingPlugin).with(EncodingConfiguration.from(httpClientConfig))
use(
HttpTimeoutPlugin,
).with(HttpTimeoutConfiguration.from(httpClientConfig, requestTimeout, connectionTimeout, socketTimeout))
use(HttpTimeoutPlugin).with(HttpTimeoutConfiguration.from(httpClientConfig, requestTimeout, connectionTimeout, socketTimeout))
use(ExceptionHandlingPlugin).with(ExceptionHandlingConfiguration.from(httpClientConfig))
}

Expand All @@ -130,8 +119,7 @@ abstract class Client(
}

/** Throw an exception if the configuration is missing. */
private fun fireMissingConfigurationIssue(configurationKey: String): Nothing =
throw ExpediaGroupConfigurationException(getMissingRequiredConfigurationMessage(configurationKey))
private fun fireMissingConfigurationIssue(configurationKey: String): Nothing = throw ExpediaGroupConfigurationException(getMissingRequiredConfigurationMessage(configurationKey))

private fun isNotSuccessfulResponse(response: HttpResponse) = response.status.value !in Constant.SUCCESSFUL_STATUS_CODES_RANGE

Expand All @@ -145,7 +133,7 @@ abstract class Client(

abstract suspend fun throwServiceException(
response: HttpResponse,
operationId: String,
operationId: String
)

suspend fun performGet(url: String): HttpResponse = httpHandler.performGet(httpClient, url)
Expand Down Expand Up @@ -237,12 +225,7 @@ abstract class Client(
*/
fun requestTimeout(milliseconds: Long): SELF {
this.requestTimeout = milliseconds
log.info(
LoggingMessageProvider.getRuntimeConfigurationProviderMessage(
ConfigurationName.REQUEST_TIMEOUT_MILLIS,
milliseconds.toString(),
),
)
log.info(LoggingMessageProvider.getRuntimeConfigurationProviderMessage(ConfigurationName.REQUEST_TIMEOUT_MILLIS, milliseconds.toString()))
return self()
}

Expand All @@ -256,12 +239,7 @@ abstract class Client(
*/
fun connectionTimeout(milliseconds: Long): SELF {
this.connectionTimeout = milliseconds
log.info(
LoggingMessageProvider.getRuntimeConfigurationProviderMessage(
ConfigurationName.CONNECTION_TIMEOUT_MILLIS,
milliseconds.toString(),
),
)
log.info(LoggingMessageProvider.getRuntimeConfigurationProviderMessage(ConfigurationName.CONNECTION_TIMEOUT_MILLIS, milliseconds.toString()))
return self()
}

Expand All @@ -275,12 +253,7 @@ abstract class Client(
*/
fun socketTimeout(milliseconds: Long): SELF {
this.socketTimeout = milliseconds
log.info(
LoggingMessageProvider.getRuntimeConfigurationProviderMessage(
ConfigurationName.SOCKET_TIMEOUT_MILLIS,
milliseconds.toString(),
),
)
log.info(LoggingMessageProvider.getRuntimeConfigurationProviderMessage(ConfigurationName.SOCKET_TIMEOUT_MILLIS, milliseconds.toString()))
return self()
}

Expand All @@ -292,12 +265,7 @@ abstract class Client(
*/
fun maskedLoggingHeaders(vararg headers: String): SELF {
this.maskedLoggingHeaders = headers.toSet()
log.info(
LoggingMessageProvider.getRuntimeConfigurationProviderMessage(
ConfigurationName.MASKED_LOGGING_HEADERS,
headers.joinToString(),
),
)
log.info(LoggingMessageProvider.getRuntimeConfigurationProviderMessage(ConfigurationName.MASKED_LOGGING_HEADERS, headers.joinToString()))
return self()
}

Expand All @@ -309,12 +277,7 @@ abstract class Client(
*/
fun maskedLoggingBodyFields(vararg fields: String): SELF {
this.maskedLoggingBodyFields = fields.toSet()
log.info(
LoggingMessageProvider.getRuntimeConfigurationProviderMessage(
ConfigurationName.MASKED_LOGGING_BODY_FIELDS,
fields.joinToString(),
),
)
log.info(LoggingMessageProvider.getRuntimeConfigurationProviderMessage(ConfigurationName.MASKED_LOGGING_BODY_FIELDS, fields.joinToString()))
return self()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Expedia, Inc.
* Copyright (C) 2022 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,5 +17,5 @@ package com.expediagroup.sdk.core.client

/** Handy utils and helpers for a client. */
abstract class ClientHelpers(
val client: Client,
val client: Client
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Expedia, Inc.
* Copyright (C) 2022 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,16 +17,16 @@ package com.expediagroup.sdk.core.client

import com.expediagroup.sdk.core.constant.HeaderKey
import com.expediagroup.sdk.core.model.Properties
import com.expediagroup.sdk.core.model.TransactionId
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.http.HttpHeaders
import java.util.UUID

interface EnvironmentProvider {
fun HttpRequestBuilder.appendHeaders()
fun HttpRequestBuilder.appendHeaders(transactionId: TransactionId = TransactionId())
}

class DefaultEnvironmentProvider(
namespace: String,
namespace: String
) : EnvironmentProvider {
private val properties = Properties.from(javaClass.classLoader.getResource("sdk.properties")!!)
private val javaVersion = System.getProperty("java.version")
Expand All @@ -35,11 +35,11 @@ class DefaultEnvironmentProvider(
private val userAgent = "expediagroup-sdk-java-$namespace/${properties["sdk-version"]!!} (Java $javaVersion; $operatingSystemName $operatingSystemVersion)"

@Suppress("MemberVisibilityCanBePrivate")
override fun HttpRequestBuilder.appendHeaders() {
override fun HttpRequestBuilder.appendHeaders(transactionId: TransactionId) {
with(headers) {
append(HttpHeaders.UserAgent, userAgent)
append(HeaderKey.X_SDK_TITLE, properties["sdk-title"]!!)
append(HeaderKey.TRANSACTION_ID, UUID.randomUUID().toString())
append(HeaderKey.TRANSACTION_ID, transactionId.dequeue().toString())
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Expedia, Inc.
* Copyright (C) 2022 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,15 +32,14 @@ import io.ktor.client.engine.HttpClientEngine
abstract class ExpediaGroupClient(
namespace: String,
clientConfiguration: ExpediaGroupClientConfiguration,
httpClientEngine: HttpClientEngine = DEFAULT_HTTP_CLIENT_ENGINE,
httpClientEngine: HttpClientEngine = DEFAULT_HTTP_CLIENT_ENGINE
) : Client(namespace) {
private val _configurationProvider: ConfigurationProvider =
ConfigurationCollector.create(
clientConfiguration.toProvider(),
ExpediaGroupConfigurationProvider,
ExpediaGroupConfigurationProvider
)
private val _httpClient: HttpClient =
buildHttpClient(_configurationProvider, AuthenticationStrategy.AuthenticationType.BEARER, httpClientEngine)
private val _httpClient: HttpClient = buildHttpClient(_configurationProvider, AuthenticationStrategy.AuthenticationType.BEARER, httpClientEngine)

init {
finalize()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Expedia, Inc.
* Copyright (C) 2022 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,16 +24,16 @@ import io.ktor.http.HttpMethod
internal interface HttpHandler {
suspend fun performGet(
httpClient: HttpClient,
link: String,
link: String
): HttpResponse
}

internal class DefaultHttpHandler(
private val environmentProvider: EnvironmentProvider,
private val environmentProvider: EnvironmentProvider
) : HttpHandler, EnvironmentProvider by environmentProvider {
override suspend fun performGet(
httpClient: HttpClient,
link: String,
link: String
): HttpResponse {
return httpClient.request {
method = HttpMethod.Get
Expand Down
Loading

0 comments on commit 3f32fe2

Please sign in to comment.