add writable parts of server and client
This commit is contained in:
ClientPart
ClientServerCommon/src/main/kotlin/com/insanusmokrassar/postssystem/core/clientserver/common
Core/src/main/kotlin/com/insanusmokrassar/postssystem/core/content
ServerPart
@ -32,6 +32,7 @@ dependencies {
|
||||
api project(":ClientServerCommon")
|
||||
|
||||
api "io.ktor:ktor-client:$ktor_version"
|
||||
api "io.ktor:ktor-client-websockets:$ktor_version"
|
||||
}
|
||||
|
||||
compileKotlin {
|
||||
|
@ -0,0 +1,40 @@
|
||||
package com.insanusmokrassar.postssystem.core.client
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.features.websocket.ws
|
||||
import io.ktor.http.HttpMethod
|
||||
import io.ktor.http.cio.websocket.Frame
|
||||
import io.ktor.http.cio.websocket.readText
|
||||
import kotlinx.coroutines.InternalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.FlowCollector
|
||||
|
||||
class EventFlow<T>(
|
||||
private val client: HttpClient,
|
||||
private val host: String,
|
||||
private val port: Int,
|
||||
private val path: String,
|
||||
private val dataConverter: suspend (String) -> T
|
||||
) : Flow<T> {
|
||||
@InternalCoroutinesApi
|
||||
override suspend fun collect(collector: FlowCollector<T>) {
|
||||
client.ws(
|
||||
method = HttpMethod.Get,
|
||||
host = host,
|
||||
port = port,
|
||||
path = path
|
||||
) {
|
||||
var frame = incoming.receiveOrClosed()
|
||||
while (!frame.isClosed) {
|
||||
val frameVal = frame.value
|
||||
when (frameVal) {
|
||||
is Frame.Text -> {
|
||||
collector.emit(
|
||||
dataConverter(frameVal.readText())
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,28 +1,46 @@
|
||||
package com.insanusmokrassar.postssystem.core.client
|
||||
|
||||
import com.insanusmokrassar.postssystem.core.api.WritePostsAPI
|
||||
import com.insanusmokrassar.postssystem.core.clientserver.common.*
|
||||
import com.insanusmokrassar.postssystem.core.clientserver.common.models.UpdatePostRequest
|
||||
import com.insanusmokrassar.postssystem.core.post.*
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.request.post
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
class WritableHttpPostsAPI(
|
||||
|
||||
private val hostUrl: String,
|
||||
private val port: Int,
|
||||
private val client: HttpClient
|
||||
) : WritePostsAPI {
|
||||
override val postCreatedFlow: Flow<Post>
|
||||
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
|
||||
override val postDeletedFlow: Flow<Post>
|
||||
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
|
||||
override val postUpdatedFlow: Flow<Post>
|
||||
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
|
||||
|
||||
override suspend fun createPost(content: PostContents): Post? {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
private inline fun postEventFlow(url: String) = EventFlow(
|
||||
client,
|
||||
hostUrl,
|
||||
port,
|
||||
"/$url"
|
||||
) {
|
||||
Json.plain.parse(SimplePost.serializer(), it)
|
||||
}
|
||||
|
||||
override suspend fun deletePost(id: PostId): Boolean {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
override val postCreatedFlow: Flow<Post> = postEventFlow(eventPostsCreatedAddress)
|
||||
override val postDeletedFlow: Flow<Post> = postEventFlow(eventPostsDeletedAddress)
|
||||
override val postUpdatedFlow: Flow<Post> = postEventFlow(eventPostsUpdatedAddress)
|
||||
|
||||
override suspend fun createPost(content: PostContents): Post? = client.post<SimplePost>(
|
||||
createPostAddress
|
||||
) {
|
||||
body = content
|
||||
}
|
||||
|
||||
override suspend fun updatePostContent(postId: PostId, content: PostContents): Boolean {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
override suspend fun deletePost(id: PostId): Boolean = client.post(deletePostAddress) {
|
||||
body = id
|
||||
}
|
||||
|
||||
override suspend fun updatePostContent(
|
||||
postId: PostId,
|
||||
content: PostContents
|
||||
): Boolean = client.post(createPostAddress) {
|
||||
body = UpdatePostRequest(postId, content)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user