From 9d971f4c9dd8190e889b68ba5130dca20f5ed089 Mon Sep 17 00:00:00 2001 From: darefox Date: Mon, 18 Jul 2022 01:19:19 +0300 Subject: [PATCH] QuizAPI: Create and implement public QuizAPI --- src/main/kotlin/kmtt/api/quiz/AuthQuizAPI.kt | 12 +++++++ .../kotlin/kmtt/api/quiz/IPublicQuizAPI.kt | 31 ++++++++++++++++ .../kotlin/kmtt/api/quiz/PublicQuizAPI.kt | 35 +++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 src/main/kotlin/kmtt/api/quiz/AuthQuizAPI.kt create mode 100644 src/main/kotlin/kmtt/api/quiz/IPublicQuizAPI.kt create mode 100644 src/main/kotlin/kmtt/api/quiz/PublicQuizAPI.kt diff --git a/src/main/kotlin/kmtt/api/quiz/AuthQuizAPI.kt b/src/main/kotlin/kmtt/api/quiz/AuthQuizAPI.kt new file mode 100644 index 00000000..53ef468b --- /dev/null +++ b/src/main/kotlin/kmtt/api/quiz/AuthQuizAPI.kt @@ -0,0 +1,12 @@ +package kmtt.api.quiz + +import kmtt.api.entry.IAuthEntryAPI +import kmtt.api.entry.IPublicEntryAPI +import kmtt.api.entry.PublicEntryAPI +import kmtt.ktor.IHttpClient +import kmtt.models.enums.Website + +internal class AuthQuizAPI(private val httpClient: IHttpClient, private val site: Website, override val token: String) + : IAuthQuizAPI, IPublicQuizAPI by PublicQuizAPI(httpClient, site, token) { + +} \ No newline at end of file diff --git a/src/main/kotlin/kmtt/api/quiz/IPublicQuizAPI.kt b/src/main/kotlin/kmtt/api/quiz/IPublicQuizAPI.kt new file mode 100644 index 00000000..421bad61 --- /dev/null +++ b/src/main/kotlin/kmtt/api/quiz/IPublicQuizAPI.kt @@ -0,0 +1,31 @@ +package kmtt.api.quiz + +import kmtt.api.common.Authable +import kmtt.models.QuizResult + +interface IPublicQuizAPI { + suspend fun getResults(hash: String): QuizResult +} + +interface IAuthQuizAPI: IPublicQuizAPI, Authable { +// fun vote(hash: String, entryID: Int, itemID: String): QuizResult +// fun resetVote(hash: String, entryID: Int): QuizResult +} +// +//fun IAuthQuizAPI.resetVote(hash: String, entry: Entry): QuizResult { +// val id = entry.id +// +// requireNotNull(id) { +// "ID of entry $entry is null" +// } +// return resetVote(hash, id.toInt()) +//} +// +//fun IAuthQuizAPI.vote(hash: String, entry: Entry, itemID: String): QuizResult { +// val id = entry.id +// +// requireNotNull(id) { +// "ID of entry $entry is null" +// } +// return vote(hash, id.toInt(), itemID) +//} \ No newline at end of file diff --git a/src/main/kotlin/kmtt/api/quiz/PublicQuizAPI.kt b/src/main/kotlin/kmtt/api/quiz/PublicQuizAPI.kt new file mode 100644 index 00000000..17e4e342 --- /dev/null +++ b/src/main/kotlin/kmtt/api/quiz/PublicQuizAPI.kt @@ -0,0 +1,35 @@ +package kmtt.api.quiz + +import io.ktor.client.request.* +import io.ktor.http.* +import kmtt.ktor.IHttpClient +import kmtt.ktor.request +import kmtt.models.QuizResult +import kmtt.models.enums.Website +import kmtt.models.generic.SuccessResponse +import kmtt.util.addTokenIfNotNull +import kmtt.util.apiURL + +internal class PublicQuizAPI(private val httpClient: IHttpClient, private val site: Website) : IPublicQuizAPI { + // Some public requests have restrictions without authentication. + // For example: + // User hid his account from anonymous users, and you can't get data about this user without authentication + internal var token: String? = null + + // This authentication API is *internal* because it only used by authenticated API for delegation + internal constructor(httpClient: IHttpClient, site: Website, token: String) : this(httpClient, site) { + this.token = token + } + + override suspend fun getResults(hash: String): QuizResult { + val endpointURL = "/quiz/$hash/results" + + val response = httpClient.request> { + url(site.apiURL() + endpointURL) + addTokenIfNotNull(token) + method = HttpMethod.Get + } + + return response.result + } +} \ No newline at end of file