diff --git a/core/src/commonMain/kotlin/ca/gosyer/jui/core/io/HttpResponse.kt b/core/src/commonMain/kotlin/ca/gosyer/jui/core/io/HttpResponse.kt deleted file mode 100644 index 39b9b5cf..00000000 --- a/core/src/commonMain/kotlin/ca/gosyer/jui/core/io/HttpResponse.kt +++ /dev/null @@ -1,21 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package ca.gosyer.jui.core.io - -import io.ktor.client.statement.HttpResponse -import io.ktor.client.statement.discardRemaining -import io.ktor.http.HttpStatusCode -import io.ktor.http.isSuccess - -suspend fun HttpResponse.asSuccess() : HttpResponse = apply { - if (!status.isSuccess()) { - discardRemaining() - throw HttpException(status) - } -} - -class HttpException(val status: HttpStatusCode) : IllegalStateException("HTTP error $status") \ No newline at end of file diff --git a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/BackupInteractionHandler.kt b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/BackupInteractionHandler.kt index fddc3bc8..0d5c93f3 100644 --- a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/BackupInteractionHandler.kt +++ b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/BackupInteractionHandler.kt @@ -7,7 +7,6 @@ package ca.gosyer.jui.data.server.interactions import ca.gosyer.jui.core.io.SYSTEM -import ca.gosyer.jui.core.io.asSuccess import ca.gosyer.jui.core.lang.IO import ca.gosyer.jui.data.models.BackupValidationResult import ca.gosyer.jui.data.server.Http @@ -16,6 +15,7 @@ import ca.gosyer.jui.data.server.requests.backupFileExportRequest import ca.gosyer.jui.data.server.requests.backupFileImportRequest import ca.gosyer.jui.data.server.requests.validateBackupFileRequest import io.ktor.client.call.body +import io.ktor.client.plugins.expectSuccess import io.ktor.client.request.HttpRequestBuilder import io.ktor.client.request.forms.formData import io.ktor.client.request.forms.submitFormWithBinaryData @@ -36,45 +36,45 @@ class BackupInteractionHandler @Inject constructor( serverPreferences: ServerPreferences ) : BaseInteractionHandler(client, serverPreferences) { + private fun buildFormData(file: Path) = formData { + append( + "backup.proto.gz", FileSystem.SYSTEM.source(file).buffer().readByteArray(), + Headers.build { + append(HttpHeaders.ContentType, ContentType.MultiPart.FormData.toString()) + append(HttpHeaders.ContentDisposition, "filename=backup.proto.gz") + } + ) + } + fun importBackupFile(file: Path, block: HttpRequestBuilder.() -> Unit = {}) = flow { val response = client.submitFormWithBinaryData( serverUrl + backupFileImportRequest(), - formData = formData { - append( - "backup.proto.gz", FileSystem.SYSTEM.source(file).buffer().readByteArray(), - Headers.build { - append(HttpHeaders.ContentType, ContentType.MultiPart.FormData.toString()) - append(HttpHeaders.ContentDisposition, "filename=backup.proto.gz") - } - ) - }, - block = block - ).asSuccess() + formData = buildFormData(file) + ) { + expectSuccess = true + block() + } emit(response) }.flowOn(Dispatchers.IO) fun validateBackupFile(file: Path, block: HttpRequestBuilder.() -> Unit = {}) = flow { val response = client.submitFormWithBinaryData( serverUrl + validateBackupFileRequest(), - formData = formData { - append( - "backup.proto.gz", FileSystem.SYSTEM.source(file).buffer().readByteArray(), - Headers.build { - append(HttpHeaders.ContentType, ContentType.MultiPart.FormData.toString()) - append(HttpHeaders.ContentDisposition, "filename=backup.proto.gz") - } - ) - }, - block = block - ).asSuccess().body() + formData = buildFormData(file) + ) { + expectSuccess = true + block() + }.body() emit(response) }.flowOn(Dispatchers.IO) fun exportBackupFile(block: HttpRequestBuilder.() -> Unit = {}) = flow { val response = client.get( - serverUrl + backupFileExportRequest(), - block - ).asSuccess() + serverUrl + backupFileExportRequest() + ) { + expectSuccess = true + block() + } emit(response) }.flowOn(Dispatchers.IO) } diff --git a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/CategoryInteractionHandler.kt b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/CategoryInteractionHandler.kt index 6f1887e6..a54f911a 100644 --- a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/CategoryInteractionHandler.kt +++ b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/CategoryInteractionHandler.kt @@ -6,7 +6,6 @@ package ca.gosyer.jui.data.server.interactions -import ca.gosyer.jui.core.io.asSuccess import ca.gosyer.jui.core.lang.IO import ca.gosyer.jui.data.models.Category import ca.gosyer.jui.data.models.Manga @@ -22,6 +21,7 @@ import ca.gosyer.jui.data.server.requests.getMangaCategoriesQuery import ca.gosyer.jui.data.server.requests.getMangaInCategoryQuery import ca.gosyer.jui.data.server.requests.removeMangaFromCategoryRequest import io.ktor.client.call.body +import io.ktor.client.plugins.expectSuccess import io.ktor.client.request.delete import io.ktor.client.request.forms.submitForm import io.ktor.client.request.get @@ -40,7 +40,9 @@ class CategoryInteractionHandler @Inject constructor( fun getMangaCategories(mangaId: Long) = flow { val response = client.get( serverUrl + getMangaCategoriesQuery(mangaId) - ).asSuccess().body>() + ) { + expectSuccess = true + }.body>() emit(response) }.flowOn(Dispatchers.IO) @@ -49,7 +51,9 @@ class CategoryInteractionHandler @Inject constructor( fun addMangaToCategory(mangaId: Long, categoryId: Long) = flow { val response = client.get( serverUrl + addMangaToCategoryQuery(mangaId, categoryId) - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) fun addMangaToCategory(manga: Manga, category: Category) = addMangaToCategory(manga.id, category.id) @@ -59,7 +63,9 @@ class CategoryInteractionHandler @Inject constructor( fun removeMangaFromCategory(mangaId: Long, categoryId: Long) = flow { val response = client.delete( serverUrl + removeMangaFromCategoryRequest(mangaId, categoryId) - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) fun removeMangaFromCategory(manga: Manga, category: Category) = removeMangaFromCategory(manga.id, category.id) @@ -69,7 +75,9 @@ class CategoryInteractionHandler @Inject constructor( fun getCategories(dropDefault: Boolean = false) = flow { val response = client.get( serverUrl + getCategoriesQuery() - ).asSuccess().body>().let { categories -> + ) { + expectSuccess = true + }.body>().let { categories -> if (dropDefault) { categories.filterNot { it.name.equals("default", true) } } else categories @@ -83,7 +91,9 @@ class CategoryInteractionHandler @Inject constructor( formParameters = Parameters.build { append("name", name) } - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) @@ -100,7 +110,8 @@ class CategoryInteractionHandler @Inject constructor( } ) { method = HttpMethod.Patch - }.asSuccess() + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) fun modifyCategory(category: Category, name: String? = null, isLanding: Boolean? = null) = modifyCategory(category.id, name, isLanding) @@ -114,14 +125,17 @@ class CategoryInteractionHandler @Inject constructor( } ) { method = HttpMethod.Patch - }.asSuccess() + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) fun deleteCategory(categoryId: Long) = flow { val response = client.delete( serverUrl + categoryDeleteRequest(categoryId) - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) fun deleteCategory(category: Category) = deleteCategory(category.id) @@ -129,7 +143,9 @@ class CategoryInteractionHandler @Inject constructor( fun getMangaFromCategory(categoryId: Long) = flow { val response = client.get( serverUrl + getMangaInCategoryQuery(categoryId) - ).asSuccess().body>() + ) { + expectSuccess = true + }.body>() emit(response) }.flowOn(Dispatchers.IO) fun getMangaFromCategory(category: Category) = getMangaFromCategory(category.id) diff --git a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/ChapterInteractionHandler.kt b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/ChapterInteractionHandler.kt index 498adb57..b08a8458 100644 --- a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/ChapterInteractionHandler.kt +++ b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/ChapterInteractionHandler.kt @@ -6,7 +6,6 @@ package ca.gosyer.jui.data.server.interactions -import ca.gosyer.jui.core.io.asSuccess import ca.gosyer.jui.core.lang.IO import ca.gosyer.jui.data.models.Chapter import ca.gosyer.jui.data.models.Manga @@ -21,6 +20,7 @@ import ca.gosyer.jui.data.server.requests.stopDownloadingChapterRequest import ca.gosyer.jui.data.server.requests.updateChapterMetaRequest import ca.gosyer.jui.data.server.requests.updateChapterRequest import io.ktor.client.call.body +import io.ktor.client.plugins.expectSuccess import io.ktor.client.request.HttpRequestBuilder import io.ktor.client.request.delete import io.ktor.client.request.forms.submitForm @@ -47,7 +47,8 @@ class ChapterInteractionHandler @Inject constructor( parameter("onlineFetch", true) } } - }.asSuccess().body>() + expectSuccess = true + }.body>() emit(response) }.flowOn(Dispatchers.IO) @@ -56,7 +57,9 @@ class ChapterInteractionHandler @Inject constructor( fun getChapter(mangaId: Long, chapterIndex: Int) = flow { val response = client.get( serverUrl + getChapterQuery(mangaId, chapterIndex) - ).asSuccess().body() + ) { + expectSuccess = true + }.body() emit(response) }.flowOn(Dispatchers.IO) @@ -92,7 +95,8 @@ class ChapterInteractionHandler @Inject constructor( } ) { method = HttpMethod.Patch - }.asSuccess() + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) @@ -130,9 +134,11 @@ class ChapterInteractionHandler @Inject constructor( fun getPage(mangaId: Long, chapterIndex: Int, pageNum: Int, block: HttpRequestBuilder.() -> Unit) = flow { val response = client.get( - serverUrl + getPageQuery(mangaId, chapterIndex, pageNum), - block - ).asSuccess() + serverUrl + getPageQuery(mangaId, chapterIndex, pageNum) + ) { + expectSuccess = true + block() + } emit(response) }.flowOn(Dispatchers.IO) @@ -145,7 +151,9 @@ class ChapterInteractionHandler @Inject constructor( fun deleteChapterDownload(mangaId: Long, chapterIndex: Int) = flow { val response = client.delete( serverUrl + deleteDownloadedChapterRequest(mangaId, chapterIndex) - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) @@ -158,7 +166,9 @@ class ChapterInteractionHandler @Inject constructor( fun queueChapterDownload(mangaId: Long, chapterIndex: Int) = flow { val response = client.get( serverUrl + queueDownloadChapterRequest(mangaId, chapterIndex) - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) @@ -171,7 +181,9 @@ class ChapterInteractionHandler @Inject constructor( fun stopChapterDownload(mangaId: Long, chapterIndex: Int) = flow { val response = client.delete( serverUrl + stopDownloadingChapterRequest(mangaId, chapterIndex) - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) @@ -190,7 +202,8 @@ class ChapterInteractionHandler @Inject constructor( } ) { method = HttpMethod.Patch - }.asSuccess() + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) diff --git a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/DownloadInteractionHandler.kt b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/DownloadInteractionHandler.kt index 12fe3d25..75a678d5 100644 --- a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/DownloadInteractionHandler.kt +++ b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/DownloadInteractionHandler.kt @@ -6,13 +6,13 @@ package ca.gosyer.jui.data.server.interactions -import ca.gosyer.jui.core.io.asSuccess import ca.gosyer.jui.core.lang.IO import ca.gosyer.jui.data.server.Http import ca.gosyer.jui.data.server.ServerPreferences import ca.gosyer.jui.data.server.requests.downloadsClearRequest import ca.gosyer.jui.data.server.requests.downloadsStartRequest import ca.gosyer.jui.data.server.requests.downloadsStopRequest +import io.ktor.client.plugins.expectSuccess import io.ktor.client.request.get import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.flow @@ -27,21 +27,27 @@ class DownloadInteractionHandler @Inject constructor( fun startDownloading() = flow { val response = client.get( serverUrl + downloadsStartRequest() - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) fun stopDownloading() = flow { val response = client.get( serverUrl + downloadsStopRequest() - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) fun clearDownloadQueue() = flow { val response = client.get( serverUrl + downloadsClearRequest() - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) } diff --git a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/ExtensionInteractionHandler.kt b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/ExtensionInteractionHandler.kt index 3640df46..80dd926b 100644 --- a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/ExtensionInteractionHandler.kt +++ b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/ExtensionInteractionHandler.kt @@ -6,7 +6,6 @@ package ca.gosyer.jui.data.server.interactions -import ca.gosyer.jui.core.io.asSuccess import ca.gosyer.jui.core.lang.IO import ca.gosyer.jui.data.models.Extension import ca.gosyer.jui.data.server.Http @@ -17,6 +16,7 @@ import ca.gosyer.jui.data.server.requests.apkUninstallQuery import ca.gosyer.jui.data.server.requests.apkUpdateQuery import ca.gosyer.jui.data.server.requests.extensionListQuery import io.ktor.client.call.body +import io.ktor.client.plugins.expectSuccess import io.ktor.client.request.HttpRequestBuilder import io.ktor.client.request.get import io.ktor.client.statement.bodyAsChannel @@ -33,36 +33,46 @@ class ExtensionInteractionHandler @Inject constructor( fun getExtensionList() = flow { val response = client.get( serverUrl + extensionListQuery() - ).asSuccess().body>() + ) { + expectSuccess = true + }.body>() emit(response) }.flowOn(Dispatchers.IO) fun installExtension(extension: Extension) = flow { val response = client.get( serverUrl + apkInstallQuery(extension.pkgName) - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) fun updateExtension(extension: Extension) = flow { val response = client.get( serverUrl + apkUpdateQuery(extension.pkgName) - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) fun uninstallExtension(extension: Extension) = flow { val response = client.get( serverUrl + apkUninstallQuery(extension.pkgName) - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) fun getApkIcon(extension: Extension, block: HttpRequestBuilder.() -> Unit) = flow { val response = client.get( - serverUrl + apkIconQuery(extension.apkName), - block - ).asSuccess().bodyAsChannel() + serverUrl + apkIconQuery(extension.apkName) + ) { + expectSuccess = true + block() + }.bodyAsChannel() emit(response) }.flowOn(Dispatchers.IO) } diff --git a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/LibraryInteractionHandler.kt b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/LibraryInteractionHandler.kt index 5ba340da..c660a5b7 100644 --- a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/LibraryInteractionHandler.kt +++ b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/LibraryInteractionHandler.kt @@ -6,13 +6,13 @@ package ca.gosyer.jui.data.server.interactions -import ca.gosyer.jui.core.io.asSuccess import ca.gosyer.jui.core.lang.IO import ca.gosyer.jui.data.models.Manga import ca.gosyer.jui.data.server.Http import ca.gosyer.jui.data.server.ServerPreferences import ca.gosyer.jui.data.server.requests.addMangaToLibraryQuery import ca.gosyer.jui.data.server.requests.removeMangaFromLibraryRequest +import io.ktor.client.plugins.expectSuccess import io.ktor.client.request.delete import io.ktor.client.request.get import kotlinx.coroutines.Dispatchers @@ -28,7 +28,9 @@ class LibraryInteractionHandler @Inject constructor( fun addMangaToLibrary(mangaId: Long) = flow { val response = client.get( serverUrl + addMangaToLibraryQuery(mangaId) - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) @@ -37,7 +39,9 @@ class LibraryInteractionHandler @Inject constructor( fun removeMangaFromLibrary(mangaId: Long) = flow { val response = client.delete( serverUrl + removeMangaFromLibraryRequest(mangaId) - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) diff --git a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/MangaInteractionHandler.kt b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/MangaInteractionHandler.kt index ab457a02..bb4cc22b 100644 --- a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/MangaInteractionHandler.kt +++ b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/MangaInteractionHandler.kt @@ -6,7 +6,6 @@ package ca.gosyer.jui.data.server.interactions -import ca.gosyer.jui.core.io.asSuccess import ca.gosyer.jui.core.lang.IO import ca.gosyer.jui.data.models.Manga import ca.gosyer.jui.data.server.Http @@ -15,6 +14,7 @@ import ca.gosyer.jui.data.server.requests.mangaQuery import ca.gosyer.jui.data.server.requests.mangaThumbnailQuery import ca.gosyer.jui.data.server.requests.updateMangaMetaRequest import io.ktor.client.call.body +import io.ktor.client.plugins.expectSuccess import io.ktor.client.request.HttpRequestBuilder import io.ktor.client.request.forms.submitForm import io.ktor.client.request.get @@ -41,7 +41,8 @@ class MangaInteractionHandler @Inject constructor( parameter("onlineFetch", true) } } - }.asSuccess().body() + expectSuccess = true + }.body() emit(response) }.flowOn(Dispatchers.IO) @@ -49,9 +50,11 @@ class MangaInteractionHandler @Inject constructor( fun getMangaThumbnail(mangaId: Long, block: HttpRequestBuilder.() -> Unit) = flow { val response = client.get( - serverUrl + mangaThumbnailQuery(mangaId), - block - ).asSuccess().bodyAsChannel() + serverUrl + mangaThumbnailQuery(mangaId) + ) { + expectSuccess = true + block() + }.bodyAsChannel() emit(response) }.flowOn(Dispatchers.IO) @@ -64,7 +67,8 @@ class MangaInteractionHandler @Inject constructor( } ) { method = HttpMethod.Patch - }.asSuccess() + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) diff --git a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/SettingsInteractionHandler.kt b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/SettingsInteractionHandler.kt index 8e07a7ec..ec48a644 100644 --- a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/SettingsInteractionHandler.kt +++ b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/SettingsInteractionHandler.kt @@ -6,7 +6,6 @@ package ca.gosyer.jui.data.server.interactions -import ca.gosyer.jui.core.io.asSuccess import ca.gosyer.jui.core.lang.IO import ca.gosyer.jui.data.models.About import ca.gosyer.jui.data.server.Http @@ -14,6 +13,7 @@ import ca.gosyer.jui.data.server.ServerPreferences import ca.gosyer.jui.data.server.requests.aboutQuery import ca.gosyer.jui.data.server.requests.checkUpdateQuery import io.ktor.client.call.body +import io.ktor.client.plugins.expectSuccess import io.ktor.client.request.get import io.ktor.client.request.post import kotlinx.coroutines.Dispatchers @@ -29,14 +29,18 @@ class SettingsInteractionHandler @Inject constructor( fun aboutServer() = flow { val response = client.get( serverUrl + aboutQuery() - ).asSuccess().body() + ) { + expectSuccess = true + }.body() emit(response) }.flowOn(Dispatchers.IO) fun checkUpdate() = flow { val response = client.post( serverUrl + checkUpdateQuery() - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) } diff --git a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/SourceInteractionHandler.kt b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/SourceInteractionHandler.kt index e5d4639d..646a691e 100644 --- a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/SourceInteractionHandler.kt +++ b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/SourceInteractionHandler.kt @@ -6,7 +6,6 @@ package ca.gosyer.jui.data.server.interactions -import ca.gosyer.jui.core.io.asSuccess import ca.gosyer.jui.core.lang.IO import ca.gosyer.jui.data.models.MangaPage import ca.gosyer.jui.data.models.Source @@ -27,6 +26,7 @@ import ca.gosyer.jui.data.server.requests.sourcePopularQuery import ca.gosyer.jui.data.server.requests.sourceSearchQuery import ca.gosyer.jui.data.server.requests.updateSourceSettingQuery import io.ktor.client.call.body +import io.ktor.client.plugins.expectSuccess import io.ktor.client.request.get import io.ktor.client.request.parameter import io.ktor.client.request.post @@ -48,14 +48,18 @@ class SourceInteractionHandler @Inject constructor( fun getSourceList() = flow { val response = client.get( serverUrl + sourceListQuery() - ).asSuccess().body>() + ) { + expectSuccess = true + }.body>() emit(response) }.flowOn(Dispatchers.IO) fun getSourceInfo(sourceId: Long) = flow { val response = client.get( serverUrl + sourceInfoQuery(sourceId) - ).asSuccess().body() + ) { + expectSuccess = true + }.body() emit(response) }.flowOn(Dispatchers.IO) @@ -64,7 +68,9 @@ class SourceInteractionHandler @Inject constructor( fun getPopularManga(sourceId: Long, pageNum: Int) = flow { val response = client.get( serverUrl + sourcePopularQuery(sourceId, pageNum) - ).asSuccess().body() + ) { + expectSuccess = true + }.body() emit(response) }.flowOn(Dispatchers.IO) @@ -76,7 +82,9 @@ class SourceInteractionHandler @Inject constructor( fun getLatestManga(sourceId: Long, pageNum: Int) = flow { val response = client.get( serverUrl + sourceLatestQuery(sourceId, pageNum) - ).asSuccess().body() + ) { + expectSuccess = true + }.body() emit(response) }.flowOn(Dispatchers.IO) @@ -95,7 +103,8 @@ class SourceInteractionHandler @Inject constructor( parameter("searchTerm", searchTerm) } } - }.asSuccess() + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) @@ -109,7 +118,8 @@ class SourceInteractionHandler @Inject constructor( parameter("searchTerm", searchTerm) } } - }.asSuccess().body() + expectSuccess = true + }.body() emit(response) }.flowOn(Dispatchers.IO) @@ -128,7 +138,8 @@ class SourceInteractionHandler @Inject constructor( parameter("reset", true) } } - }.asSuccess().body>() + expectSuccess = true + }.body>() emit(response) }.flowOn(Dispatchers.IO) @@ -140,7 +151,8 @@ class SourceInteractionHandler @Inject constructor( ) { contentType(ContentType.Application.Json) setBody(sourceFilter) - }.asSuccess() + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) @@ -160,7 +172,9 @@ class SourceInteractionHandler @Inject constructor( fun getSourceSettings(sourceId: Long) = flow { val response = client.get( serverUrl + getSourceSettingsQuery(sourceId) - ).asSuccess().body>() + ) { + expectSuccess = true + }.body>() emit(response) }.flowOn(Dispatchers.IO) @@ -172,7 +186,8 @@ class SourceInteractionHandler @Inject constructor( ) { contentType(ContentType.Application.Json) setBody(sourcePreference) - }.asSuccess() + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) diff --git a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/UpdatesInteractionHandler.kt b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/UpdatesInteractionHandler.kt index 74951e28..1a982fd0 100644 --- a/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/UpdatesInteractionHandler.kt +++ b/data/src/commonMain/kotlin/ca/gosyer/jui/data/server/interactions/UpdatesInteractionHandler.kt @@ -6,7 +6,6 @@ package ca.gosyer.jui.data.server.interactions -import ca.gosyer.jui.core.io.asSuccess import ca.gosyer.jui.core.lang.IO import ca.gosyer.jui.data.models.Category import ca.gosyer.jui.data.models.Updates @@ -15,6 +14,7 @@ import ca.gosyer.jui.data.server.ServerPreferences import ca.gosyer.jui.data.server.requests.fetchUpdatesRequest import ca.gosyer.jui.data.server.requests.recentUpdatesQuery import io.ktor.client.call.body +import io.ktor.client.plugins.expectSuccess import io.ktor.client.request.forms.submitForm import io.ktor.client.request.get import io.ktor.client.request.post @@ -32,14 +32,18 @@ class UpdatesInteractionHandler @Inject constructor( fun getRecentUpdates(pageNum: Int) = flow { val response = client.get( serverUrl + recentUpdatesQuery(pageNum) - ).asSuccess().body() + ) { + expectSuccess = true + }.body() emit(response) }.flowOn(Dispatchers.IO) fun updateLibrary() = flow { val response = client.post( serverUrl + fetchUpdatesRequest() - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) @@ -49,7 +53,9 @@ class UpdatesInteractionHandler @Inject constructor( formParameters = Parameters.build { append("category", categoryId.toString()) } - ).asSuccess() + ) { + expectSuccess = true + } emit(response) }.flowOn(Dispatchers.IO) diff --git a/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/base/image/KamelConfigProvider.kt b/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/base/image/KamelConfigProvider.kt index d9406891..95f36e38 100644 --- a/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/base/image/KamelConfigProvider.kt +++ b/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/base/image/KamelConfigProvider.kt @@ -6,7 +6,6 @@ package ca.gosyer.jui.ui.base.image -import ca.gosyer.jui.core.io.HttpException import ca.gosyer.jui.data.models.Extension import ca.gosyer.jui.data.models.Manga import ca.gosyer.jui.data.models.Source @@ -26,14 +25,13 @@ import io.kamel.core.fetcher.Fetcher import io.kamel.core.mapper.Mapper import io.kamel.image.config.imageBitmapDecoder import io.ktor.client.HttpClient +import io.ktor.client.plugins.expectSuccess import io.ktor.client.plugins.onDownload import io.ktor.client.request.request import io.ktor.client.request.takeFrom import io.ktor.client.request.url import io.ktor.client.statement.bodyAsChannel -import io.ktor.client.statement.discardRemaining import io.ktor.http.Url -import io.ktor.http.isSuccess import io.ktor.utils.io.ByteReadChannel import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope @@ -105,14 +103,10 @@ class KamelConfigProvider @Inject constructor( } takeFrom(resourceConfig.requestData) url(data) + expectSuccess = true } - if (response.status.isSuccess()) { - val bytes = response.bodyAsChannel() - send(Resource.Success(bytes)) - } else { - response.discardRemaining() - send(Resource.Failure(HttpException(response.status))) - } + val bytes = response.bodyAsChannel() + send(Resource.Success(bytes)) } } } diff --git a/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/util/compose/Image.kt b/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/util/compose/Image.kt index 228d8e7d..66103575 100644 --- a/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/util/compose/Image.kt +++ b/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/util/compose/Image.kt @@ -8,9 +8,9 @@ package ca.gosyer.jui.ui.util.compose import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.toComposeImageBitmap -import ca.gosyer.jui.core.io.asSuccess import ca.gosyer.jui.data.server.Http import io.ktor.client.call.body +import io.ktor.client.plugins.expectSuccess import io.ktor.client.request.HttpRequestBuilder import io.ktor.client.request.get import io.ktor.client.statement.HttpResponse @@ -25,7 +25,10 @@ fun imageFromFile(file: Path): ImageBitmap { } suspend fun imageFromUrl(client: Http, url: String, block: HttpRequestBuilder.() -> Unit): ImageBitmap { - return client.get(url, block).asSuccess().toImageBitmap() + return client.get(url) { + expectSuccess = true + block() + }.toImageBitmap() } actual suspend fun HttpResponse.toImageBitmap(): ImageBitmap {