Skip to content

Commit

Permalink
dialog api update response
Browse files Browse the repository at this point in the history
  • Loading branch information
Данила Беляков authored and Данила Беляков committed Aug 1, 2024
1 parent 71f596b commit fb83c3c
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.alice.ktx.api.common

sealed interface Response<T>{
data class Failed<T>(val message: String): Response<T>
data class Success<T>(val data: T): Response<T>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.alice.ktx.api.common.extensions

import com.github.alice.ktx.api.common.Response
import com.github.alice.ktx.api.dialog.yandex.models.ErrorBody
import io.ktor.client.call.*
import io.ktor.client.statement.*

suspend inline fun <reified T> HttpResponse.response(): Response<T> {
return if(status.value in 200..299)
Response.Success(this.body())
else
Response.Failed(this.body<ErrorBody>().message)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.github.alice.ktx.api.dialog

import com.github.alice.ktx.api.common.Response
import com.github.alice.ktx.api.dialog.yandex.models.image.response.ImageUpload
import com.github.alice.ktx.api.dialog.yandex.models.image.response.Images
import com.github.alice.ktx.api.dialog.yandex.models.status.Status
import java.io.File

interface DialogApi {
suspend fun getStatus(): Status
suspend fun uploadImage(url: String): ImageUpload
suspend fun uploadImage(file: File): ImageUpload
suspend fun getAllImages(): Images
suspend fun deleteImage(id: String): Boolean
suspend fun getStatus(): Response<Status>
suspend fun uploadImage(url: String): Response<ImageUpload>
suspend fun uploadImage(file: File): Response<ImageUpload>
suspend fun getAllImages(): Response<Images>
suspend fun deleteImage(id: String): Response<Unit>
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.github.alice.ktx.api.dialog.yandex.impl

import com.github.alice.ktx.Skill
import com.github.alice.ktx.api.common.Response
import com.github.alice.ktx.api.common.extensions.response
import com.github.alice.ktx.api.dialog.DialogApi
import com.github.alice.ktx.api.dialog.yandex.models.ErrorBody
import com.github.alice.ktx.api.dialog.yandex.models.image.request.ImageUploadUrl
import com.github.alice.ktx.api.dialog.yandex.models.image.response.DeleteImage
import com.github.alice.ktx.api.dialog.yandex.models.image.response.ImageUpload
Expand All @@ -14,6 +17,7 @@ import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.request.*
import io.ktor.client.request.forms.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.util.*
Expand Down Expand Up @@ -58,19 +62,19 @@ class KtorYandexDialogApi(
}
}

override suspend fun getStatus(): Status {
return client.get("api/v1/status").body()
override suspend fun getStatus(): Response<Status> {
return client.get("api/v1/status").response()
}

override suspend fun uploadImage(url: String): ImageUpload {
override suspend fun uploadImage(url: String): Response<ImageUpload> {
val body = ImageUploadUrl(url = url)
return client.post("api/v1/skills/$skillId/images") {
contentType(ContentType.Application.Json)
setBody(body)
}.body()
}.response()
}

override suspend fun uploadImage(file: File): ImageUpload {
override suspend fun uploadImage(file: File): Response<ImageUpload> {
return client.submitFormWithBinaryData(
url = "api/v1/skills/$skillId/images",
formData = formData {
Expand All @@ -79,15 +83,14 @@ class KtorYandexDialogApi(
append(HttpHeaders.ContentDisposition, "filename=\"${file.name}\"")
})
}
).body()
).response()
}

override suspend fun getAllImages(): Images {
return client.get("api/v1/skills/$skillId/images").body()
override suspend fun getAllImages(): Response<Images> {
return client.get("api/v1/skills/$skillId/images").response()
}

override suspend fun deleteImage(id: String): Boolean {
val response = client.delete("api/v1/skills/$skillId/images/$id")
return response.status == HttpStatusCode.OK && response.body<DeleteImage>().result == SUCCESS_MESSAGE
override suspend fun deleteImage(id: String): Response<Unit> {
return client.delete("api/v1/skills/$skillId/images/$id").response()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.alice.ktx.api.dialog.yandex.models

import kotlinx.serialization.Serializable

@Serializable
data class ErrorBody(
val message: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ class KtorWebServer(
}catch (e: Throwable) {
listener.responseFailure(model, e)?.let { response ->
call.respond(response)
return@post
}
throw e
}
}
}
Expand Down
36 changes: 23 additions & 13 deletions examples/src/main/kotlin/com/github/examples/ImageDialogsApi.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.examples

import com.github.alice.ktx.Dispatcher
import com.github.alice.ktx.api.common.Response
import com.github.alice.ktx.api.dialog.yandex.impl.ktorYandexDialogApi
import com.github.alice.ktx.dispatch
import com.github.alice.ktx.handlers.message
Expand All @@ -27,17 +28,21 @@ fun main() {
message({ message.request.payload?.keys?.contains("delete_image_id") == true }) {

val imageId = message.request.payload!!["delete_image_id"].toString()
val result = dialogApi?.deleteImage(imageId) ?: false
val result = dialogApi?.deleteImage(imageId)
response {
text = if(result) "Success" else "Failure"
text = if(result is Response.Success) "Success" else "Failure"
}
}

message({ message.request.originalUtterance == "upload_image_file" }) {
val file = File("ktor_icon.png")
val result = dialogApi?.uploadImage(file)
val response = dialogApi?.uploadImage(file)
response {
text = result?.image.toString()
text = when(response) {
is Response.Failed -> response.message
is Response.Success -> response.data.image.toString()
null -> throw NullPointerException("Response was null")
}
}
}
}
Expand All @@ -47,15 +52,20 @@ fun main() {
private fun Dispatcher.messageCardImages() {
message({ message.session.new }) {
val imageResponse = dialogApi?.getAllImages()
response {
cardItemsList {
header = "Images (${imageResponse?.total})"
imageResponse?.images?.forEach { image ->
item {
imageId = image.id
mediaButton {
text = "Delete"
payload = mapOf("delete_image_id" to image.id)
when(imageResponse) {
is Response.Failed, null -> response {
text = "Failed"
}
is Response.Success -> response {
cardItemsList {
header = "Images (${imageResponse.data.total})"
imageResponse.data.images.forEach { image ->
item {
imageId = image.id
mediaButton {
text = "Delete"
payload = mapOf("delete_image_id" to image.id)
}
}
}
}
Expand Down

0 comments on commit fb83c3c

Please sign in to comment.