chore: clean up API url handling

This commit is contained in:
Johannes Frohnmeyer 2024-05-12 16:42:04 +02:00
parent 74bbc91ea4
commit e8e55b8706
Signed by: Johannes
GPG Key ID: E76429612C2929F4
4 changed files with 35 additions and 11 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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<String, SDJudgementType>? = 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<List<Contest>>()
@ -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<List<Problem>>()
@ -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<List<SDJudgementType>>()
.associateBy { it.id }
}
@ -160,7 +176,10 @@ object SDom {
val resultFlow: MutableSharedFlow<Result<SDJudgement>> = 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<SDJudgement> = client.get("${SDCredentials.url}contests/${contest.id}/judgements?submission_id=${response.id}").body()
val result: List<SDJudgement> = 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

View File

@ -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()
}