Skip to content

Commit

Permalink
feat(httpclient): refactor CUrlHttpHandler and CUrlConverter for bett…
Browse files Browse the repository at this point in the history
…er variable handling

Refactored CUrlHttpHandler and CUrlConverter to improve handling of environment and process variables. Now using ShireEnvVariableFiller to fill variables in the content before parsing it into a RestClientRequest with CurlParser. Also updated related test cases to reflect these changes.
  • Loading branch information
phodal committed Sep 19, 2024
1 parent d181831 commit a039679
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
package com.phodal.shire.httpclient.converter

import com.intellij.httpClient.converters.curl.parser.CurlParser
import com.intellij.httpClient.execution.RestClientRequest
import com.intellij.httpClient.http.request.HttpRequestHeaderFields
import com.intellij.json.psi.*
import com.phodal.shirecore.provider.http.ShireEnvVariableFiller
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody

object CUrlConverter {
private fun convert(content: String): RestClientRequest {
val restClientRequest = CurlParser().parseToRestClientRequest(content)
return restClientRequest
}

fun convert(
content: String,
envVars: List<Set<String>> = listOf(),
processVars: Map<String, String> = mapOf(),
envObj: JsonObject? = null,
): Request {
fun convert(request: RestClientRequest): Request {
val builder = Request.Builder()

val filledContent = ShireEnvVariableFiller.fillVariables(content, envVars, envObj, processVars)
val request = this.convert(filledContent)

builder.url(request.buildFullUrl())
request.headers.forEach {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.phodal.shire.httpclient.handler
import com.intellij.execution.console.ConsoleViewWrapperBase
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.execution.ui.RunContentManager
import com.intellij.httpClient.converters.curl.parser.CurlParser
import com.intellij.httpClient.execution.RestClientRequest
import com.intellij.httpClient.http.request.HttpRequestCollectionProvider
import com.intellij.httpClient.http.request.notification.HttpClientWhatsNewContentService
import com.intellij.ide.scratch.ScratchUtil
Expand All @@ -18,8 +20,9 @@ import com.phodal.shire.httpclient.converter.CUrlConverter
import com.phodal.shirecore.provider.http.HttpHandler
import com.phodal.shirecore.provider.http.HttpHandlerType
import com.phodal.shirecore.provider.http.ShireEnvReader
import com.phodal.shirecore.provider.http.ShireEnvVariableFiller
import okhttp3.OkHttpClient
import okhttp3.Request
import okio.Buffer

class CUrlHttpHandler : HttpHandler {
override fun isApplicable(type: HttpHandlerType): Boolean = type == HttpHandlerType.CURL
Expand All @@ -32,7 +35,9 @@ class CUrlHttpHandler : HttpHandler {
): String? {
val processVariables: Map<String, String> = variablesName.associateWith { (variableTable[it] as? String ?: "") }

var filledShell: String = content
val client = OkHttpClient()
var restClientRequest: RestClientRequest? = null
val request = runReadAction {
val scope = getSearchScope(project)

Expand All @@ -41,18 +46,19 @@ class CUrlHttpHandler : HttpHandler {
val envObject = ShireEnvReader.getEnvObject(envName, scope, project)

val enVariables: List<Set<String>> = ShireEnvReader.fetchEnvironmentVariables(envName, scope)
CUrlConverter.convert(content, enVariables, processVariables, envObject)
}
filledShell = ShireEnvVariableFiller.fillVariables(content, enVariables, envObject, processVariables)
restClientRequest = CurlParser().parseToRestClientRequest(filledShell)

// get current terminl console
showLogInConsole(project, content, request)
CUrlConverter.convert(restClientRequest!!)
}

showLogInConsole(project, filledShell, restClientRequest)

val response = client.newCall(request).execute()
return response.body?.string()
}

fun showLogInConsole(project: Project, content: String, request: Request) {
private fun showLogInConsole(project: Project, content: String, request: RestClientRequest?) {
val contentManager = RunContentManager.getInstance(project)
val console = contentManager.selectedContent?.executionConsole as? ConsoleViewWrapperBase ?: return

Expand All @@ -64,6 +70,10 @@ class CUrlHttpHandler : HttpHandler {
console.print("\n", ConsoleViewContentType.LOG_INFO_OUTPUT)
/// converted content
console.print(request.toString(), ConsoleViewContentType.LOG_INFO_OUTPUT)
/// request.body
request?.formBodyPart?.forEach {
console.print(it.toString(), ConsoleViewContentType.LOG_INFO_OUTPUT)
}
console.print("\n--------------------\n", ConsoleViewContentType.LOG_INFO_OUTPUT)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.phodal.shire.httpclient.converter

import com.intellij.httpClient.converters.curl.parser.CurlParser
import com.intellij.json.psi.JsonFile
import com.intellij.json.psi.JsonObject
import com.intellij.testFramework.fixtures.BasePlatformTestCase
Expand All @@ -10,9 +11,10 @@ class CUrlConverterTest : BasePlatformTestCase() {
fun testShouldConvertCurlToRestClientRequest() {
// Given
val content = "curl -X POST http://example.com/api/resource -d 'data'"
val request = CurlParser().parseToRestClientRequest(content)

// When
val restClientRequest = CUrlConverter.convert(content)
val restClientRequest = CUrlConverter.convert(request = request)

// Then
assertEquals("http://example.com/api/resource", restClientRequest.url.toString())
Expand All @@ -32,9 +34,9 @@ class CUrlConverterTest : BasePlatformTestCase() {
" }\n" +
" ]\n" +
"}'"

val req = CurlParser().parseToRestClientRequest(content)
// When
val request = CUrlConverter.convert(content)
val request = CUrlConverter.convert(request = req)

// Then
assertEquals("https://open.bigmodel.cn/api/paas/v4/chat/completions", request.url.toString())
Expand Down

0 comments on commit a039679

Please sign in to comment.