NSCv3: HTTP logging

This commit is contained in:
Milos Kozak 2023-01-31 14:54:04 +01:00
parent 413d958c87
commit 78295cf827
4 changed files with 26 additions and 13 deletions

View file

@ -13,6 +13,7 @@ enum class LTag(val tag: String, val defaultValue : Boolean = true, val requires
DATATREATMENTS("DATATREATMENTS"),
EVENTS("EVENTS", defaultValue = false, requiresRestart = true),
GLUCOSE("GLUCOSE", defaultValue = false),
HTTP("HTTP"),
LOCATION("LOCATION"),
NOTIFICATION("NOTIFICATION"),
NSCLIENT("NSCLIENT"),

View file

@ -35,6 +35,7 @@ import info.nightscout.sdk.utils.toNotNull
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.logging.HttpLoggingInterceptor
import org.json.JSONObject
/**
@ -64,6 +65,7 @@ class NSAndroidClientImpl(
accessToken: String,
context: Context,
logging: Boolean,
logger: HttpLoggingInterceptor.Logger,
private val dispatcher: CoroutineDispatcher = Dispatchers.IO
) : NSAndroidClient {
@ -71,7 +73,8 @@ class NSAndroidClientImpl(
baseUrl = baseUrl,
context = context,
accessToken = accessToken,
logging = logging
logging = logging,
logger = logger
)
override var lastStatus: Status? = null
private set

View file

@ -19,19 +19,22 @@ internal object NetworkStackBuilder {
baseUrl: String,
context: Context,
accessToken: String, // refresh token
logging: Boolean = false
logging: Boolean,
logger: HttpLoggingInterceptor.Logger
): NightscoutRemoteService = getRetrofit(
baseUrl = baseUrl,
context = context,
refreshToken = accessToken,
logging = logging
logging = logging,
logger = logger
).create(NightscoutRemoteService::class.java)
private fun getRetrofit(
baseUrl: String,
context: Context,
refreshToken: String,
logging: Boolean
logging: Boolean,
logger: HttpLoggingInterceptor.Logger
): Retrofit =
Retrofit.Builder()
.baseUrl("https://$baseUrl/api/")
@ -40,7 +43,8 @@ internal object NetworkStackBuilder {
context = context,
logging = logging,
refreshToken = refreshToken,
authRefreshRetrofit = getAuthRefreshRetrofit(baseUrl, context, logging)
authRefreshRetrofit = getAuthRefreshRetrofit(baseUrl, context, logging, logger),
logger = logger
)
)
.addConverterFactory(GsonConverterFactory.create(provideGson()))
@ -49,11 +53,12 @@ internal object NetworkStackBuilder {
private fun getAuthRefreshRetrofit(
baseUrl: String,
context: Context,
logging: Boolean
logging: Boolean,
logger: HttpLoggingInterceptor.Logger
): Retrofit =
Retrofit.Builder()
.baseUrl("https://$baseUrl/api/")
.client(getAuthRefreshOkHttpClient(context = context, logging = logging))
.client(getAuthRefreshOkHttpClient(context = context, logging = logging, logger = logger))
.addConverterFactory(GsonConverterFactory.create(provideGson()))
.build()
@ -61,24 +66,27 @@ internal object NetworkStackBuilder {
context: Context,
logging: Boolean,
refreshToken: String,
authRefreshRetrofit: Retrofit
authRefreshRetrofit: Retrofit,
logger: HttpLoggingInterceptor.Logger
): OkHttpClient = OkHttpClient.Builder().run {
addInterceptor(NSAuthInterceptor(refreshToken, authRefreshRetrofit))
commonOkHttpSetup(logging, context)
commonOkHttpSetup(logging, context, logger)
}
private fun getAuthRefreshOkHttpClient(
context: Context,
logging: Boolean,
): OkHttpClient = OkHttpClient.Builder().run { commonOkHttpSetup(logging, context) }
logger: HttpLoggingInterceptor.Logger
): OkHttpClient = OkHttpClient.Builder().run { commonOkHttpSetup(logging, context, logger) }
private fun OkHttpClient.Builder.commonOkHttpSetup(
logging: Boolean,
context: Context
context: Context,
logger: HttpLoggingInterceptor.Logger
): OkHttpClient {
if (logging) {
addNetworkInterceptor(
HttpLoggingInterceptor().also { it.level = HttpLoggingInterceptor.Level.BODY }
HttpLoggingInterceptor(logger).also { it.level = HttpLoggingInterceptor.Level.BODY }
)
}
cache(Cache(context.cacheDir, OK_HTTP_CACHE_SIZE))

View file

@ -260,7 +260,8 @@ class NSClientV3Plugin @Inject constructor(
baseUrl = sp.getString(info.nightscout.core.utils.R.string.key_nsclientinternal_url, "").lowercase().replace("https://", "").replace(Regex("/$"), ""),
accessToken = sp.getString(R.string.key_ns_client_token, ""),
context = context,
logging = true
logging = true,
logger = { msg -> aapsLogger.debug(LTag.HTTP, msg) }
)
rxBus.send(EventSWSyncStatus(status))
}