Make possible to map responses recursively

This commit is contained in:
Mikołaj Pich 2024-05-30 16:35:02 +02:00
parent 7f9456fe6c
commit 1a1878acd9
No known key found for this signature in database
2 changed files with 27 additions and 7 deletions

View file

@ -848,6 +848,23 @@ internal val ApiEndpointsResponseMapping = mapOf(
"wycofana" to "HrvHItBsstFrEABGIDtuuGJJIGBECwFu",
"zalaczniki" to "GvtvvDBGvsAHEBsDsBJIJGtAtCvFswAI",
),
"WiadomoscOdpowiedzPrzekaz" to mapOf(
"adresaci" to "AsrstFGtsDuuEsHCJFBDuICHHtCuwEuG",
"apiGlobalKey" to "CFHDJEstCsAvErGrJFCvvrvEJuABHFuD",
"data" to "BArtutvvEurtEwBuJrvrCAHFIsFrJCJu",
"id" to "HtIFwvAFwsJIEAsrJIrJCEvGuIrrCAIG",
"nadawcaInfo" to "AswHHvHvJDJsEIrJIHvrvDtCFErwBGtE",
"nadawcaSkrzynkaGlobalKey" to "BHuHEFHuDGHGEFIwsrssEIFBAuADsrFE",
"nadawcaSkrzynkaNazwa" to "DwGFGAHvJtvDEEGFstwICEwEBEDrAFID",
"temat" to "CHCsGwHIAIGJErwGsHJAHGBsArsDvBBG",
"tresc" to "FJFFFtAFHJvvErrEssGBGIwDJGrEGwrH",
"uzytkownikSkrzynkaGlobalKey" to "AsvwvAGGJsCCECrGJJIAvIrtBtwBttFG",
"zalaczniki" to "vvtvEJDDIFEGEuCIstEJsHIutAIDGrGJ",
// nested
"nazwa" to "IAACDtCusJrAEJsuJICCvIDCHrrAvEtw",
"skrzynkaGlobalKey" to "BrHECCDIvBCJEvsIJtGJAHwBuAFIHDGG",
),
),
),
)

View file

@ -225,13 +225,16 @@ internal class AutoLoginInterceptor(
return response.body?.byteStream()?.bufferedReader()?.use {
val contentType = response.body?.contentType()
val body = mapResponseContent(it.readText(), jsonMappings).toResponseBody(contentType)
val body = mapResponseContent(
response = Json.decodeFromString<JsonElement>(it.readText()),
jsonMappings = jsonMappings,
).toString().toResponseBody(contentType)
response.newBuilder().body(body).build()
} ?: response
}
private fun mapResponseContent(input: String, jsonMappings: Map<String, String>?): String {
return when (val response = Json.decodeFromString<JsonElement>(input)) {
private fun mapResponseContent(response: JsonElement, jsonMappings: Map<String, String>?): JsonElement {
return when (response) {
is JsonArray -> JsonArray(
response.jsonArray.map {
when (it) {
@ -244,7 +247,7 @@ internal class AutoLoginInterceptor(
is JsonObject -> mapJsonObjectKeys(response.jsonObject, jsonMappings)
else -> response
}.toString()
}
}
private fun mapJsonObjectKeys(jsonObject: JsonObject, jsonMappings: Map<String, String>?): JsonObject {
@ -252,9 +255,9 @@ internal class AutoLoginInterceptor(
value to key
}.orEmpty().toMap()
return JsonObject(
jsonObject.mapKeys {
mapping[it.key] ?: it.key
},
jsonObject.map {
(mapping[it.key] ?: it.key) to mapResponseContent(it.value, jsonMappings)
}.toMap(),
)
}