From e8e55b8706d2ee8b088235120c530969811a4a07 Mon Sep 17 00:00:00 2001 From: JFronny Date: Sun, 12 May 2024 16:42:04 +0200 Subject: [PATCH] chore: clean up API url handling --- README.md | 2 +- .../io/gitlab/jfronny/sdom/SDCredentials.kt | 2 +- .../kotlin/io/gitlab/jfronny/sdom/SDom.kt | 40 +++++++++++++++---- .../jfronny/sdom/ui/SDLoginDialogWrapper.kt | 2 +- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index dafafbf..1b9b25e 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,6 @@ It is currently in development and not yet ready for use. ## References - [DOMjudge](https://www.domjudge.org/documentation) -- [DOMjudge API](https://www.domjudge.org/demoweb/api/doc) +- [DOMjudge API](https://domjudge.iti.kit.edu/main/api/doc) - [SimpleCodeTester plugin](https://github.com/Mr-Pine/SimpleCodeTester-IntelliJ-Plugin) - [Example Route](https://domjudge.iti.kit.edu/main/api/v4/contests/5/problems) \ No newline at end of file diff --git a/src/main/kotlin/io/gitlab/jfronny/sdom/SDCredentials.kt b/src/main/kotlin/io/gitlab/jfronny/sdom/SDCredentials.kt index 55c41a7..15d265c 100644 --- a/src/main/kotlin/io/gitlab/jfronny/sdom/SDCredentials.kt +++ b/src/main/kotlin/io/gitlab/jfronny/sdom/SDCredentials.kt @@ -24,7 +24,7 @@ object SDCredentials { } var url: String - get() = ApplicationManager.getApplication().getService(SDSettings::class.java).state.url ?: "https://domjudge.iti.kit.edu/main/api" + get() = ApplicationManager.getApplication().getService(SDSettings::class.java).state.url ?: "https://domjudge.iti.kit.edu/main" set(value) { ApplicationManager.getApplication().getService(SDSettings::class.java).state.url = value } diff --git a/src/main/kotlin/io/gitlab/jfronny/sdom/SDom.kt b/src/main/kotlin/io/gitlab/jfronny/sdom/SDom.kt index 175200c..380ddda 100644 --- a/src/main/kotlin/io/gitlab/jfronny/sdom/SDom.kt +++ b/src/main/kotlin/io/gitlab/jfronny/sdom/SDom.kt @@ -48,6 +48,7 @@ object SDom { credentials { BasicAuthCredentials(SDCredentials.credentials.first ?: "", SDCredentials.credentials.second ?: "") } + sendWithoutRequest { true } } } } @@ -80,10 +81,13 @@ object SDom { } suspend fun login(username: String, password: String, url: String) { - val fixedApi = url.trimEnd('/') + "/v4/" - val result: SDLoginResult = client.get { + val fixedApi = url.trimEnd('/').run { if (endsWith("/api/v4")) this else "$this/api/v4" } + val result: SDLoginResult = client.get(fixedApi) { + url { + appendPathSegments("user") + } + println("Using API: ${this.url}") basicAuth(username, password) - url(fixedApi + "user") }.body() if (!result.enabled) throw Exception("User is not enabled") @@ -111,7 +115,11 @@ object SDom { var judgementTypes: Map? = null suspend fun getContests() { - val result = client.get("${SDCredentials.url}contests") + val result = client.get(SDCredentials.url) { + url { + appendPathSegments("contests") + } + } if (result.status.isSuccess()) { contests = result.body>() @@ -141,7 +149,11 @@ object SDom { suspend fun getProblems() { if (currentContest == null) return - val result = client.get("${SDCredentials.url}contests/${currentContest!!.id}/problems") + val result = client.get(SDCredentials.url) { + url { + appendPathSegments("contests", currentContest!!.id, "problems") + } + } if (result.status.isSuccess()) { problems = result.body>() @@ -149,7 +161,11 @@ object SDom { currentProblem = null } } - judgementTypes = client.get("${SDCredentials.url}contests/${currentContest!!.id}/judgement-types") + judgementTypes = client.get(SDCredentials.url) { + url { + appendPathSegments("contests", currentContest!!.id, "judgement-types") + } + } .body>() .associateBy { it.id } } @@ -160,7 +176,10 @@ object SDom { val resultFlow: MutableSharedFlow> = MutableSharedFlow() launch { try { - val response: SDSubmission = client.post("${SDCredentials.url}contests/${contest.id}/submissions") { + val response: SDSubmission = client.post(SDCredentials.url) { + url { + appendPathSegments("contests", contest.id, "submissions") + } contentType(ContentType.Application.Json) setBody( SDAddSubmission( @@ -180,7 +199,12 @@ object SDom { return@launch } do { - val result: List = client.get("${SDCredentials.url}contests/${contest.id}/judgements?submission_id=${response.id}").body() + val result: List = client.get(SDCredentials.url) { + url { + appendPathSegments("contests", contest.id, "judgements") + parameters.append("submission_id", response.id) + } + }.body() if (result.isNotEmpty()) { result.forEach { resultFlow.emit(Result.success(it)) } break diff --git a/src/main/kotlin/io/gitlab/jfronny/sdom/ui/SDLoginDialogWrapper.kt b/src/main/kotlin/io/gitlab/jfronny/sdom/ui/SDLoginDialogWrapper.kt index d2f98cb..3009e8f 100644 --- a/src/main/kotlin/io/gitlab/jfronny/sdom/ui/SDLoginDialogWrapper.kt +++ b/src/main/kotlin/io/gitlab/jfronny/sdom/ui/SDLoginDialogWrapper.kt @@ -14,7 +14,7 @@ class SDLoginDialogWrapper : DialogWrapper(true) { init { title = "Log in to DOMjudge" - url = SDCredentials.url + url = SDCredentials.url.trimEnd('/').run { if (endsWith("/api/v4")) this.substring(0, this.length - 7) else this} init() }