Skip to content

Commit

Permalink
0.7.7
Browse files Browse the repository at this point in the history
- Better and more modern HTTP client for simpler use and slightly better perf
- Fix incorrect notification scheduler (Fixes #30)
- No more (unsafe) casts
- Typo
  • Loading branch information
WarningImHack3r committed Mar 29, 2024
1 parent 5a31f4d commit 1450b01
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 26 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

## [Unreleased]

### Changed

- Improve HTTP client for safer usage and better performance (#30)

### Fixed

- Fix a crash when sending a notification

## [0.7.6] - 2024-03-24

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.github.warningimhack3r.intellijshadcnplugin
pluginName = intellij-shadcn-plugin
pluginRepositoryUrl = https://github.com/WarningImHack3r/intellij-shadcn-plugin
# SemVer format -> https://semver.org
pluginVersion = 0.7.6
pluginVersion = 0.7.7

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 213
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.github.warningimhack3r.intellijshadcnplugin.backend.http

import com.intellij.openapi.diagnostic.logger
import java.net.HttpURLConnection
import java.net.URL
import com.intellij.util.applyIf
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse

// Credit to: https://gist.github.com/GrzegorzDyrda/be47602fc855a52fba240dd2c2adc2d5
object RequestSender {
private val log = logger<RequestSender>()

Expand All @@ -16,27 +18,26 @@ object RequestSender {
* [headers][Response.headers] and [body][Response.body].
*/
fun sendRequest(url: String, method: String = "GET", headers: Map<String, String>? = null, body: String? = null): Response {
val conn = URL(url).openConnection() as HttpURLConnection

with(conn) {
requestMethod = method
doOutput = body != null
headers?.forEach(::setRequestProperty)
body?.let {
outputStream.use {
it.write(body.toByteArray())
log.debug("Sending $method request to $url")
val request = HttpRequest.newBuilder(URI(url))
.method(method, body?.let {
HttpRequest.BodyPublishers.ofString(it)
} ?: HttpRequest.BodyPublishers.noBody())
.applyIf(headers != null) {
headers?.forEach { (key, value) -> header(key, value) }
this
}
.build()
log.debug("Request method: ${request.method()}, headers: ${request.headers().map()}, body: ${body?.take(100)}${if ((body?.length ?: 0) > 100) "..." else ""}")
HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.build()
.send(request, HttpResponse.BodyHandlers.ofString())
.let { response ->
return Response(response.statusCode(), response.headers().map(), response.body()).also {
log.debug("Request to $url returned ${it.statusCode} (${it.body.length} bytes): ${it.body.take(100)}${if (it.body.length > 100) "..." else ""}")
}
}
}

if (conn.responseCode in 300..399) {
log.debug("Redirecting from ${conn.url} to ${conn.getHeaderField("Location")}")
return sendRequest(conn.getHeaderField("Location"), method, mapOf(
"Cookie" to conn.getHeaderField("Set-Cookie")
).filter { it.value != null }, body)
}

return Response(conn.responseCode, conn.headerFields, conn.inputStream.bufferedReader().readText())
}

data class Response(val statusCode: Int, val headers: Map<String, List<String>>, val body: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ abstract class Source<C : Config>(val project: Project, private val serializer:
)?.children?.map { file ->
if (file.isDirectory) file.name else file.name.substringBeforeLast(".")
}?.sorted()?.also {
log.info("Fetched ${it.size} installed components: ${it.joinToString(", ")}")
log.info("Found ${it.size} installed components: ${it.joinToString(", ")}")
} ?: emptyList<String>().also {
log.warn("Unable to fetch installed components")
log.warn("Unable to find installed components")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class NotificationManager(val project: Project? = null) {
} ?: Notifications.Bus.notify(notification)
if (hide) {
// Taken from experimental Notifications.Bus.notifyAndHide
(AppExecutorUtil.getAppExecutorService() as ScheduledExecutorService).schedule({
AppExecutorUtil.getAppScheduledExecutorService().schedule({
notification.expire()
}, 5, TimeUnit.SECONDS)
}
Expand Down

0 comments on commit 1450b01

Please sign in to comment.