From 2f4bbfdc7010222466a8b031598d7af10ea69b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sat, 11 May 2024 17:06:33 +0200 Subject: [PATCH] Simplify attachModuleHeaders method --- .../io/github/wulkanowy/sdk/scrapper/Utils.kt | 19 ++++++++------- .../interceptor/AutoLoginInterceptor.kt | 23 ++++++++----------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Utils.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Utils.kt index b5b06241..eca19716 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Utils.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Utils.kt @@ -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 diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/interceptor/AutoLoginInterceptor.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/interceptor/AutoLoginInterceptor.kt index 337df2be..519bd773 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/interceptor/AutoLoginInterceptor.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/interceptor/AutoLoginInterceptor.kt @@ -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)