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"), DATATREATMENTS("DATATREATMENTS"),
EVENTS("EVENTS", defaultValue = false, requiresRestart = true), EVENTS("EVENTS", defaultValue = false, requiresRestart = true),
GLUCOSE("GLUCOSE", defaultValue = false), GLUCOSE("GLUCOSE", defaultValue = false),
HTTP("HTTP"),
LOCATION("LOCATION"), LOCATION("LOCATION"),
NOTIFICATION("NOTIFICATION"), NOTIFICATION("NOTIFICATION"),
NSCLIENT("NSCLIENT"), NSCLIENT("NSCLIENT"),

View file

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

View file

@ -19,19 +19,22 @@ internal object NetworkStackBuilder {
baseUrl: String, baseUrl: String,
context: Context, context: Context,
accessToken: String, // refresh token accessToken: String, // refresh token
logging: Boolean = false logging: Boolean,
logger: HttpLoggingInterceptor.Logger
): NightscoutRemoteService = getRetrofit( ): NightscoutRemoteService = getRetrofit(
baseUrl = baseUrl, baseUrl = baseUrl,
context = context, context = context,
refreshToken = accessToken, refreshToken = accessToken,
logging = logging logging = logging,
logger = logger
).create(NightscoutRemoteService::class.java) ).create(NightscoutRemoteService::class.java)
private fun getRetrofit( private fun getRetrofit(
baseUrl: String, baseUrl: String,
context: Context, context: Context,
refreshToken: String, refreshToken: String,
logging: Boolean logging: Boolean,
logger: HttpLoggingInterceptor.Logger
): Retrofit = ): Retrofit =
Retrofit.Builder() Retrofit.Builder()
.baseUrl("https://$baseUrl/api/") .baseUrl("https://$baseUrl/api/")
@ -40,7 +43,8 @@ internal object NetworkStackBuilder {
context = context, context = context,
logging = logging, logging = logging,
refreshToken = refreshToken, refreshToken = refreshToken,
authRefreshRetrofit = getAuthRefreshRetrofit(baseUrl, context, logging) authRefreshRetrofit = getAuthRefreshRetrofit(baseUrl, context, logging, logger),
logger = logger
) )
) )
.addConverterFactory(GsonConverterFactory.create(provideGson())) .addConverterFactory(GsonConverterFactory.create(provideGson()))
@ -49,11 +53,12 @@ internal object NetworkStackBuilder {
private fun getAuthRefreshRetrofit( private fun getAuthRefreshRetrofit(
baseUrl: String, baseUrl: String,
context: Context, context: Context,
logging: Boolean logging: Boolean,
logger: HttpLoggingInterceptor.Logger
): Retrofit = ): Retrofit =
Retrofit.Builder() Retrofit.Builder()
.baseUrl("https://$baseUrl/api/") .baseUrl("https://$baseUrl/api/")
.client(getAuthRefreshOkHttpClient(context = context, logging = logging)) .client(getAuthRefreshOkHttpClient(context = context, logging = logging, logger = logger))
.addConverterFactory(GsonConverterFactory.create(provideGson())) .addConverterFactory(GsonConverterFactory.create(provideGson()))
.build() .build()
@ -61,24 +66,27 @@ internal object NetworkStackBuilder {
context: Context, context: Context,
logging: Boolean, logging: Boolean,
refreshToken: String, refreshToken: String,
authRefreshRetrofit: Retrofit authRefreshRetrofit: Retrofit,
logger: HttpLoggingInterceptor.Logger
): OkHttpClient = OkHttpClient.Builder().run { ): OkHttpClient = OkHttpClient.Builder().run {
addInterceptor(NSAuthInterceptor(refreshToken, authRefreshRetrofit)) addInterceptor(NSAuthInterceptor(refreshToken, authRefreshRetrofit))
commonOkHttpSetup(logging, context) commonOkHttpSetup(logging, context, logger)
} }
private fun getAuthRefreshOkHttpClient( private fun getAuthRefreshOkHttpClient(
context: Context, context: Context,
logging: Boolean, 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( private fun OkHttpClient.Builder.commonOkHttpSetup(
logging: Boolean, logging: Boolean,
context: Context context: Context,
logger: HttpLoggingInterceptor.Logger
): OkHttpClient { ): OkHttpClient {
if (logging) { if (logging) {
addNetworkInterceptor( addNetworkInterceptor(
HttpLoggingInterceptor().also { it.level = HttpLoggingInterceptor.Level.BODY } HttpLoggingInterceptor(logger).also { it.level = HttpLoggingInterceptor.Level.BODY }
) )
} }
cache(Cache(context.cacheDir, OK_HTTP_CACHE_SIZE)) 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("/$"), ""), 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, ""), accessToken = sp.getString(R.string.key_ns_client_token, ""),
context = context, context = context,
logging = true logging = true,
logger = { msg -> aapsLogger.debug(LTag.HTTP, msg) }
) )
rxBus.send(EventSWSyncStatus(status)) rxBus.send(EventSWSyncStatus(status))
} }