Skip to content

Commit

Permalink
feat(client): add User-Agent header (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and Stainless Bot committed Nov 1, 2024
1 parent 3865552 commit 9f537ed
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package com.lithic.api.client

import com.lithic.api.core.ClientOptions
import com.lithic.api.core.RequestOptions
import com.lithic.api.core.getPackageVersion
import com.lithic.api.core.handlers.errorHandler
import com.lithic.api.core.handlers.jsonHandler
import com.lithic.api.core.handlers.withErrorHandler
Expand All @@ -19,90 +20,117 @@ constructor(
private val clientOptions: ClientOptions,
) : LithicClientAsync {

private val clientOptionsWithUserAgent =
if (clientOptions.headers.containsKey("User-Agent")) clientOptions
else
clientOptions
.toBuilder()
.putHeader("User-Agent", "${javaClass.simpleName}/Kotlin ${getPackageVersion()}")
.build()

private val errorHandler: Handler<LithicError> = errorHandler(clientOptions.jsonMapper)

// Pass the original clientOptions so that this client sets its own User-Agent.
private val sync: LithicClient by lazy { LithicClientImpl(clientOptions) }

private val accounts: AccountServiceAsync by lazy { AccountServiceAsyncImpl(clientOptions) }
private val accounts: AccountServiceAsync by lazy {
AccountServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val accountHolders: AccountHolderServiceAsync by lazy {
AccountHolderServiceAsyncImpl(clientOptions)
AccountHolderServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val authRules: AuthRuleServiceAsync by lazy { AuthRuleServiceAsyncImpl(clientOptions) }
private val authRules: AuthRuleServiceAsync by lazy {
AuthRuleServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val authStreamEnrollment: AuthStreamEnrollmentServiceAsync by lazy {
AuthStreamEnrollmentServiceAsyncImpl(clientOptions)
AuthStreamEnrollmentServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val tokenizationDecisioning: TokenizationDecisioningServiceAsync by lazy {
TokenizationDecisioningServiceAsyncImpl(clientOptions)
TokenizationDecisioningServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val tokenizations: TokenizationServiceAsync by lazy {
TokenizationServiceAsyncImpl(clientOptions)
TokenizationServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val cards: CardServiceAsync by lazy { CardServiceAsyncImpl(clientOptions) }
private val cards: CardServiceAsync by lazy { CardServiceAsyncImpl(clientOptionsWithUserAgent) }

private val balances: BalanceServiceAsync by lazy { BalanceServiceAsyncImpl(clientOptions) }
private val balances: BalanceServiceAsync by lazy {
BalanceServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val aggregateBalances: AggregateBalanceServiceAsync by lazy {
AggregateBalanceServiceAsyncImpl(clientOptions)
AggregateBalanceServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val disputes: DisputeServiceAsync by lazy { DisputeServiceAsyncImpl(clientOptions) }
private val disputes: DisputeServiceAsync by lazy {
DisputeServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val events: EventServiceAsync by lazy { EventServiceAsyncImpl(clientOptions) }
private val events: EventServiceAsync by lazy {
EventServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val transfers: TransferServiceAsync by lazy { TransferServiceAsyncImpl(clientOptions) }
private val transfers: TransferServiceAsync by lazy {
TransferServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val financialAccounts: FinancialAccountServiceAsync by lazy {
FinancialAccountServiceAsyncImpl(clientOptions)
FinancialAccountServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val transactions: TransactionServiceAsync by lazy {
TransactionServiceAsyncImpl(clientOptions)
TransactionServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val responderEndpoints: ResponderEndpointServiceAsync by lazy {
ResponderEndpointServiceAsyncImpl(clientOptions)
ResponderEndpointServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val webhooks: WebhookServiceAsync by lazy { WebhookServiceAsyncImpl(clientOptions) }

private val externalBankAccounts: ExternalBankAccountServiceAsync by lazy {
ExternalBankAccountServiceAsyncImpl(clientOptions)
ExternalBankAccountServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val payments: PaymentServiceAsync by lazy { PaymentServiceAsyncImpl(clientOptions) }
private val payments: PaymentServiceAsync by lazy {
PaymentServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val threeDS: ThreeDSServiceAsync by lazy { ThreeDSServiceAsyncImpl(clientOptions) }
private val threeDS: ThreeDSServiceAsync by lazy {
ThreeDSServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val reports: ReportServiceAsync by lazy { ReportServiceAsyncImpl(clientOptions) }
private val reports: ReportServiceAsync by lazy {
ReportServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val cardPrograms: CardProgramServiceAsync by lazy {
CardProgramServiceAsyncImpl(clientOptions)
CardProgramServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val digitalCardArt: DigitalCardArtServiceAsync by lazy {
DigitalCardArtServiceAsyncImpl(clientOptions)
DigitalCardArtServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val bookTransfers: BookTransferServiceAsync by lazy {
BookTransferServiceAsyncImpl(clientOptions)
BookTransferServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val creditProducts: CreditProductServiceAsync by lazy {
CreditProductServiceAsyncImpl(clientOptions)
CreditProductServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val externalPayments: ExternalPaymentServiceAsync by lazy {
ExternalPaymentServiceAsyncImpl(clientOptions)
ExternalPaymentServiceAsyncImpl(clientOptionsWithUserAgent)
}

private val managementOperations: ManagementOperationServiceAsync by lazy {
ManagementOperationServiceAsyncImpl(clientOptions)
ManagementOperationServiceAsyncImpl(clientOptionsWithUserAgent)
}

override fun sync(): LithicClient = sync
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package com.lithic.api.client

import com.lithic.api.core.ClientOptions
import com.lithic.api.core.RequestOptions
import com.lithic.api.core.getPackageVersion
import com.lithic.api.core.handlers.errorHandler
import com.lithic.api.core.handlers.jsonHandler
import com.lithic.api.core.handlers.withErrorHandler
Expand All @@ -19,86 +20,103 @@ constructor(
private val clientOptions: ClientOptions,
) : LithicClient {

private val clientOptionsWithUserAgent =
if (clientOptions.headers.containsKey("User-Agent")) clientOptions
else
clientOptions
.toBuilder()
.putHeader("User-Agent", "${javaClass.simpleName}/Kotlin ${getPackageVersion()}")
.build()

private val errorHandler: Handler<LithicError> = errorHandler(clientOptions.jsonMapper)

// Pass the original clientOptions so that this client sets its own User-Agent.
private val async: LithicClientAsync by lazy { LithicClientAsyncImpl(clientOptions) }

private val accounts: AccountService by lazy { AccountServiceImpl(clientOptions) }
private val accounts: AccountService by lazy { AccountServiceImpl(clientOptionsWithUserAgent) }

private val accountHolders: AccountHolderService by lazy {
AccountHolderServiceImpl(clientOptions)
AccountHolderServiceImpl(clientOptionsWithUserAgent)
}

private val authRules: AuthRuleService by lazy { AuthRuleServiceImpl(clientOptions) }
private val authRules: AuthRuleService by lazy {
AuthRuleServiceImpl(clientOptionsWithUserAgent)
}

private val authStreamEnrollment: AuthStreamEnrollmentService by lazy {
AuthStreamEnrollmentServiceImpl(clientOptions)
AuthStreamEnrollmentServiceImpl(clientOptionsWithUserAgent)
}

private val tokenizationDecisioning: TokenizationDecisioningService by lazy {
TokenizationDecisioningServiceImpl(clientOptions)
TokenizationDecisioningServiceImpl(clientOptionsWithUserAgent)
}

private val tokenizations: TokenizationService by lazy {
TokenizationServiceImpl(clientOptions)
TokenizationServiceImpl(clientOptionsWithUserAgent)
}

private val cards: CardService by lazy { CardServiceImpl(clientOptions) }
private val cards: CardService by lazy { CardServiceImpl(clientOptionsWithUserAgent) }

private val balances: BalanceService by lazy { BalanceServiceImpl(clientOptions) }
private val balances: BalanceService by lazy { BalanceServiceImpl(clientOptionsWithUserAgent) }

private val aggregateBalances: AggregateBalanceService by lazy {
AggregateBalanceServiceImpl(clientOptions)
AggregateBalanceServiceImpl(clientOptionsWithUserAgent)
}

private val disputes: DisputeService by lazy { DisputeServiceImpl(clientOptions) }
private val disputes: DisputeService by lazy { DisputeServiceImpl(clientOptionsWithUserAgent) }

private val events: EventService by lazy { EventServiceImpl(clientOptions) }
private val events: EventService by lazy { EventServiceImpl(clientOptionsWithUserAgent) }

private val transfers: TransferService by lazy { TransferServiceImpl(clientOptions) }
private val transfers: TransferService by lazy {
TransferServiceImpl(clientOptionsWithUserAgent)
}

private val financialAccounts: FinancialAccountService by lazy {
FinancialAccountServiceImpl(clientOptions)
FinancialAccountServiceImpl(clientOptionsWithUserAgent)
}

private val transactions: TransactionService by lazy { TransactionServiceImpl(clientOptions) }
private val transactions: TransactionService by lazy {
TransactionServiceImpl(clientOptionsWithUserAgent)
}

private val responderEndpoints: ResponderEndpointService by lazy {
ResponderEndpointServiceImpl(clientOptions)
ResponderEndpointServiceImpl(clientOptionsWithUserAgent)
}

private val webhooks: WebhookService by lazy { WebhookServiceImpl(clientOptions) }

private val externalBankAccounts: ExternalBankAccountService by lazy {
ExternalBankAccountServiceImpl(clientOptions)
ExternalBankAccountServiceImpl(clientOptionsWithUserAgent)
}

private val payments: PaymentService by lazy { PaymentServiceImpl(clientOptions) }
private val payments: PaymentService by lazy { PaymentServiceImpl(clientOptionsWithUserAgent) }

private val threeDS: ThreeDSService by lazy { ThreeDSServiceImpl(clientOptions) }
private val threeDS: ThreeDSService by lazy { ThreeDSServiceImpl(clientOptionsWithUserAgent) }

private val reports: ReportService by lazy { ReportServiceImpl(clientOptions) }
private val reports: ReportService by lazy { ReportServiceImpl(clientOptionsWithUserAgent) }

private val cardPrograms: CardProgramService by lazy { CardProgramServiceImpl(clientOptions) }
private val cardPrograms: CardProgramService by lazy {
CardProgramServiceImpl(clientOptionsWithUserAgent)
}

private val digitalCardArt: DigitalCardArtService by lazy {
DigitalCardArtServiceImpl(clientOptions)
DigitalCardArtServiceImpl(clientOptionsWithUserAgent)
}

private val bookTransfers: BookTransferService by lazy {
BookTransferServiceImpl(clientOptions)
BookTransferServiceImpl(clientOptionsWithUserAgent)
}

private val creditProducts: CreditProductService by lazy {
CreditProductServiceImpl(clientOptions)
CreditProductServiceImpl(clientOptionsWithUserAgent)
}

private val externalPayments: ExternalPaymentService by lazy {
ExternalPaymentServiceImpl(clientOptions)
ExternalPaymentServiceImpl(clientOptionsWithUserAgent)
}

private val managementOperations: ManagementOperationService by lazy {
ManagementOperationServiceImpl(clientOptions)
ManagementOperationServiceImpl(clientOptionsWithUserAgent)
}

override fun async(): LithicClientAsync = async
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ private constructor(
val headers: ListMultimap<String, String>,
val queryParams: ListMultimap<String, String>,
val responseValidation: Boolean,
val maxRetries: Int,
) {

fun toBuilder() = Builder().from(this)

companion object {

const val PRODUCTION_URL = "https://api.lithic.com"
Expand All @@ -46,6 +49,25 @@ private constructor(
private var apiKey: String? = null
private var webhookSecret: String? = null

internal fun from(clientOptions: ClientOptions) = apply {
httpClient = clientOptions.httpClient
jsonMapper = clientOptions.jsonMapper
clock = clientOptions.clock
baseUrl = clientOptions.baseUrl
headers =
clientOptions.headers.asMap().mapValuesTo(mutableMapOf()) { (_, value) ->
value.toMutableList()
}
queryParams =
clientOptions.queryParams.asMap().mapValuesTo(mutableMapOf()) { (_, value) ->
value.toMutableList()
}
responseValidation = clientOptions.responseValidation
maxRetries = clientOptions.maxRetries
apiKey = clientOptions.apiKey
webhookSecret = clientOptions.webhookSecret
}

fun httpClient(httpClient: HttpClient) = apply { this.httpClient = httpClient }

fun jsonMapper(jsonMapper: JsonMapper) = apply { this.jsonMapper = jsonMapper }
Expand Down Expand Up @@ -141,6 +163,7 @@ private constructor(
headers.toUnmodifiable(),
queryParams.toUnmodifiable(),
responseValidation,
maxRetries,
)
}
}
Expand Down

0 comments on commit 9f537ed

Please sign in to comment.