Skip to content

Commit

Permalink
PIA-1889: Add timeout parameter to status request (#11)
Browse files Browse the repository at this point in the history
* PIA-1889: Add timeout parameter to status request

* PIA-1889: Change requestTimeoutMillis to non-nullable type

* PIA-1889: Adapt iOS and tvOS to also have parameterizable API request timeouts
  • Loading branch information
kp-aldo-pedromingo authored Jun 4, 2024
1 parent b9c4fd7 commit d841496
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion account/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
@OptIn(org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi::class)
kotlin {
group = "com.kape.android"
version = "1.4.6"
version = "1.4.7"

// Enable the default target hierarchy.
// It's a template for all possible targets and their shared source sets hardcoded in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ internal actual object AccountHttpClient {

actual fun client(
certificate: String?,
pinnedEndpoint: Pair<String, String>?
pinnedEndpoint: Pair<String, String>?,
requestTimeoutMillis: Long
) : Pair<HttpClient?, Exception?> {
var httpClient: HttpClient? = null
var exception: Exception? = null
try {
httpClient = HttpClient(OkHttp) {
expectSuccess = false
install(HttpTimeout) {
requestTimeoutMillis = Account.REQUEST_TIMEOUT_MS
this.requestTimeoutMillis = requestTimeoutMillis
}

if (certificate != null && pinnedEndpoint != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.privateinternetaccess.account

import com.privateinternetaccess.account.internals.Account
import com.privateinternetaccess.account.internals.AndroidAccount
import com.privateinternetaccess.account.internals.IOSAccount
import com.privateinternetaccess.account.model.request.AmazonSignupInformation
Expand Down Expand Up @@ -109,9 +110,13 @@ public interface AccountAPI {
)

/**
* @param requestTimeoutMillis `Long`
* @param callback `(status: ClientStatusInformation?, error: List<AccountRequestError>) -> Unit`
*/
fun clientStatus(callback: (status: ClientStatusInformation?, error: List<AccountRequestError>) -> Unit)
fun clientStatus(
requestTimeoutMillis: Long = Account.REQUEST_TIMEOUT_MS,
callback: (status: ClientStatusInformation?, error: List<AccountRequestError>) -> Unit,
)

/**
* @param email `String`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ internal expect object AccountHttpClient {
*/
fun client(
certificate: String? = null,
pinnedEndpoint: Pair<String, String>? = null
pinnedEndpoint: Pair<String, String>? = null,
requestTimeoutMillis: Long = Account.REQUEST_TIMEOUT_MS
): Pair<HttpClient?, Exception?>
}

Expand Down Expand Up @@ -221,10 +222,12 @@ internal open class Account(
}

override fun clientStatus(
requestTimeoutMillis: Long,
callback: (status: ClientStatusInformation?, error: List<AccountRequestError>) -> Unit

) {
launch {
clientStatusAsync(endpointsProvider.accountEndpoints(), callback)
clientStatusAsync(endpointsProvider.accountEndpoints(), callback, requestTimeoutMillis)
}
}

Expand Down Expand Up @@ -967,7 +970,8 @@ internal open class Account(

private suspend fun clientStatusAsync(
endpoints: List<AccountEndpoint>,
callback: (status: ClientStatusInformation?, error: List<AccountRequestError>) -> Unit
callback: (status: ClientStatusInformation?, error: List<AccountRequestError>) -> Unit,
requestTimeoutMillis: Long
) {
var clientStatus: ClientStatusInformation? = null
val listErrors: MutableList<AccountRequestError> = mutableListOf()
Expand All @@ -994,9 +998,9 @@ internal open class Account(
}

val httpClientConfigResult = if (endpoint.usePinnedCertificate) {
AccountHttpClient.client(certificate, Pair(endpoint.ipOrRootDomain, endpoint.certificateCommonName!!))
AccountHttpClient.client(certificate, Pair(endpoint.ipOrRootDomain, endpoint.certificateCommonName!!), requestTimeoutMillis)
} else {
AccountHttpClient.client()
AccountHttpClient.client(requestTimeoutMillis = requestTimeoutMillis)
}

val httpClient = httpClientConfigResult.first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.ktor.client.engine.darwin.*
import io.ktor.client.engine.ios.Ios
import io.ktor.client.plugins.*
import io.ktor.client.engine.ios.*
import io.ktor.client.request.request
import kotlinx.cinterop.*
import platform.CoreFoundation.*
import platform.Foundation.*
Expand All @@ -33,12 +34,13 @@ internal actual object AccountHttpClient {

actual fun client(
certificate: String?,
pinnedEndpoint: Pair<String, String>?
pinnedEndpoint: Pair<String, String>?,
requestTimeoutMillis: Long
): Pair<HttpClient?, Exception?> {
return Pair(HttpClient(Darwin) {
expectSuccess = false
install(HttpTimeout) {
requestTimeoutMillis = Account.REQUEST_TIMEOUT_MS
this.requestTimeoutMillis = requestTimeoutMillis
}

if (certificate != null && pinnedEndpoint != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ internal actual object AccountHttpClient {

actual fun client(
certificate: String?,
pinnedEndpoint: Pair<String, String>?
pinnedEndpoint: Pair<String, String>?,
requestTimeoutMillis: Long
): Pair<HttpClient?, Exception?> {
return Pair(HttpClient(Darwin) {
expectSuccess = false
install(HttpTimeout) {
requestTimeoutMillis = Account.REQUEST_TIMEOUT_MS
this.requestTimeoutMillis = requestTimeoutMillis
}

if (certificate != null && pinnedEndpoint != null) {
Expand Down

0 comments on commit d841496

Please sign in to comment.