Skip to content

Commit

Permalink
Revert "Use Kotlin-generated client/model in Billing" (#12297)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroslopez committed Apr 30, 2024
1 parent 4ba83d5 commit 2e3377d
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.slf4j.LoggerFactory;

/**
* DEPRECATED. USE {@link io.airbyte.api.client2.AirbyteApiClient}.
* DEPRECATED. USE {@link io.airbyte.api.client2.AirbyteApiClient2}.
* <p>
* This class is meant to consolidate all our API endpoints into a fluent-ish client. Currently, all
* open API generators create a separate class per API "root-route". For example, if our API has two
Expand All @@ -47,7 +47,7 @@
* <p>
* This is currently manually maintained. We could look into autogenerating it if needed.
*
* @deprecated Replaced by {@link io.airbyte.api.client2.AirbyteApiClient}
* @deprecated Replaced by {@link io.airbyte.api.client2.AirbyteApiClient2}
*/
@Deprecated
public class AirbyteApiClient {
Expand Down Expand Up @@ -196,7 +196,7 @@ public WorkspaceApi getWorkspaceApi() {
}

/**
* DEPRECATED: Use {@link io.airbyte.api.client2.AirbyteApiClient} instead.
* DEPRECATED: Use {@link io.airbyte.api.client2.AirbyteApiClient2} instead.
* <p>
* Default to 4 retries with a randomised 1 - 10 seconds interval between the first two retries and
* an 10-minute wait for the last retry.
Expand All @@ -207,15 +207,15 @@ public WorkspaceApi getWorkspaceApi() {
* @param desc short readable explanation of why this method is executed
* @param <T> type of return type
* @return value returned by method
* @deprecated replaced by {@link io.airbyte.api.client2.AirbyteApiClient}
* @deprecated replaced by {@link io.airbyte.api.client2.AirbyteApiClient2}
*/
@Deprecated
public static <T> T retryWithJitter(final Callable<T> call, final String desc) {
return retryWithJitter(call, desc, DEFAULT_RETRY_INTERVAL_SECS, DEFAULT_FINAL_INTERVAL_SECS, DEFAULT_MAX_RETRIES);
}

/**
* DEPRECATED: Use {@link io.airbyte.api.client2.AirbyteApiClient} instead.
* DEPRECATED: Use {@link io.airbyte.api.client2.AirbyteApiClient2} instead.
* <p>
* Provides a simple retry wrapper for api calls. This retry behaviour is slightly different from
* generally available retries libraries - the last retry is able to wait an interval inconsistent
Expand All @@ -231,7 +231,7 @@ public static <T> T retryWithJitter(final Callable<T> call, final String desc) {
* @param desc short readable explanation of why this method is executed
* @param jitterMaxIntervalSecs upper limit of the randomised retry interval. Minimum value is 1.
* @param finalIntervalSecs retry interval before the last retry.
* @deprecated replaced by {@link io.airbyte.api.client2.AirbyteApiClient}
* @deprecated replaced by {@link io.airbyte.api.client2.AirbyteApiClient2}
*/
@Deprecated
@VisibleForTesting
Expand All @@ -252,7 +252,7 @@ public static <T> T retryWithJitter(final Callable<T> call,
}

/**
* DEPRECATED: Use {@link io.airbyte.api.client2.AirbyteApiClient} instead.
* DEPRECATED: Use {@link io.airbyte.api.client2.AirbyteApiClient2} instead.
* <p>
* Default to 4 retries with a randomised 1 - 10 seconds interval between the first two retries and
* an 10-minute wait for the last retry.
Expand All @@ -262,15 +262,15 @@ public static <T> T retryWithJitter(final Callable<T> call,
* @param <T> type of return type
* @return value returned by method
* @throws Exception exception while jittering
* @deprecated replaced by {@link io.airbyte.api.client2.AirbyteApiClient}
* @deprecated replaced by {@link io.airbyte.api.client2.AirbyteApiClient2}
*/
@Deprecated
public static <T> T retryWithJitterThrows(final Callable<T> call, final String desc) throws Exception {
return retryWithJitterThrows(call, desc, DEFAULT_RETRY_INTERVAL_SECS, DEFAULT_FINAL_INTERVAL_SECS, DEFAULT_MAX_RETRIES);
}

/**
* DEPRECATED: Use {@link io.airbyte.api.client2.AirbyteApiClient} instead.
* DEPRECATED: Use {@link io.airbyte.api.client2.AirbyteApiClient2} instead.
* <p>
* Provides a simple retry wrapper for api calls. This retry behaviour is slightly different from
* generally available retries libraries - the last retry is able to wait an interval inconsistent
Expand All @@ -284,7 +284,7 @@ public static <T> T retryWithJitterThrows(final Callable<T> call, final String d
* @param desc short readable explanation of why this method is executed
* @param jitterMaxIntervalSecs upper limit of the randomised retry interval. Minimum value is 1.
* @param finalIntervalSecs retry interval before the last retry.
* @deprecated replaced by {@link io.airbyte.api.client2.AirbyteApiClient}
* @deprecated replaced by {@link io.airbyte.api.client2.AirbyteApiClient2}
*/
@VisibleForTesting
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import okhttp3.Response
@Suppress("MemberVisibilityCanBePrivate")
@Singleton
@Requires(property = "airbyte.internal-api.base-path")
class AirbyteApiClient
class AirbyteApiClient2
@JvmOverloads
constructor(
@Value("\${airbyte.internal-api.base-path}") basePath: String,
Expand Down
15 changes: 0 additions & 15 deletions airbyte-api/src/main/kotlin/auth/AirbyteApiInterceptor.kt

This file was deleted.

11 changes: 8 additions & 3 deletions airbyte-api/src/main/kotlin/auth/AirbyteAuthHeaderInterceptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

package io.airbyte.api.client.auth

import jakarta.inject.Named
import io.micronaut.context.annotation.Value
import io.micronaut.http.HttpHeaders
import jakarta.inject.Singleton
import okhttp3.Interceptor
import okhttp3.Request
Expand All @@ -15,14 +16,18 @@ import java.util.Optional
* Adds a custom Airbyte authentication header to requests made by a client.
*/
@Singleton
@Named("airbyteAuthHeaderInterceptor")
class AirbyteAuthHeaderInterceptor(
private val authHeaders: Optional<AirbyteAuthHeader>,
) : AirbyteApiInterceptor {
@Value("\${micronaut.application.name}") private val userAgent: String,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest: Request = chain.request()
val builder: Request.Builder = originalRequest.newBuilder()

if (originalRequest.header(HttpHeaders.USER_AGENT) == null) {
builder.addHeader(HttpHeaders.USER_AGENT, formatUserAgent(userAgent))
}

authHeaders.ifPresent { h -> builder.addHeader(h.getHeaderName(), h.getHeaderValue()) }

return chain.proceed(builder.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,40 @@

package io.airbyte.api.client.auth

import com.google.common.base.CaseFormat
import io.github.oshai.kotlinlogging.KotlinLogging
import io.micronaut.context.annotation.Value
import jakarta.inject.Named
import io.micronaut.http.HttpHeaders
import jakarta.inject.Singleton
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response

private val logger = KotlinLogging.logger {}

fun formatUserAgent(userAgent: String): String {
return CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, userAgent)
}

@Singleton
@Named("internalApiAuthenticationInterceptor")
class InternalApiAuthenticationInterceptor(
@Value("\${airbyte.internal-api.auth-header.name}") private val authHeaderName: String,
@Value("\${airbyte.internal-api.auth-header.value}") private val authHeaderValue: String,
) : AirbyteApiInterceptor {
@Value("\${micronaut.application.name}") private val userAgent: String,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest: Request = chain.request()
val builder: Request.Builder = originalRequest.newBuilder()

if (originalRequest.header(HttpHeaders.USER_AGENT) == null) {
builder.addHeader(HttpHeaders.USER_AGENT, formatUserAgent(userAgent))
}

if (authHeaderName.isNotBlank() && authHeaderValue.isNotBlank()) {
logger.debug { "Adding authorization header..." }
builder.addHeader(authHeaderName, authHeaderValue)
} else {
logger.debug { "Authorization header/value not provided." }
logger.debug { "Bearer token not provided." }
}

return chain.proceed(builder.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package io.airbyte.api.client.auth
import io.github.oshai.kotlinlogging.KotlinLogging
import io.micronaut.context.annotation.Value
import io.micronaut.http.HttpHeaders
import jakarta.inject.Named
import jakarta.inject.Singleton
import okhttp3.Interceptor
import okhttp3.Request
Expand All @@ -17,14 +16,18 @@ import java.util.Base64
private val logger = KotlinLogging.logger {}

@Singleton
@Named("workloadApiAuthenticationInterceptor")
class WorkloadApiAuthenticationInterceptor(
@Value("\${airbyte.workload-api.bearer-token}") private val bearerToken: String,
@Value("\${micronaut.application.name}") private val userAgent: String,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest: Request = chain.request()
val builder: Request.Builder = originalRequest.newBuilder()

if (originalRequest.header(HttpHeaders.USER_AGENT) == null) {
builder.addHeader(HttpHeaders.USER_AGENT, formatUserAgent(userAgent))
}

if (bearerToken.isNotBlank()) {
logger.debug { "Adding authorization header..." }
val encodedBearerToken = Base64.getEncoder().encodeToString(bearerToken.toByteArray())
Expand Down
3 changes: 1 addition & 2 deletions airbyte-api/src/main/kotlin/client/ThrowOn5xxInterceptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package io.airbyte.api.client

import io.airbyte.api.client.auth.AirbyteApiInterceptor
import io.micronaut.context.annotation.Requires
import io.micronaut.http.HttpStatus
import jakarta.inject.Named
Expand All @@ -16,7 +15,7 @@ import java.io.IOException
@Singleton
@Named("throwOn5xxInterceptor")
@Requires(property = "airbyte.internal.throws-on-5xx", value = "true", defaultValue = "true")
class ThrowOn5xxInterceptor : AirbyteApiInterceptor {
class ThrowOn5xxInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val response = chain.proceed(chain.request())
if (response.code >= HttpStatus.INTERNAL_SERVER_ERROR.code) {
Expand Down
36 changes: 0 additions & 36 deletions airbyte-api/src/main/kotlin/client/UserAgentInterceptor.kt

This file was deleted.

18 changes: 10 additions & 8 deletions airbyte-api/src/main/kotlin/config/ClientSupportFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
package io.airbyte.api.client.config

import dev.failsafe.RetryPolicy
import io.airbyte.api.client.UserAgentInterceptor
import io.airbyte.api.client.auth.AirbyteApiInterceptor
import io.airbyte.api.client.ThrowOn5xxInterceptor
import io.airbyte.api.client.auth.AirbyteAuthHeaderInterceptor
import io.airbyte.api.client.auth.InternalApiAuthenticationInterceptor
import io.airbyte.api.client.auth.WorkloadApiAuthenticationInterceptor
import io.github.oshai.kotlinlogging.KotlinLogging
import io.micrometer.core.instrument.MeterRegistry
Expand Down Expand Up @@ -46,10 +46,14 @@ class ClientSupportFactory {
fun defaultAirbyteApiOkHttpClient(
@Value("\${airbyte.internal-api.connect-timeout-seconds}") connectTimeoutSeconds: Long,
@Value("\${airbyte.internal-api.read-timeout-seconds}") readTimeoutSeconds: Long,
interceptors: List<AirbyteApiInterceptor>,
internalApiAuthenticationInterceptor: InternalApiAuthenticationInterceptor,
airbyteAuthHeaderInterceptor: AirbyteAuthHeaderInterceptor,
@Named("throwOn5xxInterceptor") throwOn5xxInterceptor: Optional<ThrowOn5xxInterceptor>,
): OkHttpClient {
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
interceptors.forEach(builder::addInterceptor)
builder.addInterceptor(internalApiAuthenticationInterceptor)
builder.addInterceptor(airbyteAuthHeaderInterceptor)
throwOn5xxInterceptor.ifPresent { builder.addInterceptor(it) }
builder.readTimeout(Duration.ofSeconds(readTimeoutSeconds))
builder.connectTimeout(Duration.ofSeconds(connectTimeoutSeconds))
return builder.build()
Expand All @@ -72,14 +76,12 @@ class ClientSupportFactory {
fun defaultWorkloadApiOkHttpClient(
@Value("\${airbyte.workload-api.connect-timeout-seconds}") connectTimeoutSeconds: Long,
@Value("\${airbyte.workload-api.read-timeout-seconds}") readTimeoutSeconds: Long,
@Named("workloadApiAuthenticationInterceptor") workloadApiAuthenticationInterceptor: WorkloadApiAuthenticationInterceptor,
@Named("userAgentInterceptor") userAgentInterceptor: UserAgentInterceptor,
@Named("airbyteAuthHeaderInterceptor") airbyteAuthHeaderInterceptor: AirbyteAuthHeaderInterceptor,
workloadApiAuthenticationInterceptor: WorkloadApiAuthenticationInterceptor,
airbyteAuthHeaderInterceptor: AirbyteAuthHeaderInterceptor,
): OkHttpClient {
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
builder.addInterceptor(workloadApiAuthenticationInterceptor)
builder.addInterceptor(airbyteAuthHeaderInterceptor)
builder.addInterceptor(userAgentInterceptor)
builder.readTimeout(Duration.ofSeconds(readTimeoutSeconds))
builder.connectTimeout(Duration.ofSeconds(connectTimeoutSeconds))
return builder.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AirbyteApiClientTest {
val client: OkHttpClient = mockk()
val policy: RetryPolicy<Response> = mockk()

val airbyteApiClient = AirbyteApiClient(basePath, policy, client)
val airbyteApiClient = AirbyteApiClient2(basePath, policy, client)
assertNotNull(airbyteApiClient.attemptApi)
assertEquals(client, airbyteApiClient.attemptApi.client)
assertEquals(policy, airbyteApiClient.attemptApi.policy)
Expand Down

This file was deleted.

Loading

0 comments on commit 2e3377d

Please sign in to comment.