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

Commit

Permalink
API: add UserAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
DareFox committed Apr 22, 2022
1 parent acae88a commit d21f863
Show file tree
Hide file tree
Showing 5 changed files with 466 additions and 0 deletions.
165 changes: 165 additions & 0 deletions src/main/kotlin/kmtt/api/user/AuthUserAPI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package kmtt.api.user

import io.ktor.client.request.*
import io.ktor.http.*
import kmtt.constants.Content
import kmtt.ktor.IHttpClient
import kmtt.ktor.request
import kmtt.models.Notification
import kmtt.models.comment.Comment
import kmtt.models.entry.Entry
import kmtt.models.enums.Website
import kmtt.models.generic.SuccessResponse
import kmtt.models.subsite.Subsite
import kmtt.util.addTokenIfNotNull
import kmtt.util.apiURL

internal class AuthUserAPI(
private val httpClient: IHttpClient,
private val site: Website,
override val token: String,
): IAuthUserAPI, IPublicUserAPI by PublicUserAPI(httpClient, site, token) {
override suspend fun getMe(): Subsite {
val endpointURL = "/user/me"

val response = httpClient.request<SuccessResponse<Subsite>> {
url(site.apiURL() + endpointURL)
addTokenIfNotNull(token)
method = HttpMethod.Get
}

return response.result
}

override suspend fun getMyNotifications(): List<Notification> {
val endpointURL = "/user/me/updates"

val response = httpClient.request<SuccessResponse<List<Notification>>> {
url(site.apiURL() + endpointURL)
addTokenIfNotNull(token)
method = HttpMethod.Get
}

return response.result
}

override suspend fun getMyNotificationsCount(): Int {
@kotlinx.serialization.Serializable
data class Count(val count: Int)

val endpointURL = "/user/me/updates/count"

val response = httpClient.request<SuccessResponse<Count>> {
url(site.apiURL() + endpointURL)
addTokenIfNotNull(token)
method = HttpMethod.Get
}

return response.result.count
}

override suspend fun readNotificationsById(id: Long): Boolean {
val endpointURL = "/user/me/updates/read/$id"

val response = httpClient.request<SuccessResponse<SuccessResponse<Boolean>>> {
url(site.apiURL() + endpointURL)
addTokenIfNotNull(token)
method = HttpMethod.Post
}

return response.result.result
}

override suspend fun readAllNotifications(): List<Notification> {
val endpointURL = "/user/me/updates?is_read=1"

val response = httpClient.request<SuccessResponse<List<Notification>>> {
url(site.apiURL() + endpointURL)
addTokenIfNotNull(token)
method = HttpMethod.Get
}

return response.result
}

override suspend fun getMyComments(count: Int, offset: Int): List<Comment> {
val endpointURL = "/user/me/comments"
val params = mutableListOf<Pair<String, String>>()

params.add("count" to count.toString())
params.add("offset" to offset.toString())

val query = params.formUrlEncode().let {
"?$it"
}

val response = httpClient.request<SuccessResponse<List<Comment>>> {
url(site.apiURL() + endpointURL + query)
addTokenIfNotNull(token)
method = HttpMethod.Get
}

return response.result
}

override suspend fun getMyEntries(count: Int, offset: Int): List<Entry> {
val endpointURL = "/user/me/comments"
val params = mutableListOf<Pair<String, String>>()

params.add("count" to count.toString())
params.add("offset" to offset.toString())

val query = params.formUrlEncode().let {
"?$it"
}

val response = httpClient.request<SuccessResponse<List<Entry>>> {
url(site.apiURL() + endpointURL + query)
addTokenIfNotNull(token)
method = HttpMethod.Get
}

return response.result
}

override suspend fun getMyFavoriteEntries(count: Int, offset: Int): List<Entry> {
val endpointURL = "/user/me/favorites/entries"
val params = mutableListOf<Pair<String, String>>()

params.add("count" to count.toString())
params.add("offset" to offset.toString())

val query = params.formUrlEncode().let {
"?$it"
}

val response = httpClient.request<SuccessResponse<List<Entry>>> {
url(site.apiURL() + endpointURL + query)
addTokenIfNotNull(token)
method = HttpMethod.Get
}

return response.result
}

override suspend fun getMyFavoriteComments(count: Int, offset: Int): List<Comment> {
val endpointURL = "/user/me/favorites/comments"
val params = mutableListOf<Pair<String, String>>()

params.add("count" to count.toString())
params.add("offset" to offset.toString())

val query = params.formUrlEncode().let {
"?$it"
}

val response = httpClient.request<SuccessResponse<List<Comment>>> {
url(site.apiURL() + endpointURL + query)
addTokenIfNotNull(token)
method = HttpMethod.Get
}

return response.result
}

}
35 changes: 35 additions & 0 deletions src/main/kotlin/kmtt/api/user/IPublicUserAPI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kmtt.api.user

import kmtt.api.common.Authable
import kmtt.models.Notification
import kmtt.models.comment.Comment
import kmtt.models.entry.Entry
import kmtt.models.subsite.Subsite

interface IPublicUserAPI {
suspend fun getUserByID(userID: Long): Subsite

suspend fun getUserComments(userID: Long, count: Int, offset: Int): List<Comment>

suspend fun getUserEntries(userID: Long, count: Int, offset: Int): List<Entry>
}

interface IAuthUserAPI : IPublicUserAPI, Authable {
suspend fun getMe(): Subsite

suspend fun getMyNotifications(): List<Notification>

suspend fun getMyNotificationsCount(): Int

suspend fun readNotificationsById(id: Long): Boolean

suspend fun readAllNotifications(): List<Notification>

suspend fun getMyComments(count: Int, offset: Int): List<Comment>

suspend fun getMyEntries(count: Int, offset: Int): List<Entry>

suspend fun getMyFavoriteEntries(count: Int, offset: Int): List<Entry>

suspend fun getMyFavoriteComments(count: Int, offset: Int): List<Comment>
}
77 changes: 77 additions & 0 deletions src/main/kotlin/kmtt/api/user/PublicUserAPI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package kmtt.api.user

import io.ktor.client.request.*
import io.ktor.http.*
import kmtt.ktor.IHttpClient
import kmtt.ktor.request
import kmtt.models.comment.Comment
import kmtt.models.entry.Entry
import kmtt.models.enums.Website
import kmtt.models.generic.SuccessResponse
import kmtt.models.subsite.Subsite
import kmtt.util.addTokenIfNotNull
import kmtt.util.apiURL

internal class PublicUserAPI(private val httpClient: IHttpClient, private val site: Website) : IPublicUserAPI {
// 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 getUserByID(userID: Long): Subsite {
val endpointURL = "/user/$userID"

val response = httpClient.request<SuccessResponse<Subsite>> {
url(site.apiURL() + endpointURL)
addTokenIfNotNull(token)
method = HttpMethod.Get
}

return response.result
}

override suspend fun getUserComments(userID: Long, count: Int, offset: Int): List<Comment> {
val endpointURL = "/user/$userID/comments"
val params = mutableListOf<Pair<String, String>>()

params.add("count" to count.toString())
params.add("offset" to offset.toString())

val query = params.formUrlEncode().let {
"?$it"
}

val response = httpClient.request<SuccessResponse<List<Comment>>> {
url(site.apiURL() + endpointURL + query)
addTokenIfNotNull(token)
method = HttpMethod.Get
}

return response.result
}

override suspend fun getUserEntries(userID: Long, count: Int, offset: Int): List<Entry> {
val endpointURL = "/user/$userID/entries"
val params = mutableListOf<Pair<String, String>>()

params.add("count" to count.toString())
params.add("offset" to offset.toString())

val query = params.formUrlEncode().let {
"?$it"
}

val response = httpClient.request<SuccessResponse<List<Entry>>> {
url(site.apiURL() + endpointURL + query)
addTokenIfNotNull(token)
method = HttpMethod.Get
}

return response.result
}
}
Loading

0 comments on commit d21f863

Please sign in to comment.