Simplify attachModuleHeaders method

This commit is contained in:
Mikołaj Pich 2024-05-11 17:06:33 +02:00
parent 48a6c72d00
commit 2f4bbfdc70
No known key found for this signature in database
2 changed files with 21 additions and 21 deletions

View file

@ -202,11 +202,7 @@ internal fun String.md5(): String {
}
internal fun HttpUrl.mapModuleUrls(moduleHost: String, appVersion: String?): HttpUrl {
val pathSegmentIndex = when (moduleHost) {
StudentPlusModuleHost -> 3
StudentModuleHost, MessagesModuleHost -> 2
else -> error("Not supported!")
}
val pathSegmentIndex = getPathIndexByModuleHost(moduleHost)
val pathKey = pathSegments.getOrNull(pathSegmentIndex)
val mappedPath = ApiEndpointsMap[appVersion]
?.get(moduleHost)
@ -223,10 +219,17 @@ internal fun HttpUrl.mapModuleUrls(moduleHost: String, appVersion: String?): Htt
} else this
}
internal fun Request.Builder.attachVToken(url: HttpUrl, headers: ModuleHeaders?): Request.Builder {
val pathKey = url.pathSegments.getOrNull(2)
internal fun getPathIndexByModuleHost(moduleHost: String): Int = when (moduleHost) {
StudentPlusModuleHost -> 3
StudentModuleHost, MessagesModuleHost -> 2
else -> -1
}
internal fun Request.Builder.attachVToken(moduleHost: String, url: HttpUrl, headers: ModuleHeaders?): Request.Builder {
val pathSegmentIndex = getPathIndexByModuleHost(moduleHost)
val pathKey = url.pathSegments.getOrNull(pathSegmentIndex)
val mappedUuid = ApiEndpointsVTokenMap[headers?.appVersion]
?.get(MessagesModuleHost)
?.get(moduleHost)
?.get(pathKey)
?: return this

View file

@ -163,20 +163,17 @@ internal class AutoLoginInterceptor(
}
private fun Request.attachModuleHeaders(): Request {
val headers = when {
MessagesModuleHost in url.host -> headersByHost[MessagesModuleHost]
StudentPlusModuleHost in url.host -> headersByHost[StudentPlusModuleHost]
StudentModuleHost in url.host -> headersByHost[StudentModuleHost]
else -> return this
val moduleHost = when {
MessagesModuleHost in url.host -> MessagesModuleHost
StudentPlusModuleHost in url.host -> StudentPlusModuleHost
StudentModuleHost in url.host -> StudentModuleHost
else -> ""
}
logger.info("X-V-AppVersion: ${headers?.appVersion}")
val mappedUrl = when {
MessagesModuleHost in url.host -> url.mapModuleUrls(MessagesModuleHost, headers?.appVersion)
StudentPlusModuleHost in url.host -> url.mapModuleUrls(StudentPlusModuleHost, headers?.appVersion)
StudentModuleHost in url.host -> url.mapModuleUrls(StudentModuleHost, headers?.appVersion)
else -> url
}
val headers = headersByHost[moduleHost]
val mappedUrl = url.mapModuleUrls(moduleHost, headers?.appVersion)
logger.info("X-V-AppVersion: ${headers?.appVersion}")
return newBuilder()
.apply {
@ -184,7 +181,7 @@ internal class AutoLoginInterceptor(
addHeader("X-V-RequestVerificationToken", it.token)
addHeader("X-V-AppGuid", it.appGuid)
addHeader("X-V-AppVersion", it.appVersion)
attachVToken(url, headers)
attachVToken(moduleHost, url, headers)
}
}
.url(mappedUrl)