From e9d42a9434bc8771cfde38afe48fa53058240d74 Mon Sep 17 00:00:00 2001 From: pavelperc Date: Mon, 4 Nov 2024 00:26:02 +0400 Subject: [PATCH] fixed ktlint and detekt issues. --- .../plugins/network/intercept/NetworkData.kt | 1 + .../plugins/network/intercept/NetworkData.kt | 9 ++++---- .../interceptor/ui/list/ApiItemHolder.kt | 2 +- .../network/internal/okhttp/ApiService.kt | 1 + .../internal/okhttp/OkhttpViewModel.kt | 23 ++++++++++++------- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/pluto-plugins/plugins/network/core/lib-no-op/src/main/java/com/pluto/plugins/network/intercept/NetworkData.kt b/pluto-plugins/plugins/network/core/lib-no-op/src/main/java/com/pluto/plugins/network/intercept/NetworkData.kt index be3bb14a..395ac78b 100644 --- a/pluto-plugins/plugins/network/core/lib-no-op/src/main/java/com/pluto/plugins/network/intercept/NetworkData.kt +++ b/pluto-plugins/plugins/network/core/lib-no-op/src/main/java/com/pluto/plugins/network/intercept/NetworkData.kt @@ -11,6 +11,7 @@ class NetworkData { ) data class Response( + val request: Request, val statusCode: Int, val body: Body?, val headers: Map, diff --git a/pluto-plugins/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/intercept/NetworkData.kt b/pluto-plugins/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/intercept/NetworkData.kt index c77c11df..d81c61df 100644 --- a/pluto-plugins/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/intercept/NetworkData.kt +++ b/pluto-plugins/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/intercept/NetworkData.kt @@ -24,8 +24,7 @@ class NetworkData { private fun parseGraphqlData(): GraphqlData? { if (method != "POST" || body == null || - body.isBinary || - !body.body.startsWith("{") + !body.isLikelyJson ) return null val json = runCatching { JSONObject(body!!.body.toString()) }.getOrNull() ?: return null val query = json.optString("query") ?: return null @@ -60,13 +59,12 @@ class NetworkData { get() = headers["Content-Encoding"].equals("gzip", ignoreCase = true) private fun getStatusMessage() = mapCode2Message(statusCode) + - if (hasGraphqlErrors) ", Response with errors" else "" + if (hasGraphqlErrors) ", Response with errors" else "" private fun parseHasGraphqlErrors(): Boolean { if (request.graphqlData == null || body == null || - body.isBinary || - !body.body.startsWith("{") + !body.isLikelyJson ) return false val json = runCatching { JSONObject(body!!.body.toString()) }.getOrNull() ?: return false return json.has("errors") @@ -83,6 +81,7 @@ class NetworkData { internal val isBinary: Boolean = BINARY_MEDIA_TYPES.contains(mediaType) val sizeInBytes: Long = body.length.toLong() internal val mediaTypeFull: String = "$mediaType/$mediaSubtype" + val isLikelyJson get() = !isBinary && body.startsWith('{') } companion object { diff --git a/pluto-plugins/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/internal/interceptor/ui/list/ApiItemHolder.kt b/pluto-plugins/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/internal/interceptor/ui/list/ApiItemHolder.kt index 5ac960f7..efeacf82 100644 --- a/pluto-plugins/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/internal/interceptor/ui/list/ApiItemHolder.kt +++ b/pluto-plugins/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/internal/interceptor/ui/list/ApiItemHolder.kt @@ -45,7 +45,7 @@ internal class ApiItemHolder(parent: ViewGroup, actionListener: DiffAwareAdapter url.setSpan { append(fontColor(method, context.color(R.color.pluto___text_dark_60))) - append(" ${urlOrQuery}") + append(" $urlOrQuery") } progress.visibility = VISIBLE status.visibility = INVISIBLE diff --git a/sample/src/main/java/com/sampleapp/functions/network/internal/okhttp/ApiService.kt b/sample/src/main/java/com/sampleapp/functions/network/internal/okhttp/ApiService.kt index 751b401a..556d070d 100644 --- a/sample/src/main/java/com/sampleapp/functions/network/internal/okhttp/ApiService.kt +++ b/sample/src/main/java/com/sampleapp/functions/network/internal/okhttp/ApiService.kt @@ -31,6 +31,7 @@ interface ApiService { @POST("xml") suspend fun xml(@Body hashMapOf: RequestBody): Any + // https://studio.apollographql.com/public/SpaceX-pxxbxen/variant/current/home @POST("https://spacex-production.up.railway.app/") suspend fun graphql(@Body body: Any): Any } diff --git a/sample/src/main/java/com/sampleapp/functions/network/internal/okhttp/OkhttpViewModel.kt b/sample/src/main/java/com/sampleapp/functions/network/internal/okhttp/OkhttpViewModel.kt index 4e185d90..b03e58af 100644 --- a/sample/src/main/java/com/sampleapp/functions/network/internal/okhttp/OkhttpViewModel.kt +++ b/sample/src/main/java/com/sampleapp/functions/network/internal/okhttp/OkhttpViewModel.kt @@ -30,8 +30,8 @@ class OkhttpViewModel : ViewModel() { enqueue { apiService.graphql( mapOf( - "query" to "query Launches(\$limit: Int){launches(limit: \$limit){mission_name}}", - "variables" to mapOf("limit" to 3), + GQL_QUERY to "query Launches(\$limit: Int){launches(limit: \$limit){mission_name}}", + GQL_VARIABLES to mapOf("limit" to GQL_LIMIT_VALID), ) ) } @@ -43,8 +43,8 @@ class OkhttpViewModel : ViewModel() { enqueue { apiService.graphql( mapOf( - "query" to "query Launches(\$limit: Int){launches(limit: \$limit){mission_name}}", - "variables" to mapOf("limit" to -1111), + GQL_QUERY to "query Launches(\$limit: Int){launches(limit: \$limit){mission_name}}", + GQL_VARIABLES to mapOf("limit" to GQL_LIMIT_INVALID), ) ) } @@ -56,8 +56,8 @@ class OkhttpViewModel : ViewModel() { enqueue { apiService.graphql( mapOf( - "query" to "mutation Insert_users(\$objects: [users_insert_input!]!) {insert_users(objects: \$objects) {affected_rows}}", - "variables" to mapOf("objects" to emptyList()), + GQL_QUERY to "mutation Insert_users(\$objects: [users_insert_input!]!) {insert_users(objects: \$objects) {affected_rows}}", + GQL_VARIABLES to mapOf("objects" to emptyList()), ) ) } @@ -69,8 +69,8 @@ class OkhttpViewModel : ViewModel() { enqueue { apiService.graphql( mapOf( - "query" to "mutation Insert_users(\$objects: [users_insert_input!]!) {insert_users112231321(objects: \$objects) {affected_rows}}", - "variables" to mapOf("objects" to emptyList()), + GQL_QUERY to "mutation Insert_users(\$objects: [users_insert_input!]!) {insert_users112231321(objects: \$objects) {affected_rows}}", + GQL_VARIABLES to mapOf("objects" to emptyList()), ) ) } @@ -131,4 +131,11 @@ class OkhttpViewModel : ViewModel() { ) } } + + companion object { + private const val GQL_QUERY = "query" + private const val GQL_LIMIT_VALID = 3 + private const val GQL_LIMIT_INVALID = -1111 + private const val GQL_VARIABLES = "variables" + } }