This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
466 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.