NSCv3: HTTP logging
This commit is contained in:
parent
413d958c87
commit
78295cf827
|
@ -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"),
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue