Skip to content

Commit

Permalink
Fix image generation error response
Browse files Browse the repository at this point in the history
  • Loading branch information
adriantache committed Dec 5, 2024
1 parent 7caabd0 commit 39116ba
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package data.httpClient

import getPlatform
import io.ktor.client.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.logging.*
Expand All @@ -14,7 +15,13 @@ val realHttpClient = HttpClient {

if (ENABLE_LOGGING) {
install(Logging) {
logger = Logger.ANDROID
logger = if (getPlatform().name.contains("Android")) {
Logger.ANDROID
} else {
// This doesn't seem to work on desktop...
Logger.DEFAULT
}

level = LogLevel.ALL
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package data.imageGeneration

import data.imageGeneration.service.ApiCaller
import domain.imageGeneration.data.ImageGenerationRepository
import domain.imageGeneration.data.model.ImageResultData

// Note: Skipping repository here since there is no complex data processing.
class ImageGenerationDataSource(
private val apiService: ApiCaller = ApiCaller(),
) : ImageGenerationRepository {
override suspend fun generateImage(prompt: String): String {
override suspend fun generateImage(prompt: String): ImageResultData {
return apiService.getImage(prompt)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ import kotlinx.serialization.Serializable
@Serializable
data class OpenAiImageResponse(
val created: Long?,
val data: List<ImageData>,
val data: List<ImageData>?,
val error: ImageError?,
) {
val url = data.first().url
val url = data?.first()?.url
}

@Serializable
data class ImageData(
val url: String,
)

@Serializable
data class ImageError(
val code: String,
val message: String,
val type: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import data.imageGeneration.model.OpenAiImageRequest
import data.imageGeneration.model.OpenAiImageResponse
import data.settings.dataSource.SettingsDataSource
import data.settings.dataSource.SettingsDataSourceImpl
import domain.imageGeneration.data.model.ImageResultData
import domain.imageGeneration.data.model.ImageResultData.ImageResultError
import domain.imageGeneration.data.model.ImageResultData.ImageResultSuccess
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.*
Expand Down Expand Up @@ -38,7 +41,7 @@ class ApiCaller(
}
}

suspend fun getImage(prompt: String): String {
suspend fun getImage(prompt: String): ImageResultData {
val apiKey = settingsDataSource.getSettings().apiKey
?: error("ERROR: Api key should be present!")

Expand All @@ -52,6 +55,9 @@ class ApiCaller(
}
}

return result.body<OpenAiImageResponse>().url
val resultBody = result.body<OpenAiImageResponse>()

return resultBody.url?.let { ImageResultSuccess(it) }
?: ImageResultError(resultBody.error?.message.orEmpty())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package domain.imageGeneration

import data.imageGeneration.ImageGenerationDataSource
import domain.imageGeneration.data.ImageGenerationRepository
import domain.imageGeneration.data.model.ImageResultData.ImageResultError
import domain.imageGeneration.data.model.ImageResultData.ImageResultSuccess
import domain.imageGeneration.entity.ImagePrompt
import domain.imageGeneration.entity.ImageResult
import domain.imageGeneration.state.ImageGenerationState
Expand Down Expand Up @@ -40,9 +42,12 @@ object ImageGenerationUseCases {
_state.value = ImageGenerationState.Loading

scope.launch {
val imageResultData = repository.generateImage(prompt)

val imageResult = ImageResult(
image = repository.generateImage(prompt),
imageUrl = (imageResultData as? ImageResultSuccess)?.imageUrl,
imagePrompt = prompt,
errorMessage = (imageResultData as? ImageResultError)?.errorMessage,
)
_state.value = ImageGenerationState.Result(
result = imageResult.toUi(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package domain.imageGeneration.data

import domain.imageGeneration.data.model.ImageResultData

interface ImageGenerationRepository {
suspend fun generateImage(prompt: String): String
suspend fun generateImage(prompt: String): ImageResultData
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package domain.imageGeneration.data.model

sealed interface ImageResultData {
data class ImageResultSuccess(val imageUrl: String) : ImageResultData
data class ImageResultError(val errorMessage: String) : ImageResultData
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package domain.imageGeneration.entity

data class ImageResult(
val image: String,
val imageUrl: String?,
val imagePrompt: String,
val errorMessage: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import domain.imageGeneration.entity.ImageResult
import domain.imageGeneration.ui.model.ImageResultUi

fun ImageResult.toUi() = ImageResultUi(
image = image,
imagePrompt = imagePrompt
imageUrl = imageUrl,
imagePrompt = imagePrompt,
errorMessage = errorMessage,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package domain.imageGeneration.ui.model

data class ImageResultUi(
val image: String,
val imageUrl: String?,
val imagePrompt: String,
val errorMessage: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,27 @@ fun ImageGenerationResultScreen(

Spacer(Modifier.height(16.dp))

AsyncImage(
modifier = Modifier.fillMaxWidth()
.weight(1f)
.clickable { clipboard.setText(AnnotatedString(imageGenerationItem.image)) },
contentDescription = imageGenerationItem.imageInput,
model = imageGenerationItem.image,
)
imageGenerationItem.image?.let {
AsyncImage(
modifier = Modifier.fillMaxWidth()
.weight(1f)
.clickable { clipboard.setText(AnnotatedString(imageGenerationItem.image)) },
contentDescription = imageGenerationItem.imageInput,
model = imageGenerationItem.image,
)
}

imageGenerationItem.errorMessage?.let {
Text(
modifier = Modifier.fillMaxWidth()
.weight(1f)
.padding(16.dp)
.clickable { imageGenerationItem.onReset() },
text = "Error: $it",
color = AppColor.error(),
textAlign = TextAlign.Center,
)
}

Spacer(Modifier.height(16.dp))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package presentation.imageGeneration.model

data class ImageGenerationResultItem(
val image: String,
val image: String?,
val imageInput: String,
val onReset: () -> Unit,
val errorMessage: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class ImageGenerationPresenter {

fun getResultItem(localState: ImageGenerationState.Result): ImageGenerationResultItem {
return ImageGenerationResultItem(
image = localState.result.image,
image = localState.result.imageUrl,
imageInput = localState.result.imagePrompt,
onReset = localState.onReset,
errorMessage = localState.result.errorMessage,
)
}
}

0 comments on commit 39116ba

Please sign in to comment.