Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public class ServersPlugin(
return getPlugin<ServersPlugin>().getContainer<ServersPluginContainer>().serversRepository.getDefault()
}

public fun getSelectedStage(): DebugStage {
public fun getSelectedStage(): DebugStage? {
return getPlugin<ServersPlugin>().getContainer<ServersPluginContainer>().stagesRepository.getSelectedStage()
}

public fun getDefaultStage(): DebugStage {
public fun getDefaultStage(): DebugStage? {
return getPlugin<ServersPlugin>().getContainer<ServersPluginContainer>().stagesRepository.getDefault()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class DebugStageRepository(
}
}

fun getSelectedStage(): DebugStage {
fun getSelectedStage(): DebugStage? {
val stageName = sharedPreferences.getString(SELECTED_STAGE_NAME, null)
val hostsHash = sharedPreferences.getInt(SELECTED_STAGE_HOSTS_HASH, -1)

Expand All @@ -48,8 +48,8 @@ internal class DebugStageRepository(
}
}

fun getDefault(): DebugStage {
return preInstalledStages.first { it.isDefault }
fun getDefault(): DebugStage? {
return preInstalledStages.firstOrNull { it.isDefault }
}

suspend fun getStages(): List<DebugStage> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,22 @@ public data class DebugStage(
}

override fun equals(other: Any?): Boolean {
val otherServer = other as DebugStage
return this.hosts.size == otherServer.hosts.size && this.hosts == otherServer.hosts
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as DebugStage

if (name != other.name) return false
if (hosts != other.hosts) return false
if (isDefault != other.isDefault) return false

return true
}

override fun hashCode(): Int {
var result = name.hashCode()
result = 31 * result + hosts.hashCode()
result = 31 * result + isDefault.hashCode()
return result
Comment thread
EmogurovAnton marked this conversation as resolved.
Outdated
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ public class DebugStageInterceptor(private val hostName: String) : Interceptor {
var request = chain.request()
return if (DebugPanel.isInitialized) {
val debugStage = stageRepository.getSelectedStage()
val host = debugStage.hosts[hostName]
if (host != null) {
val newUrl = request.getNewUrl(host)
request = request.newBuilder()
.url(newUrl)
.build()
}
chain.proceed(requestModifier?.invoke(request, debugStage) ?: request)
val modifiedRequest = debugStage?.let { stage ->
val host = stage.hosts[hostName]
if (host != null) {
val newUrl = request.getNewUrl(host)
request = request.newBuilder()
.url(newUrl)
.build()
}

requestModifier?.invoke(request, stage)
} ?: request

chain.proceed(modifiedRequest)
Comment thread
EmogurovAnton marked this conversation as resolved.
Outdated
} else {
chain.proceed(request)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.redmadrobot.debug_sample.debug_data

import com.redmadrobot.debug.plugin.servers.data.model.DebugServer
import com.redmadrobot.debug.plugin.servers.data.model.DebugServerData
import com.redmadrobot.debug.plugin.servers.data.model.DebugStage

class DebugServersProvider {

Expand All @@ -22,23 +21,6 @@ class DebugServersProvider {
DebugServer(
name = "debug 4", url = "https://testserver4.com"
),
DebugStage(
name = "debug stage 1",
hosts = mapOf(
"main" to "https://testserver1main.com",
"s3" to "https://testserver1s3.com",
"wss" to "https://testserver1wss.com"
),
isDefault = true
),
Comment thread
TopHlop marked this conversation as resolved.
DebugStage(
name = "debug stage 2",
hosts = mapOf(
"main" to "https://testserver2main.com",
"s3" to "https://testserver2s3.com",
"wss" to "https://testserver2wss.com"
)
),
)
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package com.redmadrobot.debug_sample.network

import com.redmadrobot.debug.plugin.servers.interceptor.DebugStageInterceptor
import com.redmadrobot.debug.plugin.servers.interceptor.DebugServerInterceptor
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import retrofit2.Retrofit
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLConnection
import java.net.URLStreamHandler

object ApiFactory {

fun getSampleApi(onCalled: (String) -> Unit): SampleApi {
val retrofit = Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
Expand All @@ -22,25 +16,14 @@ object ApiFactory {
}

private fun getSampleClient(onCalled: (String) -> Unit): OkHttpClient {

return OkHttpClient.Builder()
.addInterceptor(interceptor = DebugServerInterceptor())
.addInterceptor(
DebugStageInterceptor("s3").modifyRequest { request, server ->
if (server.name == "Test") {
request.newBuilder()
.addHeader("Authorization", "testToken")
.build()
} else {
request
}
}
)
.addInterceptor(object : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
interceptor = Interceptor { chain ->
onCalled.invoke(chain.request().url.toString())
return chain.proceed(chain.request())
chain.proceed(chain.request())
}
})
)
.build()
}
}
Loading