diff --git a/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt b/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt index f9e55e6103995216d4ae8e0665ae74f8758a4135..826fb761e7d7ea5487d5db92d06f909952ff94c0 100644 --- a/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt +++ b/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt @@ -1,7 +1,5 @@ /* - * Apps Quickly and easily install Android apps onto your device! - * Copyright (C) 2021, Rahul Kumar Patel - * Copyright (C) 2021 E FOUNDATION + * Copyright (C) 2021-2024 MURENA SAS * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,6 +13,7 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * */ package foundation.e.apps.data.playstore.utils @@ -27,6 +26,9 @@ import foundation.e.apps.utils.SystemInfoProvider import foundation.e.apps.utils.eventBus.AppEvent import foundation.e.apps.utils.eventBus.EventBus import kotlinx.coroutines.MainScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch import okhttp3.Cache import okhttp3.Headers.Companion.toHeaders @@ -38,25 +40,21 @@ import okhttp3.Request import okhttp3.RequestBody import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.Response +import okhttp3.logging.HttpLoggingInterceptor import timber.log.Timber import java.io.IOException import java.net.SocketTimeoutException import java.util.concurrent.TimeUnit import javax.inject.Inject -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.flow.asStateFlow class GPlayHttpClient @Inject constructor( - private val cache: Cache, + private val cache: Cache, loggingInterceptor: HttpLoggingInterceptor ) : IHttpClient { - private val POST = "POST" - private val GET = "GET" - companion object { - private const val TAG = "GPlayHttpClient" private const val HTTP_TIMEOUT_IN_SECOND = 10L + private const val HTTP_METHOD_POST = "POST" + private const val HTTP_METHOD_GET = "GET" private const val SEARCH_SUGGEST = "searchSuggest" private const val STATUS_CODE_OK = 200 const val STATUS_CODE_UNAUTHORIZED = 401 @@ -78,6 +76,7 @@ class GPlayHttpClient @Inject constructor( .followRedirects(true) .followSslRedirects(true) .cache(cache) + .addInterceptor(loggingInterceptor) .build() @Throws(IOException::class) @@ -85,7 +84,7 @@ class GPlayHttpClient @Inject constructor( val request = Request.Builder() .url(url) .headers(headers.toHeaders()) - .method(POST, requestBody) + .method(HTTP_METHOD_POST, requestBody) .build() return processRequest(request) } @@ -99,7 +98,7 @@ class GPlayHttpClient @Inject constructor( val request = Request.Builder() .url(buildUrl(url, params)) .headers(headers.toHeaders()) - .method(POST, "".toRequestBody(null)) + .method(HTTP_METHOD_POST, "".toRequestBody(null)) .build() return processRequest(request) } @@ -112,7 +111,7 @@ class GPlayHttpClient @Inject constructor( val request = Request.Builder() .headers(headers.toHeaders()) .url(url) - .method(POST, requestBody) + .method(HTTP_METHOD_POST, requestBody) .build() return processRequest(request) } @@ -141,7 +140,7 @@ class GPlayHttpClient @Inject constructor( val request = Request.Builder() .url(buildUrl(url, params)) .headers(headers.toHeaders()) - .method(GET, null) + .method(HTTP_METHOD_GET, null) .build() return processRequest(request) } @@ -149,7 +148,7 @@ class GPlayHttpClient @Inject constructor( override fun getAuth(url: String): PlayResponse { val request = Request.Builder() .url(url) - .method(GET, null) + .method(HTTP_METHOD_GET, null) .build() Timber.d("get auth request", request.toString()) return processRequest(request) @@ -164,7 +163,7 @@ class GPlayHttpClient @Inject constructor( val request = Request.Builder() .url(url + paramString) .headers(headers.toHeaders()) - .method(GET, null) + .method(HTTP_METHOD_GET, null) .build() return processRequest(request) } @@ -200,7 +199,6 @@ class GPlayHttpClient @Inject constructor( isSuccessful = response.isSuccessful code = response.code val url = response.request.url - Timber.d("$TAG: Url: $url\nStatus: $code") when (code) { STATUS_CODE_UNAUTHORIZED -> MainScope().launch { diff --git a/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt b/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt index f83637ca627ecc8370814b3db960af0fe2cac882..3ebcf2e29afbd724fc8fe298c247f0a56d122598 100644 --- a/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt +++ b/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt @@ -1,6 +1,5 @@ /* - * Copyright MURENA SAS 2023 - * Apps Quickly and easily install Android apps onto your device! + * Copyright (C) 2024 MURENA SAS * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,6 +13,7 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * */ package foundation.e.apps.gplay @@ -35,6 +35,7 @@ import kotlinx.coroutines.test.runTest import okhttp3.Cache import okhttp3.OkHttpClient import okhttp3.RequestBody.Companion.toRequestBody +import okhttp3.logging.HttpLoggingInterceptor import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Before @@ -52,6 +53,9 @@ class GplyHttpClientTest { @Mock private lateinit var cache: Cache + @Mock + private lateinit var loggingInterceptor: HttpLoggingInterceptor + @Mock private lateinit var okHttpClient: OkHttpClient @@ -66,7 +70,7 @@ class GplyHttpClientTest { @Before fun setup() { MockitoAnnotations.openMocks(this) - gPlayHttpClient = GPlayHttpClient(cache) + gPlayHttpClient = GPlayHttpClient(cache, loggingInterceptor) gPlayHttpClient.okHttpClient = this.okHttpClient call = FakeCall() }