Skip to content
This repository was archived by the owner on Apr 16, 2024. It is now read-only.

Commit 9d971f4

Browse files
committed
QuizAPI: Create and implement public QuizAPI
1 parent 1a414e9 commit 9d971f4

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package kmtt.api.quiz
2+
3+
import kmtt.api.entry.IAuthEntryAPI
4+
import kmtt.api.entry.IPublicEntryAPI
5+
import kmtt.api.entry.PublicEntryAPI
6+
import kmtt.ktor.IHttpClient
7+
import kmtt.models.enums.Website
8+
9+
internal class AuthQuizAPI(private val httpClient: IHttpClient, private val site: Website, override val token: String)
10+
: IAuthQuizAPI, IPublicQuizAPI by PublicQuizAPI(httpClient, site, token) {
11+
12+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package kmtt.api.quiz
2+
3+
import kmtt.api.common.Authable
4+
import kmtt.models.QuizResult
5+
6+
interface IPublicQuizAPI {
7+
suspend fun getResults(hash: String): QuizResult
8+
}
9+
10+
interface IAuthQuizAPI: IPublicQuizAPI, Authable {
11+
// fun vote(hash: String, entryID: Int, itemID: String): QuizResult
12+
// fun resetVote(hash: String, entryID: Int): QuizResult
13+
}
14+
//
15+
//fun IAuthQuizAPI.resetVote(hash: String, entry: Entry): QuizResult {
16+
// val id = entry.id
17+
//
18+
// requireNotNull(id) {
19+
// "ID of entry $entry is null"
20+
// }
21+
// return resetVote(hash, id.toInt())
22+
//}
23+
//
24+
//fun IAuthQuizAPI.vote(hash: String, entry: Entry, itemID: String): QuizResult {
25+
// val id = entry.id
26+
//
27+
// requireNotNull(id) {
28+
// "ID of entry $entry is null"
29+
// }
30+
// return vote(hash, id.toInt(), itemID)
31+
//}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package kmtt.api.quiz
2+
3+
import io.ktor.client.request.*
4+
import io.ktor.http.*
5+
import kmtt.ktor.IHttpClient
6+
import kmtt.ktor.request
7+
import kmtt.models.QuizResult
8+
import kmtt.models.enums.Website
9+
import kmtt.models.generic.SuccessResponse
10+
import kmtt.util.addTokenIfNotNull
11+
import kmtt.util.apiURL
12+
13+
internal class PublicQuizAPI(private val httpClient: IHttpClient, private val site: Website) : IPublicQuizAPI {
14+
// Some public requests have restrictions without authentication.
15+
// For example:
16+
// User hid his account from anonymous users, and you can't get data about this user without authentication
17+
internal var token: String? = null
18+
19+
// This authentication API is *internal* because it only used by authenticated API for delegation
20+
internal constructor(httpClient: IHttpClient, site: Website, token: String) : this(httpClient, site) {
21+
this.token = token
22+
}
23+
24+
override suspend fun getResults(hash: String): QuizResult {
25+
val endpointURL = "/quiz/$hash/results"
26+
27+
val response = httpClient.request<SuccessResponse<QuizResult>> {
28+
url(site.apiURL() + endpointURL)
29+
addTokenIfNotNull(token)
30+
method = HttpMethod.Get
31+
}
32+
33+
return response.result
34+
}
35+
}

0 commit comments

Comments
 (0)