chore: clean up API url handling
This commit is contained in:
parent
74bbc91ea4
commit
e8e55b8706
@ -8,6 +8,6 @@ It is currently in development and not yet ready for use.
|
|||||||
|
|
||||||
## References
|
## References
|
||||||
- [DOMjudge](https://www.domjudge.org/documentation)
|
- [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)
|
- [SimpleCodeTester plugin](https://github.com/Mr-Pine/SimpleCodeTester-IntelliJ-Plugin)
|
||||||
- [Example Route](https://domjudge.iti.kit.edu/main/api/v4/contests/5/problems)
|
- [Example Route](https://domjudge.iti.kit.edu/main/api/v4/contests/5/problems)
|
@ -24,7 +24,7 @@ object SDCredentials {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var url: String
|
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) {
|
set(value) {
|
||||||
ApplicationManager.getApplication().getService(SDSettings::class.java).state.url = value
|
ApplicationManager.getApplication().getService(SDSettings::class.java).state.url = value
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,7 @@ object SDom {
|
|||||||
credentials {
|
credentials {
|
||||||
BasicAuthCredentials(SDCredentials.credentials.first ?: "", SDCredentials.credentials.second ?: "")
|
BasicAuthCredentials(SDCredentials.credentials.first ?: "", SDCredentials.credentials.second ?: "")
|
||||||
}
|
}
|
||||||
|
sendWithoutRequest { true }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -80,10 +81,13 @@ object SDom {
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun login(username: String, password: String, url: String) {
|
suspend fun login(username: String, password: String, url: String) {
|
||||||
val fixedApi = url.trimEnd('/') + "/v4/"
|
val fixedApi = url.trimEnd('/').run { if (endsWith("/api/v4")) this else "$this/api/v4" }
|
||||||
val result: SDLoginResult = client.get {
|
val result: SDLoginResult = client.get(fixedApi) {
|
||||||
|
url {
|
||||||
|
appendPathSegments("user")
|
||||||
|
}
|
||||||
|
println("Using API: ${this.url}")
|
||||||
basicAuth(username, password)
|
basicAuth(username, password)
|
||||||
url(fixedApi + "user")
|
|
||||||
}.body()
|
}.body()
|
||||||
|
|
||||||
if (!result.enabled) throw Exception("User is not enabled")
|
if (!result.enabled) throw Exception("User is not enabled")
|
||||||
@ -111,7 +115,11 @@ object SDom {
|
|||||||
var judgementTypes: Map<String, SDJudgementType>? = null
|
var judgementTypes: Map<String, SDJudgementType>? = null
|
||||||
|
|
||||||
suspend fun getContests() {
|
suspend fun getContests() {
|
||||||
val result = client.get("${SDCredentials.url}contests")
|
val result = client.get(SDCredentials.url) {
|
||||||
|
url {
|
||||||
|
appendPathSegments("contests")
|
||||||
|
}
|
||||||
|
}
|
||||||
if (result.status.isSuccess()) {
|
if (result.status.isSuccess()) {
|
||||||
contests = result.body<List<Contest>>()
|
contests = result.body<List<Contest>>()
|
||||||
|
|
||||||
@ -141,7 +149,11 @@ object SDom {
|
|||||||
|
|
||||||
suspend fun getProblems() {
|
suspend fun getProblems() {
|
||||||
if (currentContest == null) return
|
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()) {
|
if (result.status.isSuccess()) {
|
||||||
problems = result.body<List<Problem>>()
|
problems = result.body<List<Problem>>()
|
||||||
|
|
||||||
@ -149,7 +161,11 @@ object SDom {
|
|||||||
currentProblem = null
|
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>>()
|
.body<List<SDJudgementType>>()
|
||||||
.associateBy { it.id }
|
.associateBy { it.id }
|
||||||
}
|
}
|
||||||
@ -160,7 +176,10 @@ object SDom {
|
|||||||
val resultFlow: MutableSharedFlow<Result<SDJudgement>> = MutableSharedFlow()
|
val resultFlow: MutableSharedFlow<Result<SDJudgement>> = MutableSharedFlow()
|
||||||
launch {
|
launch {
|
||||||
try {
|
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)
|
contentType(ContentType.Application.Json)
|
||||||
setBody(
|
setBody(
|
||||||
SDAddSubmission(
|
SDAddSubmission(
|
||||||
@ -180,7 +199,12 @@ object SDom {
|
|||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
do {
|
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()) {
|
if (result.isNotEmpty()) {
|
||||||
result.forEach { resultFlow.emit(Result.success(it)) }
|
result.forEach { resultFlow.emit(Result.success(it)) }
|
||||||
break
|
break
|
||||||
|
@ -14,7 +14,7 @@ class SDLoginDialogWrapper : DialogWrapper(true) {
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
title = "Log in to DOMjudge"
|
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()
|
init()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user