From a4103db65453531ed822df3567b19cbeb25a54f8 Mon Sep 17 00:00:00 2001 From: tangcent Date: Sat, 29 Jun 2024 20:45:40 +0800 Subject: [PATCH] fix: correct content-type for Postman API export --- .../com/itangcent/http/RawContentType.kt | 14 +++++++++ .../export/postman/DefaultPostmanApiHelper.kt | 12 +++----- .../api/export/postman/PostmanApiExporter.kt | 8 ----- .../postman/DefaultPostmanApiHelperTest.kt | 29 +++++++++---------- 4 files changed, 31 insertions(+), 32 deletions(-) create mode 100644 common-api/src/main/kotlin/com/itangcent/http/RawContentType.kt diff --git a/common-api/src/main/kotlin/com/itangcent/http/RawContentType.kt b/common-api/src/main/kotlin/com/itangcent/http/RawContentType.kt new file mode 100644 index 00000000..099da2d7 --- /dev/null +++ b/common-api/src/main/kotlin/com/itangcent/http/RawContentType.kt @@ -0,0 +1,14 @@ +package com.itangcent.http + +import org.apache.http.entity.ContentType + +/** + * define raw content types without charset + * + * @author tangcent + * @date 2024/06/29 + */ +object RawContentType { + + val APPLICATION_JSON = ContentType.create("application/json")!! +} \ No newline at end of file diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/DefaultPostmanApiHelper.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/DefaultPostmanApiHelper.kt index 205bc101..302d011b 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/DefaultPostmanApiHelper.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/DefaultPostmanApiHelper.kt @@ -6,10 +6,7 @@ import com.google.inject.Inject import com.itangcent.common.logger.Log import com.itangcent.common.logger.traceError import com.itangcent.common.utils.* -import com.itangcent.http.HttpClient -import com.itangcent.http.HttpRequest -import com.itangcent.http.HttpResponse -import com.itangcent.http.contentType +import com.itangcent.http.* import com.itangcent.idea.plugin.settings.helper.PostmanSettingsHelper import com.itangcent.idea.utils.GsonExUtils import com.itangcent.intellij.context.ActionContext @@ -21,7 +18,6 @@ import com.itangcent.intellij.extend.rx.ThrottleHelper import com.itangcent.intellij.extend.sub import com.itangcent.intellij.logger.Logger import com.itangcent.suv.http.HttpClientProvider -import org.apache.http.entity.ContentType import java.util.concurrent.Semaphore import java.util.concurrent.TimeUnit @@ -119,7 +115,7 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { LOG.info("create collection in workspace $workspaceId to postman") val request = getHttpClient() .post(COLLECTION) - .contentType(ContentType.APPLICATION_JSON) + .contentType(RawContentType.APPLICATION_JSON) .header("x-api-key", postmanSettingsHelper.getPrivateToken()) .body(linkedMapOf("collection" to collection)) @@ -206,7 +202,7 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { private fun doUpdateCollection(collectionId: String, apiInfo: Map): Boolean { val request = getHttpClient().put("$COLLECTION/$collectionId") - .contentType(ContentType.APPLICATION_JSON) + .contentType(RawContentType.APPLICATION_JSON) .header("x-api-key", postmanSettingsHelper.getPrivateToken()) .body(GsonUtils.toJson(linkedMapOf("collection" to apiInfo)).apply { GsonExUtils.resolveGsonLazily(this) }) @@ -434,7 +430,7 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { } companion object : Log() { - const val POSTMAN_HOST = "https://api.getpostman.com" + private const val POSTMAN_HOST = "https://api.getpostman.com" //const val IMPOREDAPI = "$POSTMANHOST/import/exported" const val COLLECTION = "$POSTMAN_HOST/collections" diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/PostmanApiExporter.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/PostmanApiExporter.kt index 4aaacc6d..df97aba7 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/PostmanApiExporter.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/PostmanApiExporter.kt @@ -8,24 +8,16 @@ import com.itangcent.common.logger.traceError import com.itangcent.common.model.Request import com.itangcent.common.utils.* import com.itangcent.idea.plugin.api.ClassApiExporterHelper -import com.itangcent.idea.plugin.rule.SuvRuleContext -import com.itangcent.idea.plugin.rule.setDoc import com.itangcent.idea.plugin.settings.PostmanExportMode import com.itangcent.idea.plugin.settings.helper.PostmanSettingsHelper -import com.itangcent.idea.psi.resource import com.itangcent.idea.utils.FileSaveHelper import com.itangcent.idea.utils.ModuleHelper -import com.itangcent.intellij.config.rule.RuleComputer -import com.itangcent.intellij.context.ActionContext import com.itangcent.intellij.logger.Logger @Singleton class PostmanApiExporter { - @Inject - private lateinit var actionContext: ActionContext - @Inject private lateinit var logger: Logger diff --git a/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/api/export/postman/DefaultPostmanApiHelperTest.kt b/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/api/export/postman/DefaultPostmanApiHelperTest.kt index 0f635aaf..8566865e 100644 --- a/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/api/export/postman/DefaultPostmanApiHelperTest.kt +++ b/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/api/export/postman/DefaultPostmanApiHelperTest.kt @@ -3,6 +3,7 @@ package com.itangcent.idea.plugin.api.export.postman import com.google.inject.Inject import com.itangcent.common.kit.toJson import com.itangcent.common.utils.KV +import com.itangcent.http.RawContentType import com.itangcent.idea.plugin.settings.helper.SettingsHelperTest import com.itangcent.intellij.context.ActionContext import com.itangcent.intellij.extend.asJsonElement @@ -10,14 +11,10 @@ import com.itangcent.intellij.extend.asMap import com.itangcent.intellij.extend.guice.singleton import com.itangcent.intellij.extend.guice.with import com.itangcent.intellij.extend.sub -import com.itangcent.mock.AdvancedContextTest import com.itangcent.suv.http.HttpClientProvider import com.itangcent.test.HttpClientProviderMockBuilder import com.itangcent.test.response404 -import org.apache.http.entity.ContentType import org.junit.jupiter.api.Test -import org.junit.jupiter.api.condition.DisabledOnOs -import org.junit.jupiter.api.condition.OS import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertNull @@ -52,19 +49,19 @@ abstract class DefaultPostmanApiHelperTest : SettingsHelperTest() { .method("POST") .response( content = CREATE_OR_UPDATE_COLLECTION_SUCCESS_RESULT, - contentType = ContentType.APPLICATION_JSON + contentType = RawContentType.APPLICATION_JSON ) .url("${DefaultPostmanApiHelper.COLLECTION}/8283378b-5df6-488b-237c-f6b9ed0d7883") .method("PUT") .response( content = CREATE_OR_UPDATE_COLLECTION_SUCCESS_RESULT, - contentType = ContentType.APPLICATION_JSON + contentType = RawContentType.APPLICATION_JSON ) .url("${DefaultPostmanApiHelper.COLLECTION}/31415926-67ce-2d4a-13c1-535897932384") .method("PUT") .response( content = CREATE_OR_UPDATE_COLLECTION_FAILED_RESULT, - contentType = ContentType.APPLICATION_JSON + contentType = RawContentType.APPLICATION_JSON ) .url("${DefaultPostmanApiHelper.COLLECTION}/31415926-67ce-12cf-13c1-535897932384") .method("PUT") @@ -75,43 +72,43 @@ abstract class DefaultPostmanApiHelperTest : SettingsHelperTest() { .method("GET") .response( content = ALL_COLLECTIONS_RESULT, - contentType = ContentType.APPLICATION_JSON + contentType = RawContentType.APPLICATION_JSON ) .url("${DefaultPostmanApiHelper.WORKSPACE}/e508269b-72ef-4c67-92c4-55777ba33434") .method("GET") .response( content = COLLECTIONS_IN_WORKSPACE, - contentType = ContentType.APPLICATION_JSON + contentType = RawContentType.APPLICATION_JSON ) .url("${DefaultPostmanApiHelper.COLLECTION}/42dc9386-34ab-2d4a-83c1-535897932384") .method("GET") .response( content = COLLECTION_INFO_RESULT, - contentType = ContentType.APPLICATION_JSON + contentType = RawContentType.APPLICATION_JSON ) .url(DefaultPostmanApiHelper.WORKSPACE) .method("GET") .response( content = ALL_WORKSPACES_RESULT, - contentType = ContentType.APPLICATION_JSON + contentType = RawContentType.APPLICATION_JSON ) .url("${DefaultPostmanApiHelper.WORKSPACE}/0db55d8f-8e03-4568-2871-250a378ab87c") .method("GET") .response( content = WORKSPACE_RESULT, - contentType = ContentType.APPLICATION_JSON + contentType = RawContentType.APPLICATION_JSON ) .url("${DefaultPostmanApiHelper.COLLECTION}/42dc9386-34ab-2d4a-83c1-535897932384") .method("DELETE") .response( content = DELETE_COLLECTION_RESULT, - contentType = ContentType.APPLICATION_JSON + contentType = RawContentType.APPLICATION_JSON ) .url("${DefaultPostmanApiHelper.COLLECTION}/378bacc3-77cf-5331-82c1-ed273450ae35") .method("DELETE") .response( content = DELETE_COLLECTION_FAILED_RESULT, - contentType = ContentType.APPLICATION_JSON + contentType = RawContentType.APPLICATION_JSON ) .notFound().response404() .build() @@ -257,7 +254,7 @@ abstract class DefaultPostmanApiHelperTest : SettingsHelperTest() { .call() .response( content = AUTHENTICATION_FAILED_RESULT, - contentType = ContentType.APPLICATION_JSON + contentType = RawContentType.APPLICATION_JSON ) .notFound().response404() .build() @@ -277,7 +274,7 @@ abstract class DefaultPostmanApiHelperTest : SettingsHelperTest() { .call() .response( content = SERVICE_LIMIT_EXHAUSTED_FAILED_RESULT, - contentType = ContentType.APPLICATION_JSON, + contentType = RawContentType.APPLICATION_JSON, responseCode = 429 ) .notFound().response404()