From 6d6bed2eb075bb62ac74c3fd63b48e05173caead Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 14 Aug 2020 18:05:58 +0600 Subject: [PATCH] add unified way to make post and get requests from client --- .../content/ReadContentRepoKtorClient.kt | 21 +++++------ .../content/WriteContentRepoKtorClient.kt | 24 ++++++------ .../client/post/ReadPostsRepoKtorClient.kt | 37 +++++++++---------- .../client/post/WritePostsRepoKtorClient.kt | 36 ++++++++---------- .../ktor/client/StandardHttpClientGetPost.kt | 28 ++++++++++++++ 5 files changed, 81 insertions(+), 65 deletions(-) create mode 100644 ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/client/StandardHttpClientGetPost.kt diff --git a/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/content/ReadContentRepoKtorClient.kt b/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/content/ReadContentRepoKtorClient.kt index 568824bd..4ec4c212 100644 --- a/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/content/ReadContentRepoKtorClient.kt +++ b/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/content/ReadContentRepoKtorClient.kt @@ -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( - "$baseUrl/$getContentByIdRoute/$id" - ).let { - standardKtorSerialFormat.load(RegisteredContent.serializer().nullable, it) - } + override suspend fun getContentById(id: ContentId): RegisteredContent? = client.uniget( + "$baseUrl/$getContentByIdRoute/$id", + RegisteredContent.serializer().nullable + ) - override suspend fun getContentByPagination(pagination: Pagination): PaginationResult = client.get( - "$baseUrl/$getContentByPaginationRoute".includeQueryParams( - pagination.asUrlQueryParts - ) - ).let { - standardKtorSerialFormat.load(registeredContentPaginationResultSerializer, it) - } + override suspend fun getContentByPagination(pagination: Pagination): PaginationResult = client.uniget( + "$baseUrl/$getContentByPaginationRoute".includeQueryParams(pagination.asUrlQueryParts), + registeredContentPaginationResultSerializer + ) } \ No newline at end of file diff --git a/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/content/WriteContentRepoKtorClient.kt b/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/content/WriteContentRepoKtorClient.kt index e7233e21..04126f49 100644 --- a/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/content/WriteContentRepoKtorClient.kt +++ b/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/content/WriteContentRepoKtorClient.kt @@ -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 { - 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 { - 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() + ) } \ No newline at end of file diff --git a/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/post/ReadPostsRepoKtorClient.kt b/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/post/ReadPostsRepoKtorClient.kt index d5d9559f..768253f0 100644 --- a/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/post/ReadPostsRepoKtorClient.kt +++ b/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/post/ReadPostsRepoKtorClient.kt @@ -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( - "$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 = client.get( - "$baseUrl/$getPostsByContentRoute/$id" - ).let { - standardKtorSerialFormat.load(registeredPostsListSerializer, it) - } + override suspend fun getPostsByContent(id: ContentId): List = client.uniget( + "$baseUrl/$getPostsByContentRoute/$id", + registeredPostsListSerializer + ) override suspend fun getPostsByCreatingDates( from: DateTime, to: DateTime, pagination: Pagination - ): PaginationResult = client.get( + ): PaginationResult = client.uniget( "$baseUrl/$getPostsByCreatingDatesRoute".includeQueryParams( (from to to).asFromToUrlPart + pagination.asUrlQueryParts - ) - ).let { - standardKtorSerialFormat.load(registeredPostsPaginationResultSerializer, it) - } + ), + registeredPostsPaginationResultSerializer + ) - override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult = client.get( + override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult = client.uniget( "$baseUrl/$getPostsByPaginationRoute".includeQueryParams( pagination.asUrlQueryParts - ) - ).let { - standardKtorSerialFormat.load(registeredPostsPaginationResultSerializer, it) - } + ), + registeredPostsPaginationResultSerializer + ) } \ No newline at end of file diff --git a/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/post/WritePostsRepoKtorClient.kt b/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/post/WritePostsRepoKtorClient.kt index b4d271ce..ec4c283d 100644 --- a/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/post/WritePostsRepoKtorClient.kt +++ b/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/post/WritePostsRepoKtorClient.kt @@ -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 { - 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 { - 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 { - 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() + ) } diff --git a/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/client/StandardHttpClientGetPost.kt b/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/client/StandardHttpClientGetPost.kt new file mode 100644 index 00000000..ed42c57c --- /dev/null +++ b/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/client/StandardHttpClientGetPost.kt @@ -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 = Pair, T> + +suspend fun HttpClient.uniget( + url: String, + resultDeserializer: DeserializationStrategy +) = get( + url +).let { + standardKtorSerialFormat.load(resultDeserializer, it) +} + +suspend fun HttpClient.unipost( + url: String, + bodyInfo: BodyPair, + resultDeserializer: DeserializationStrategy +) = post(url) { + body = standardKtorSerialFormat.dump(bodyInfo.first, bodyInfo.second) +}.let { + standardKtorSerialFormat.load(resultDeserializer, it) +}