Skip to content

Commit

Permalink
Add other toolbox generator
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Sep 14, 2024
1 parent aecff72 commit 15080dc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/main/kotlin/org/http4k/intellij/action/GenerateCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.openapi.vfs.readText
import com.intellij.openapi.wm.impl.status.StatusBarUtil
import dev.forkhandles.result4k.Failure
import dev.forkhandles.result4k.Result
import dev.forkhandles.result4k.map
import dev.forkhandles.result4k.onFailure
import dev.forkhandles.result4k.orThrow
import dev.forkhandles.result4k.resultFrom
Expand All @@ -26,10 +28,15 @@ import org.http4k.intellij.wizard.ClientApiStyle.connect
import org.http4k.intellij.wizard.ClientApiStyle.standard
import org.http4k.intellij.wizard.GeneratorFormat
import org.http4k.intellij.wizard.ToolboxApi
import java.io.BufferedOutputStream
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.InputStream
import java.time.Clock
import java.time.temporal.ChronoUnit.SECONDS
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream

abstract class GenerateCode(private val functionName: String) : AnAction(), ActionUpdateThreadAware {
override fun update(e: AnActionEvent) {
Expand All @@ -40,7 +47,7 @@ abstract class GenerateCode(private val functionName: String) : AnAction(), Acti
override fun getActionUpdateThread() = BGT

override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
val project = e.project ?: return
val file = e.dataContext.getData(VIRTUAL_FILE) ?: return
val http4kDir = File(project.basePath, ".http4k")
val functionDir = File(http4kDir, functionName)
Expand Down Expand Up @@ -102,6 +109,25 @@ class GenerateDataClasses : GenerateCode("dataclasses") {
override fun activeFor(selected: VirtualFile?) = selected?.supportedFormat() != null
}

class GenerateMessageCode : GenerateCode("http") {
override fun VirtualFile.generateCode() =
ToolboxApi().generateMessage(inputStream)
.map {
val outputStream = ByteArrayOutputStream()
ZipOutputStream(BufferedOutputStream(outputStream)).use { out ->
with(out) {
putNextEntry(ZipEntry("message.kt"))
write(it.readBytes())
}
}
ByteArrayInputStream(outputStream.toByteArray())
}

override fun activeFor(selected: VirtualFile?) =
(selected?.name?.endsWith(".http") ?: true) &&
selected?.readText()?.contains("HTTP/") == true
}

class GenerateData4kClasses : GenerateCode("data4k") {
override fun VirtualFile.generateCode(): Result<InputStream, RemoteRequestFailed> {
val format = supportedFormat() ?: return Failure(RemoteRequestFailed(BAD_REQUEST, "Unsupported format"))
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/org/http4k/intellij/wizard/ToolboxApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ class ToolboxApi(
}
}

fun generateMessage(content: InputStream) =
http(Request(POST, "/api/v1/message").body(content)).run {
when {
status.successful -> Success(body.stream)
else -> Failure(RemoteRequestFailed(status, bodyString()))
}
}

fun generateOpenApiClasses(clientApiStyle: ClientApiStyle , inputStream: InputStream): Result<InputStream, RemoteRequestFailed> {
val style = MultipartFormField.required("clientApiStyle")
val field = MultipartFormField.required("packageName")
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ It:
<li>generates boilerplate http4k based projects with ease by integrating a dynamic wizard into IntelliJ IDEA <b>New Project</b> option</li>
<li>*generates http4k server, client and model code from <a href="https://www.openapis.org/">OpenAPI</a> spec. Right click on the file and select <b>New -> http4k -> OpenAPI to http4k</b></li>
<li>*generates data classes from JSON/YAML files. Right click on the file and select <b>New -> http4k -> Dataclasses</b></li>
<li>*generates HTTP request/response code from files. Right click on the file and select <b>New -> http4k -> HTTP Request/Response</b></li>
<li>*generates <a href="https://github.com/fork-handles/forkhandles/tree/trunk/data4k">data4k</a> wrapper classes from JSON files. Right click on the file and select <b>New -> http4k -> data4k wrapper</b></li>
</ul>
<br/>
Expand Down Expand Up @@ -82,6 +83,13 @@ All generated code is added to the project in the `.http4k` directory structure
<add-to-group group-id="http4k-menu" anchor="last"/>
</action>

<action id="generate_http_message"
class="org.http4k.intellij.action.GenerateMessageCode"
text="HTTP Request/Response"
description="Generate HTTP message from file">
<add-to-group group-id="http4k-menu" anchor="last"/>
</action>

<action id="generate_http4k_connect"
class="org.http4k.intellij.action.GenerateOpenApiCodeConnect"
text="... with http4k-connect client"
Expand Down

0 comments on commit 15080dc

Please sign in to comment.