From ae5a8015b105b180e92e865bbff3ed8657c723f9 Mon Sep 17 00:00:00 2001 From: Assaf Sapir Date: Tue, 3 Oct 2023 09:38:35 +0300 Subject: [PATCH] DEV2-3857: get_server_url implementation (#651) --- .../tabnineCommon/chat/ChatMessagesRouter.kt | 2 ++ .../commandHandlers/GetServerUrlHandler.kt | 18 ++++++++++++++++++ .../chat/commandHandlers/InitHandler.kt | 8 +------- .../chat/commandHandlers/utils/ServerUrl.kt | 9 +++++++++ 4 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 Common/src/main/java/com/tabnineCommon/chat/commandHandlers/GetServerUrlHandler.kt create mode 100644 Common/src/main/java/com/tabnineCommon/chat/commandHandlers/utils/ServerUrl.kt diff --git a/Common/src/main/java/com/tabnineCommon/chat/ChatMessagesRouter.kt b/Common/src/main/java/com/tabnineCommon/chat/ChatMessagesRouter.kt index efc7acee5..c40a50858 100644 --- a/Common/src/main/java/com/tabnineCommon/chat/ChatMessagesRouter.kt +++ b/Common/src/main/java/com/tabnineCommon/chat/ChatMessagesRouter.kt @@ -6,6 +6,7 @@ import com.google.gson.JsonElement import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.project.Project import com.tabnineCommon.chat.commandHandlers.ChatMessageHandler +import com.tabnineCommon.chat.commandHandlers.GetServerUrlHandler import com.tabnineCommon.chat.commandHandlers.GetUserHandler import com.tabnineCommon.chat.commandHandlers.SendEventHandler import com.tabnineCommon.chat.commandHandlers.chatSettings.GetChatSettingsHandler @@ -36,6 +37,7 @@ class ChatMessagesRouter { "insert_at_cursor" to InsertAtCursorHandler(gson), "get_settings" to GetChatSettingsHandler(gson), "update_settings" to UpdateChatSettingsHandler(gson), + "get_server_url" to GetServerUrlHandler(gson), ) fun handleRawMessage(rawRequest: String, project: Project): String { diff --git a/Common/src/main/java/com/tabnineCommon/chat/commandHandlers/GetServerUrlHandler.kt b/Common/src/main/java/com/tabnineCommon/chat/commandHandlers/GetServerUrlHandler.kt new file mode 100644 index 000000000..957e47708 --- /dev/null +++ b/Common/src/main/java/com/tabnineCommon/chat/commandHandlers/GetServerUrlHandler.kt @@ -0,0 +1,18 @@ +package com.tabnineCommon.chat.commandHandlers + +import com.google.gson.Gson +import com.google.gson.JsonElement +import com.intellij.openapi.project.Project +import com.tabnineCommon.chat.commandHandlers.utils.getServerUrl + +data class ServerUrlResponse(val serverUrl: String) + +class GetServerUrlHandler(gson: Gson) : ChatMessageHandler(gson) { + override fun handle(payload: Unit?, project: Project): ServerUrlResponse { + val serverUrl = getServerUrl() ?: "https://api.tabnine.com" + return ServerUrlResponse(serverUrl) + } + + override fun deserializeRequest(data: JsonElement?) { + } +} diff --git a/Common/src/main/java/com/tabnineCommon/chat/commandHandlers/InitHandler.kt b/Common/src/main/java/com/tabnineCommon/chat/commandHandlers/InitHandler.kt index adf447277..c38d0ff65 100644 --- a/Common/src/main/java/com/tabnineCommon/chat/commandHandlers/InitHandler.kt +++ b/Common/src/main/java/com/tabnineCommon/chat/commandHandlers/InitHandler.kt @@ -6,8 +6,7 @@ import com.intellij.openapi.project.Project import com.tabnineCommon.capabilities.CapabilitiesService import com.tabnineCommon.capabilities.Capability import com.tabnineCommon.chat.commandHandlers.ChatMessageHandler -import com.tabnineCommon.config.Config -import com.tabnineCommon.general.StaticConfig +import com.tabnineCommon.chat.commandHandlers.utils.getServerUrl import java.awt.Color import javax.swing.UIManager @@ -33,11 +32,6 @@ class InitHandler(gson: Gson) : ChatMessageHandler(gson) { return CapabilitiesService.getInstance().isCapabilityEnabled(Capability.ALPHA) } - private fun getServerUrl(): String? { - if (!Config.IS_SELF_HOSTED) return null - return StaticConfig.getTabnineEnterpriseHost().orElse(null) - } - private fun readColorPalette(): MutableMap { val colorPalette = mutableMapOf() diff --git a/Common/src/main/java/com/tabnineCommon/chat/commandHandlers/utils/ServerUrl.kt b/Common/src/main/java/com/tabnineCommon/chat/commandHandlers/utils/ServerUrl.kt new file mode 100644 index 000000000..6e348066c --- /dev/null +++ b/Common/src/main/java/com/tabnineCommon/chat/commandHandlers/utils/ServerUrl.kt @@ -0,0 +1,9 @@ +package com.tabnineCommon.chat.commandHandlers.utils + +import com.tabnineCommon.config.Config +import com.tabnineCommon.general.StaticConfig + +fun getServerUrl(): String? { + if (!Config.IS_SELF_HOSTED) return null + return StaticConfig.getTabnineEnterpriseHost().orElse(null) +}