Vladimir
06/03/2024, 4:21 PMobject HttpUtils {
val aastorage = AcceptAllCookiesStorage()
val client = HttpClient(CIO) {
expectSuccess = true
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
})
}
install(HttpCookies) {
storage = aastorage
}
install(Logging) {
logger = Logger.SIMPLE
}
}
private val config = ConfigLoader.getConfig()
suspend fun installCookies(cookies: List<Cookie>) {
cookies.forEach {
aastorage.addCookie(
Url(config.base_url), Cookie(
name = it.name,
value = it.value,
maxAge = it.maxAge,
expires = it.expires,
domain = it.domain,
path = it.path,
secure = it.secure,
httpOnly = it.httpOnly,
extensions = it.extensions,
encoding = it.encoding
)
)
}
}
suspend inline fun get(request: HttpGetRequest): Result<HttpResponse> {
return withContext(Dispatchers.IO) {
try {
val response: HttpResponse = client.get(request.url)
Result.success(response)
} catch (e: Exception) {
Result.failure(e)
}
}
}
suspend inline fun <reified P> post(request: HttpPostRequest<P>): Result<HttpResponse> {
return withContext(Dispatchers.IO) {
try {
val response: HttpResponse = client.post(request.url){
contentType(ContentType.Application.Json)
setBody(request.payload)
}
Result.success(response)
} catch (e: Exception) {
Result.failure(e)
}
}
}
suspend inline fun <reified P> patch(request: HttpPatchRequest<P>): Result<HttpResponse> {
return withContext(Dispatchers.IO) {
try {
val response: HttpResponse = client.patch(request.url){
contentType(ContentType.Application.Json)
setBody(request.payload)
}
Result.success(response)
} catch (e: Exception) {
Result.failure(e)
}
}
}
}
This singleton is then used by my repository classes.
Could you kindly suggest any recommended strategies to pinpoint and address this issue? Thank youChrimaeon
06/03/2024, 4:28 PMAcceptAllHeadersStorage
is just In-Memory and if the app is in background, it might get killed and you loose the cookies.Vladimir
06/03/2024, 4:31 PMChrimaeon
06/03/2024, 4:33 PMVladimir
06/03/2024, 4:34 PMChrimaeon
06/03/2024, 4:37 PM