add unified way to make post and get requests from client

This commit is contained in:
InsanusMokrassar 2020-08-14 18:05:58 +06:00
parent 8c3a48db99
commit 6d6bed2eb0
5 changed files with 81 additions and 65 deletions

View File

@ -5,6 +5,7 @@ import com.insanusmokrassar.postssystem.core.content.RegisteredContent
import com.insanusmokrassar.postssystem.core.content.api.ReadContentRepo
import com.insanusmokrassar.postssystem.core.ktor.*
import com.insanusmokrassar.postssystem.ktor.*
import com.insanusmokrassar.postssystem.ktor.client.uniget
import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
import io.ktor.client.HttpClient
@ -21,17 +22,13 @@ class ReadContentRepoKtorClient(
standardKtorSerialFormat.load(contentIdsSerializer, it)
}
override suspend fun getContentById(id: ContentId): RegisteredContent? = client.get<ByteArray>(
"$baseUrl/$getContentByIdRoute/$id"
).let {
standardKtorSerialFormat.load(RegisteredContent.serializer().nullable, it)
}
override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<RegisteredContent> = client.get<ByteArray>(
"$baseUrl/$getContentByPaginationRoute".includeQueryParams(
pagination.asUrlQueryParts
override suspend fun getContentById(id: ContentId): RegisteredContent? = client.uniget(
"$baseUrl/$getContentByIdRoute/$id",
RegisteredContent.serializer().nullable
)
override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<RegisteredContent> = client.uniget(
"$baseUrl/$getContentByPaginationRoute".includeQueryParams(pagination.asUrlQueryParts),
registeredContentPaginationResultSerializer
)
).let {
standardKtorSerialFormat.load(registeredContentPaginationResultSerializer, it)
}
}

View File

@ -3,7 +3,7 @@ package com.insanusmokrassar.postssystem.core.ktor.client.content
import com.insanusmokrassar.postssystem.core.content.*
import com.insanusmokrassar.postssystem.core.content.api.WriteContentRepo
import com.insanusmokrassar.postssystem.core.ktor.*
import com.insanusmokrassar.postssystem.ktor.client.createStandardWebsocketFlow
import com.insanusmokrassar.postssystem.ktor.client.*
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.client.HttpClient
import io.ktor.client.request.post
@ -28,17 +28,15 @@ class WriteContentRepoKtorClient(
standardKtorSerialFormat.load(RegisteredContent.serializer(), it)
}
override suspend fun registerContent(content: Content): RegisteredContent? = client.post<ByteArray> {
url("$baseUrl/$registerContentRoute")
body = standardKtorSerialFormat.dump(Content.serializer(), content)
}.let {
standardKtorSerialFormat.load(RegisteredContent.serializer().nullable, it)
}
override suspend fun registerContent(content: Content): RegisteredContent? = client.unipost(
"$baseUrl/$registerContentRoute",
BodyPair(Content.serializer(), content),
RegisteredContent.serializer().nullable
)
override suspend fun deleteContent(id: ContentId): Boolean = client.post<ByteArray> {
url("$baseUrl/$deleteContentRoute")
body = standardKtorSerialFormat.dump(ContentId.serializer(), id)
}.let {
standardKtorSerialFormat.load(Boolean.serializer(), it)
}
override suspend fun deleteContent(id: ContentId): Boolean = client.unipost(
"$baseUrl/$deleteContentRoute",
BodyPair(ContentId.serializer(), id),
Boolean.serializer()
)
}

View File

@ -6,6 +6,7 @@ import com.insanusmokrassar.postssystem.core.post.PostId
import com.insanusmokrassar.postssystem.core.post.RegisteredPost
import com.insanusmokrassar.postssystem.core.post.repo.ReadPostsRepo
import com.insanusmokrassar.postssystem.ktor.*
import com.insanusmokrassar.postssystem.ktor.client.uniget
import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
import com.soywiz.klock.DateTime
@ -23,35 +24,31 @@ class ReadPostsRepoKtorClient(
standardKtorSerialFormat.load(postIdsSerializer, it)
}
override suspend fun getPostById(id: PostId): RegisteredPost? = client.get<ByteArray>(
"$baseUrl/$getPostByIdRoute/$id"
).let {
standardKtorSerialFormat.load(RegisteredPost.serializer().nullable, it)
}
override suspend fun getPostById(id: PostId): RegisteredPost? = client.uniget(
"$baseUrl/$getPostByIdRoute/$id",
RegisteredPost.serializer().nullable
)
override suspend fun getPostsByContent(id: ContentId): List<RegisteredPost> = client.get<ByteArray>(
"$baseUrl/$getPostsByContentRoute/$id"
).let {
standardKtorSerialFormat.load(registeredPostsListSerializer, it)
}
override suspend fun getPostsByContent(id: ContentId): List<RegisteredPost> = client.uniget(
"$baseUrl/$getPostsByContentRoute/$id",
registeredPostsListSerializer
)
override suspend fun getPostsByCreatingDates(
from: DateTime,
to: DateTime,
pagination: Pagination
): PaginationResult<RegisteredPost> = client.get<ByteArray>(
): PaginationResult<RegisteredPost> = client.uniget(
"$baseUrl/$getPostsByCreatingDatesRoute".includeQueryParams(
(from to to).asFromToUrlPart + pagination.asUrlQueryParts
),
registeredPostsPaginationResultSerializer
)
).let {
standardKtorSerialFormat.load(registeredPostsPaginationResultSerializer, it)
}
override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<RegisteredPost> = client.get<ByteArray>(
override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<RegisteredPost> = client.uniget(
"$baseUrl/$getPostsByPaginationRoute".includeQueryParams(
pagination.asUrlQueryParts
),
registeredPostsPaginationResultSerializer
)
).let {
standardKtorSerialFormat.load(registeredPostsPaginationResultSerializer, it)
}
}

View File

@ -3,7 +3,7 @@ package com.insanusmokrassar.postssystem.core.ktor.client.post
import com.insanusmokrassar.postssystem.core.ktor.*
import com.insanusmokrassar.postssystem.core.post.*
import com.insanusmokrassar.postssystem.core.post.repo.WritePostsRepo
import com.insanusmokrassar.postssystem.ktor.client.createStandardWebsocketFlow
import com.insanusmokrassar.postssystem.ktor.client.*
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.client.HttpClient
import io.ktor.client.features.websocket.WebSockets
@ -35,25 +35,21 @@ class WritePostsRepoKtorClient (
standardKtorSerialFormat.load(RegisteredPost.serializer(), it)
}
override suspend fun createPost(post: Post): RegisteredPost? = client.post<ByteArray> {
url("$baseUrl/$createPostRoute")
body = standardKtorSerialFormat.dump(Post.serializer(), post)
}.let {
standardKtorSerialFormat.load(RegisteredPost.serializer().nullable, it)
}
override suspend fun createPost(post: Post): RegisteredPost? = client.unipost(
"$baseUrl/$createPostRoute",
BodyPair(Post.serializer(), post),
RegisteredPost.serializer().nullable
)
override suspend fun deletePost(id: PostId): Boolean = client.post<ByteArray> {
url("$baseUrl/$deletePostRoute")
body = standardKtorSerialFormat.dump(PostId.serializer(), id)
}.let {
standardKtorSerialFormat.load(Boolean.serializer(), it)
}
override suspend fun updatePostContent(postId: PostId, post: Post): Boolean = client.post<ByteArray> {
url("$baseUrl/$updatePostContentRoute")
body = standardKtorSerialFormat.dump(UpdatePostObject.serializer(), UpdatePostObject(postId, post))
}.let {
standardKtorSerialFormat.load(Boolean.serializer(), it)
}
override suspend fun deletePost(id: PostId): Boolean = client.unipost(
"$baseUrl/$deletePostRoute",
BodyPair(PostId.serializer(), id),
Boolean.serializer()
)
override suspend fun updatePostContent(postId: PostId, post: Post): Boolean = client.unipost(
"$baseUrl/$updatePostContentRoute",
BodyPair(UpdatePostObject.serializer(), UpdatePostObject(postId, post)),
Boolean.serializer()
)
}

View File

@ -0,0 +1,28 @@
package com.insanusmokrassar.postssystem.ktor.client
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.request.post
import kotlinx.serialization.*
typealias BodyPair<T> = Pair<SerializationStrategy<T>, T>
suspend fun <ResultType> HttpClient.uniget(
url: String,
resultDeserializer: DeserializationStrategy<ResultType>
) = get<ByteArray>(
url
).let {
standardKtorSerialFormat.load(resultDeserializer, it)
}
suspend fun <BodyType, ResultType> HttpClient.unipost(
url: String,
bodyInfo: BodyPair<BodyType>,
resultDeserializer: DeserializationStrategy<ResultType>
) = post<ByteArray>(url) {
body = standardKtorSerialFormat.dump(bodyInfo.first, bodyInfo.second)
}.let {
standardKtorSerialFormat.load(resultDeserializer, it)
}