diff --git a/build.gradle.kts b/build.gradle.kts index f1501f9e..4b50ade7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,6 +2,7 @@ import com.vanniktech.maven.publish.SonatypeHost import fr.brouillard.oss.jgitver.Strategies +import love.forte.plugin.suspendtrans.configuration.SuspendTransformConfigurations.kotlinJsExportIgnoreClassInfo import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeSimulatorTest import ru.vyarus.gradle.plugin.mkdocs.task.MkdocsTask @@ -17,8 +18,7 @@ plugins { alias(libs.plugins.dokka) alias(libs.plugins.mkdocs) alias(libs.plugins.jgitver) - alias(libs.plugins.ksp) - alias(libs.plugins.ktorfit) + alias(libs.plugins.suspendTransformCompiler) id("maven-publish") } @@ -85,7 +85,6 @@ kotlin { implementation(libs.kotlinx.serialization.json) implementation(libs.ktor.client.content.negotiation) implementation(libs.ktor.serialization.kotlinx.json) - implementation(libs.ktorfit) } val nonJsMain by creating { dependsOn(commonMain.get()) } @@ -123,8 +122,13 @@ kotlin { } } -// Gradle complains if we don't have this; unclear why. -tasks.getByName("sourcesJar").dependsOn("kspCommonMainKotlinMetadata") +suspendTransformPlugin { + transformers { + addJvmAsync() + addJvmBlocking() + addJsPromise { addCopyAnnotationExclude { from(kotlinJsExportIgnoreClassInfo) } } + } +} // Standalone mode is missing certificates, which causes our LiveTest to fail. // This should resolve it but requires us to start the simulator(s) beforehand. diff --git a/demo-app/src/commonMain/kotlin/co/pokeapi/pokekotlin/demoapp/DemoApp.kt b/demo-app/src/commonMain/kotlin/co/pokeapi/pokekotlin/demoapp/DemoApp.kt index 0b1e2d3b..e8d32d29 100644 --- a/demo-app/src/commonMain/kotlin/co/pokeapi/pokekotlin/demoapp/DemoApp.kt +++ b/demo-app/src/commonMain/kotlin/co/pokeapi/pokekotlin/demoapp/DemoApp.kt @@ -1,26 +1,11 @@ package co.pokeapi.pokekotlin.demoapp import androidx.compose.foundation.clickable -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.PaddingValues -import androidx.compose.foundation.layout.consumeWindowInsets -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items -import androidx.compose.material3.CircularProgressIndicator -import androidx.compose.material3.ColorScheme -import androidx.compose.material3.ListItem -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Scaffold -import androidx.compose.material3.Text -import androidx.compose.material3.TopAppBar -import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue +import androidx.compose.material3.* +import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.text.style.TextOverflow @@ -67,7 +52,7 @@ fun PokemonList(padding: PaddingValues, pokemon: NamedApiResourceList) { LazyColumn(contentPadding = padding) { items(pokemon.results) { summary -> var result by remember { mutableStateOf?>(null) } - LaunchedEffect(Unit) { result = PokeApi.getPokemonSpecies(summary.id) } + LaunchedEffect(Unit) { result = runCatching { PokeApi.getPokemonSpecies(summary.id) } } result ?.onSuccess { PokemonListItem(it) } ?.onFailure { PokemonListItemError(summary, it.message ?: "Unknown error") } @@ -104,16 +89,15 @@ fun DemoApp() { topBar = { TopAppBar(title = { Text("PokeKotlin Demo") }) }, content = { innerPadding -> var result by remember { mutableStateOf?>(null) } - LaunchedEffect(Unit) { result = PokeApi.getPokemonSpeciesList(0, 100000) } - when { - result == null -> CenteredLoading(innerPadding) - result!!.isSuccess -> PokemonList(innerPadding, result!!.getOrThrow()) - result!!.isFailure -> + LaunchedEffect(Unit) { result = runCatching { PokeApi.getPokemonSpeciesList(0, 100000) } } + result + ?.onSuccess { PokemonList(innerPadding, it) } + ?.onFailure { ErrorMessage( padding = innerPadding, message = result!!.exceptionOrNull()!!.message ?: "Unknown error", ) - } + } ?: CenteredLoading(innerPadding) }, ) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 24321f8f..1ee081db 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,13 +9,12 @@ ktor = "3.1.3" gradle-android = "8.7.2" gradle-compose = "1.8.1" gradle-kotlin = "2.1.21" -gradle-ksp = "2.1.21-2.0.1" # first part is Kotlin version -gradle-ktorfit = "2.5.2" gradle-dokka = "2.0.0" gradle-jgitver = "0.10.0-rc03" gradle-mavenPublish = "0.32.0" gradle-mkdocs = "4.0.1" gradle-spotless = "7.0.4" +gradle-suspendTransformCompiler = "2.1.20-0.12.0" tool-prettier = "3.5.3" [libraries] @@ -26,7 +25,6 @@ kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutine kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" } kotlinx-io = { module = "org.jetbrains.kotlinx:kotlinx-io-core", version.ref = "kotlinx-io" } kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" } -ktorfit = { module = "de.jensklingenberg.ktorfit:ktorfit-lib", version.ref = "gradle-ktorfit" } ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" } ktor-client-darwin = { module = "io.ktor:ktor-client-darwin", version.ref = "ktor" } ktor-client-winhttp = { module = "io.ktor:ktor-client-winhttp", version.ref = "ktor" } @@ -44,8 +42,7 @@ jgitver = { id = "fr.brouillard.oss.gradle.jgitver", version.ref = "gradle-jgitv kotlin-composeCompiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "gradle-kotlin" } kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "gradle-kotlin" } kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "gradle-kotlin" } -ksp = { id = "com.google.devtools.ksp", version.ref = "gradle-ksp" } -ktorfit = { id = "de.jensklingenberg.ktorfit", version.ref = "gradle-ktorfit" } mavenPublish = { id = "com.vanniktech.maven.publish", version.ref = "gradle-mavenPublish" } mkdocs = { id = "ru.vyarus.mkdocs-build", version.ref = "gradle-mkdocs" } spotless = { id = "com.diffplug.spotless", version.ref = "gradle-spotless" } +suspendTransformCompiler = { id = "love.forte.plugin.suspend-transform", version.ref = "gradle-suspendTransformCompiler" } \ No newline at end of file diff --git a/src/commonMain/kotlin/co/pokeapi/pokekotlin/PokeApi.kt b/src/commonMain/kotlin/co/pokeapi/pokekotlin/PokeApi.kt index 6af435f8..a459abf0 100644 --- a/src/commonMain/kotlin/co/pokeapi/pokekotlin/PokeApi.kt +++ b/src/commonMain/kotlin/co/pokeapi/pokekotlin/PokeApi.kt @@ -1,367 +1,457 @@ package co.pokeapi.pokekotlin -import co.pokeapi.pokekotlin.internal.ResultConverter -import co.pokeapi.pokekotlin.internal.createPokeApiKtorfitBuilder +import co.pokeapi.pokekotlin.internal.JsOnlyExport +import co.pokeapi.pokekotlin.internal.PokeApiJson import co.pokeapi.pokekotlin.internal.getDefaultEngine import co.pokeapi.pokekotlin.model.* -import de.jensklingenberg.ktorfit.http.GET -import de.jensklingenberg.ktorfit.http.Path -import de.jensklingenberg.ktorfit.http.Query import io.ktor.client.* +import io.ktor.client.call.* import io.ktor.client.engine.* +import io.ktor.client.plugins.* +import io.ktor.client.plugins.cache.* import io.ktor.client.plugins.cache.storage.* - -public fun createPokeApi( - baseUrl: String = "https://pokeapi.co/api/v2/", - cacheStorage: CacheStorage? = null, - engine: HttpClientEngine = getDefaultEngine(), - configure: HttpClientConfig<*>.() -> Unit = {}, -): PokeApi { - return createPokeApiKtorfitBuilder( - baseUrl = baseUrl, - cacheStorage = cacheStorage, - engine = engine, - configure = configure, +import io.ktor.client.plugins.contentnegotiation.* +import io.ktor.client.request.* +import io.ktor.http.* +import io.ktor.serialization.kotlinx.json.* +import kotlin.js.ExperimentalJsStatic +import kotlin.js.JsExport +import kotlin.js.JsName +import love.forte.plugin.suspendtrans.annotation.JsPromise +import love.forte.plugin.suspendtrans.annotation.JvmAsync +import love.forte.plugin.suspendtrans.annotation.JvmBlocking + +private suspend inline fun HttpClient.getBodyWithOffsetAndLimit( + path: String, + offset: Int, + limit: Int, +): T = + get(path) { + parameter("offset", offset) + parameter("limit", limit) + } + .body() + +private suspend inline fun HttpClient.getBody(path: String): T = get(path).body() + +@JsOnlyExport +public sealed class PokeApi +@JsExport.Ignore +constructor( + baseUrl: String, + engine: HttpClientEngine, + cacheStorage: CacheStorage?, + configure: HttpClientConfig<*>.() -> Unit, +) { + private val client = + HttpClient(engine) { + configure() + defaultRequest { url(baseUrl) } + install(HttpCache) { cacheStorage?.let { privateStorage(it) } } + install(ContentNegotiation) { json(PokeApiJson, ContentType.Any) } + expectSuccess = true + } + + public class Custom + @JsExport.Ignore + public constructor( + baseUrl: String = "https://pokeapi.co/", + engine: HttpClientEngine = getDefaultEngine(), + cacheStorage: CacheStorage? = null, + configure: HttpClientConfig<*>.() -> Unit = {}, + ) : PokeApi(baseUrl, engine, cacheStorage, configure) { + @JsName("create") + public constructor(baseUrl: String = "https://pokeapi.co/") : this(baseUrl, configure = {}) + } + + @OptIn(ExperimentalJsStatic::class) + public companion object Default : + PokeApi( + baseUrl = "https://pokeapi.co/", + engine = getDefaultEngine(), + cacheStorage = null, + configure = {}, ) - .converterFactories(ResultConverter.Factory) - .build() - .createPokeApi() -} - -public interface PokeApi { - public companion object : PokeApi by createPokeApi() // region Resource Lists // region Berries - @GET("berry/") - public suspend fun getBerryList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("berry-firmness/") - public suspend fun getBerryFirmnessList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("berry-flavor/") - public suspend fun getBerryFlavorList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getBerryList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/berry", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getBerryFirmnessList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/berry-firmness", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getBerryFlavorList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/berry-flavor", offset, limit) // endregion Berries // region Contests - @GET("contest-type/") - public suspend fun getContestTypeList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("contest-effect/") - public suspend fun getContestEffectList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("super-contest-effect/") - public suspend fun getSuperContestEffectList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getContestTypeList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/contest-type", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getContestEffectList(offset: Int, limit: Int): ApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/contest-effect", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getSuperContestEffectList(offset: Int, limit: Int): ApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/super-contest-effect", offset, limit) // endregion Contests // region Encounters - @GET("encounter-method/") - public suspend fun getEncounterMethodList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("encounter-condition/") - public suspend fun getEncounterConditionList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("encounter-condition-value/") - public suspend fun getEncounterConditionValueList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getEncounterMethodList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/encounter-method", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getEncounterConditionList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/encounter-condition", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getEncounterConditionValueList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/encounter-condition-value", offset, limit) // endregion // region Evolution - @GET("evolution-chain/") - public suspend fun getEvolutionChainList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getEvolutionChainList(offset: Int, limit: Int): ApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/evolution-chain", offset, limit) - @GET("evolution-trigger/") - public suspend fun getEvolutionTriggerList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getEvolutionTriggerList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/evolution-trigger", offset, limit) // endregion // region Games - @GET("generation/") - public suspend fun getGenerationList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("pokedex/") - public suspend fun getPokedexList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("version/") - public suspend fun getVersionList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("version-group/") - public suspend fun getVersionGroupList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getGenerationList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/generation", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokedexList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/pokedex", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getVersionList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/version", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getVersionGroupList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/version-group", offset, limit) // endregion // region Items - @GET("item/") - public suspend fun getItemList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("item-attribute/") - public suspend fun getItemAttributeList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("item-category/") - public suspend fun getItemCategoryList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("item-fling-effect/") - public suspend fun getItemFlingEffectList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("item-pocket/") - public suspend fun getItemPocketList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getItemList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/item", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getItemAttributeList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/item-attribute", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getItemCategoryList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/item-category", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getItemFlingEffectList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/item-fling-effect", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getItemPocketList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/item-pocket", offset, limit) // endregion // region Moves - @GET("move/") - public suspend fun getMoveList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("move-ailment/") - public suspend fun getMoveAilmentList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("move-battle-style/") - public suspend fun getMoveBattleStyleList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("move-category/") - public suspend fun getMoveCategoryList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("move-damage-class/") - public suspend fun getMoveDamageClassList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("move-learn-method/") - public suspend fun getMoveLearnMethodList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("move-target/") - public suspend fun getMoveTargetList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMoveList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/move", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMoveAilmentList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/move-ailment", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMoveBattleStyleList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/move-battle-style", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMoveCategoryList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/move-category", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMoveDamageClassList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/move-damage-class", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMoveLearnMethodList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/move-learn-method", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMoveTargetList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/move-target", offset, limit) // endregion // region Locations - @GET("location/") - public suspend fun getLocationList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("location-area/") - public suspend fun getLocationAreaList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("pal-park-area/") - public suspend fun getPalParkAreaList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("region/") - public suspend fun getRegionList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getLocationList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/location", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getLocationAreaList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/location-area", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPalParkAreaList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/pal-park-area", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getRegionList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/region", offset, limit) // endregion // region Machines - @GET("machine/") - public suspend fun getMachineList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMachineList(offset: Int, limit: Int): ApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/machine", offset, limit) // endregion // region Pokemon - @GET("ability/") - public suspend fun getAbilityList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("characteristic/") - public suspend fun getCharacteristicList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("egg-group/") - public suspend fun getEggGroupList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("gender/") - public suspend fun getGenderList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("growth-rate/") - public suspend fun getGrowthRateList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("nature/") - public suspend fun getNatureList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("pokeathlon-stat/") - public suspend fun getPokeathlonStatList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("pokemon/") - public suspend fun getPokemonList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("pokemon-color/") - public suspend fun getPokemonColorList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("pokemon-form/") - public suspend fun getPokemonFormList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("pokemon-habitat/") - public suspend fun getPokemonHabitatList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("pokemon-shape/") - public suspend fun getPokemonShapeList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("pokemon-species/") - public suspend fun getPokemonSpeciesList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("stat/") - public suspend fun getStatList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result - - @GET("type/") - public suspend fun getTypeList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getAbilityList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/ability", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getCharacteristicList(offset: Int, limit: Int): ApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/characteristic", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getEggGroupList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/egg-group", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getGenderList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/gender", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getGrowthRateList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/growth-rate", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getNatureList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/nature", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokeathlonStatList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/pokeathlon-stat", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokemonList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/pokemon", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokemonColorList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/pokemon-color", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokemonFormList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/pokemon-form", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokemonHabitatList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/pokemon-habitat", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokemonShapeList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/pokemon-shape", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokemonSpeciesList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/pokemon-species", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getStatList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/stat", offset, limit) + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getTypeList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/type", offset, limit) // endregion // region Utility - @GET("language/") - public suspend fun getLanguageList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getLanguageList(offset: Int, limit: Int): NamedApiResourceList = + client.getBodyWithOffsetAndLimit("/api/v2/language", offset, limit) // endregion @@ -369,179 +459,372 @@ public interface PokeApi { // region Berries - @GET("berry/{id}/") public suspend fun getBerry(@Path("id") id: Int): Result - - @GET("berry-firmness/{id}/") - public suspend fun getBerryFirmness(@Path("id") id: Int): Result - - @GET("berry-flavor/{id}/") - public suspend fun getBerryFlavor(@Path("id") id: Int): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getBerry(id: Int): Berry = client.getBody("${"/api/v2/berry"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getBerryFirmness(id: Int): BerryFirmness = + client.getBody("${"/api/v2/berry-firmness"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getBerryFlavor(id: Int): BerryFlavor = + client.getBody("${"/api/v2/berry-flavor"}/$id") // endregion Berries // region Contests - @GET("contest-type/{id}/") - public suspend fun getContestType(@Path("id") id: Int): Result - - @GET("contest-effect/{id}/") - public suspend fun getContestEffect(@Path("id") id: Int): Result - - @GET("super-contest-effect/{id}/") - public suspend fun getSuperContestEffect(@Path("id") id: Int): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getContestType(id: Int): ContestType = + client.getBody("${"/api/v2/contest-type"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getContestEffect(id: Int): ContestEffect = + client.getBody("${"/api/v2/contest-effect"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getSuperContestEffect(id: Int): SuperContestEffect = + client.getBody("${"/api/v2/super-contest-effect"}/$id") // endregion Contests // region Encounters - @GET("encounter-method/{id}/") - public suspend fun getEncounterMethod(@Path("id") id: Int): Result - - @GET("encounter-condition/{id}/") - public suspend fun getEncounterCondition(@Path("id") id: Int): Result - - @GET("encounter-condition-value/{id}/") - public suspend fun getEncounterConditionValue( - @Path("id") id: Int - ): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getEncounterMethod(id: Int): EncounterMethod = + client.getBody("${"/api/v2/encounter-method"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getEncounterCondition(id: Int): EncounterCondition = + client.getBody("${"/api/v2/encounter-condition"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getEncounterConditionValue(id: Int): EncounterConditionValue = + client.getBody("${"/api/v2/encounter-condition-value"}/$id") - // endregion Contests + // endregion // region Evolution - @GET("evolution-chain/{id}/") - public suspend fun getEvolutionChain(@Path("id") id: Int): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getEvolutionChain(id: Int): EvolutionChain = + client.getBody("${"/api/v2/evolution-chain"}/$id") - @GET("evolution-trigger/{id}/") - public suspend fun getEvolutionTrigger(@Path("id") id: Int): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getEvolutionTrigger(id: Int): EvolutionTrigger = + client.getBody("${"/api/v2/evolution-trigger"}/$id") // endregion Evolution // region Games - @GET("generation/{id}/") public suspend fun getGeneration(@Path("id") id: Int): Result - - @GET("pokedex/{id}/") public suspend fun getPokedex(@Path("id") id: Int): Result - - @GET("version/{id}/") public suspend fun getVersion(@Path("id") id: Int): Result - - @GET("version-group/{id}/") - public suspend fun getVersionGroup(@Path("id") id: Int): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getGeneration(id: Int): Generation = + client.getBody("${"/api/v2/generation"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokedex(id: Int): Pokedex = client.getBody("${"/api/v2/pokedex"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getVersion(id: Int): Version = client.getBody("${"/api/v2/version"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getVersionGroup(id: Int): VersionGroup = + client.getBody("${"/api/v2/version-group"}/$id") // endregion Games // region Items - @GET("item/{id}/") public suspend fun getItem(@Path("id") id: Int): Result - - @GET("item-attribute/{id}/") - public suspend fun getItemAttribute(@Path("id") id: Int): Result - - @GET("item-category/{id}/") - public suspend fun getItemCategory(@Path("id") id: Int): Result - - @GET("item-fling-effect/{id}/") - public suspend fun getItemFlingEffect(@Path("id") id: Int): Result - - @GET("item-pocket/{id}/") - public suspend fun getItemPocket(@Path("id") id: Int): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getItem(id: Int): Item = client.getBody("${"/api/v2/item"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getItemAttribute(id: Int): ItemAttribute = + client.getBody("${"/api/v2/item-attribute"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getItemCategory(id: Int): ItemCategory = + client.getBody("${"/api/v2/item-category"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getItemFlingEffect(id: Int): ItemFlingEffect = + client.getBody("${"/api/v2/item-fling-effect"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getItemPocket(id: Int): ItemPocket = + client.getBody("${"/api/v2/item-pocket"}/$id") // endregion Items // region Moves - @GET("move/{id}/") public suspend fun getMove(@Path("id") id: Int): Result - - @GET("move-ailment/{id}/") - public suspend fun getMoveAilment(@Path("id") id: Int): Result - - @GET("move-battle-style/{id}/") - public suspend fun getMoveBattleStyle(@Path("id") id: Int): Result - - @GET("move-category/{id}/") - public suspend fun getMoveCategory(@Path("id") id: Int): Result - - @GET("move-damage-class/{id}/") - public suspend fun getMoveDamageClass(@Path("id") id: Int): Result - - @GET("move-learn-method/{id}/") - public suspend fun getMoveLearnMethod(@Path("id") id: Int): Result - - @GET("move-target/{id}/") - public suspend fun getMoveTarget(@Path("id") id: Int): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMove(id: Int): Move = client.getBody("${"/api/v2/move"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMoveAilment(id: Int): MoveAilment = + client.getBody("${"/api/v2/move-ailment"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMoveBattleStyle(id: Int): MoveBattleStyle = + client.getBody("${"/api/v2/move-battle-style"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMoveCategory(id: Int): MoveCategory = + client.getBody("${"/api/v2/move-category"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMoveDamageClass(id: Int): MoveDamageClass = + client.getBody("${"/api/v2/move-damage-class"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMoveLearnMethod(id: Int): MoveLearnMethod = + client.getBody("${"/api/v2/move-learn-method"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMoveTarget(id: Int): MoveTarget = + client.getBody("${"/api/v2/move-target"}/$id") // endregion Moves // region Locations - @GET("location/{id}/") public suspend fun getLocation(@Path("id") id: Int): Result - - @GET("location-area/{id}/") - public suspend fun getLocationArea(@Path("id") id: Int): Result - - @GET("pal-park-area/{id}/") - public suspend fun getPalParkArea(@Path("id") id: Int): Result - - @GET("region/{id}/") public suspend fun getRegion(@Path("id") id: Int): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getLocation(id: Int): Location = client.getBody("${"/api/v2/location"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getLocationArea(id: Int): LocationArea = + client.getBody("${"/api/v2/location-area"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPalParkArea(id: Int): PalParkArea = + client.getBody("${"/api/v2/pal-park-area"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getRegion(id: Int): Region = client.getBody("${"/api/v2/region"}/$id") // endregion Locations // region Machines - @GET("machine/{id}/") public suspend fun getMachine(@Path("id") id: Int): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getMachine(id: Int): Machine = client.getBody("${"/api/v2/machine"}/$id") // endregion // region Pokemon - @GET("ability/{id}/") public suspend fun getAbility(@Path("id") id: Int): Result - - @GET("characteristic/{id}/") - public suspend fun getCharacteristic(@Path("id") id: Int): Result - - @GET("egg-group/{id}/") public suspend fun getEggGroup(@Path("id") id: Int): Result - - @GET("gender/{id}/") public suspend fun getGender(@Path("id") id: Int): Result - - @GET("growth-rate/{id}/") - public suspend fun getGrowthRate(@Path("id") id: Int): Result - - @GET("nature/{id}/") public suspend fun getNature(@Path("id") id: Int): Result - - @GET("pokeathlon-stat/{id}/") - public suspend fun getPokeathlonStat(@Path("id") id: Int): Result - - @GET("pokemon/{id}/") public suspend fun getPokemon(@Path("id") id: Int): Result - - @GET("pokemon/{id}/encounters/") - public suspend fun getPokemonEncounterList( - @Path("id") id: Int - ): Result> - - @GET("pokemon-color/{id}/") - public suspend fun getPokemonColor(@Path("id") id: Int): Result - - @GET("pokemon-form/{id}/") - public suspend fun getPokemonForm(@Path("id") id: Int): Result - - @GET("pokemon-habitat/{id}/") - public suspend fun getPokemonHabitat(@Path("id") id: Int): Result - - @GET("pokemon-shape/{id}/") - public suspend fun getPokemonShape(@Path("id") id: Int): Result - - @GET("pokemon-species/{id}/") - public suspend fun getPokemonSpecies(@Path("id") id: Int): Result - - @GET("stat/{id}/") public suspend fun getStat(@Path("id") id: Int): Result - - @GET("type/{id}/") public suspend fun getType(@Path("id") id: Int): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getAbility(id: Int): Ability = client.getBody("${"/api/v2/ability"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getCharacteristic(id: Int): Characteristic = + client.getBody("${"/api/v2/characteristic"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getEggGroup(id: Int): EggGroup = client.getBody("${"/api/v2/egg-group"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getGender(id: Int): Gender = client.getBody("${"/api/v2/gender"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getGrowthRate(id: Int): GrowthRate = + client.getBody("${"/api/v2/growth-rate"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getNature(id: Int): Nature = client.getBody("${"/api/v2/nature"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokeathlonStat(id: Int): PokeathlonStat = + client.getBody("${"/api/v2/pokeathlon-stat"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokemon(id: Int): Pokemon = client.getBody("${"/api/v2/pokemon"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokemonEncounterList(id: Int): List = + client.getBody("/api/v2/pokemon/$id/encounters") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokemonColor(id: Int): PokemonColor = + client.getBody("${"/api/v2/pokemon-color"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokemonForm(id: Int): PokemonForm = + client.getBody("${"/api/v2/pokemon-form"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokemonHabitat(id: Int): PokemonHabitat = + client.getBody("${"/api/v2/pokemon-habitat"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokemonShape(id: Int): PokemonShape = + client.getBody("${"/api/v2/pokemon-shape"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getPokemonSpecies(id: Int): PokemonSpecies = + client.getBody("${"/api/v2/pokemon-species"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getStat(id: Int): Stat = client.getBody("${"/api/v2/stat"}/$id") + + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getType(id: Int): Type = client.getBody("${"/api/v2/type"}/$id") // endregion Pokemon // region Utility - @GET("language/{id}/") public suspend fun getLanguage(@Path("id") id: Int): Result + @JvmBlocking + @JvmAsync + @JsPromise + @JsExport.Ignore + public suspend fun getLanguage(id: Int): Language = client.getBody("${"/api/v2/language"}/$id") // endregion Utility } diff --git a/src/commonMain/kotlin/co/pokeapi/pokekotlin/internal/Ktorfit.kt b/src/commonMain/kotlin/co/pokeapi/pokekotlin/internal/Ktorfit.kt deleted file mode 100644 index dd3788cb..00000000 --- a/src/commonMain/kotlin/co/pokeapi/pokekotlin/internal/Ktorfit.kt +++ /dev/null @@ -1,28 +0,0 @@ -package co.pokeapi.pokekotlin.internal - -import de.jensklingenberg.ktorfit.Ktorfit -import io.ktor.client.HttpClient -import io.ktor.client.HttpClientConfig -import io.ktor.client.engine.HttpClientEngine -import io.ktor.client.plugins.cache.HttpCache -import io.ktor.client.plugins.cache.storage.CacheStorage -import io.ktor.client.plugins.contentnegotiation.ContentNegotiation -import io.ktor.http.ContentType -import io.ktor.serialization.kotlinx.json.json - -internal fun createPokeApiKtorfitBuilder( - baseUrl: String, - cacheStorage: CacheStorage?, - engine: HttpClientEngine, - configure: HttpClientConfig<*>.() -> Unit, -) = - Ktorfit.Builder() - .baseUrl(baseUrl) - .httpClient( - HttpClient(engine) { - configure() - install(HttpCache) { cacheStorage?.let { privateStorage(it) } } - install(ContentNegotiation) { json(PokeApiJson, ContentType.Any) } - expectSuccess = true - } - ) diff --git a/src/commonMain/kotlin/co/pokeapi/pokekotlin/internal/ResultConverter.kt b/src/commonMain/kotlin/co/pokeapi/pokekotlin/internal/ResultConverter.kt deleted file mode 100644 index 0ba90df4..00000000 --- a/src/commonMain/kotlin/co/pokeapi/pokekotlin/internal/ResultConverter.kt +++ /dev/null @@ -1,33 +0,0 @@ -package co.pokeapi.pokekotlin.internal - -import de.jensklingenberg.ktorfit.Ktorfit -import de.jensklingenberg.ktorfit.converter.Converter -import de.jensklingenberg.ktorfit.converter.KtorfitResult -import de.jensklingenberg.ktorfit.converter.TypeData -import io.ktor.client.call.body -import io.ktor.client.statement.HttpResponse -import io.ktor.http.isSuccess - -internal class ResultConverter(private val typeData: TypeData) : - Converter.SuspendResponseConverter> { - - override suspend fun convert(result: KtorfitResult): Result { - return when (result) { - is KtorfitResult.Failure -> Result.failure(result.throwable) - is KtorfitResult.Success -> { - when { - result.response.status.isSuccess() -> - Result.success(result.response.body(typeData.typeArgs.first().typeInfo)) - // we configure the client with expectSuccess - else -> error("impossible: " + result.response) - } - } - } - } - - internal object Factory : Converter.Factory { - override fun suspendResponseConverter(typeData: TypeData, ktorfit: Ktorfit): ResultConverter? { - return if (typeData.typeInfo.type != Result::class) null else ResultConverter(typeData) - } - } -} diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/example/example.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/example/example.kt index 648f3956..c9d1093c 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/example/example.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/example/example.kt @@ -1,7 +1,6 @@ package co.pokeapi.pokekotlin.example import co.pokeapi.pokekotlin.PokeApi -import co.pokeapi.pokekotlin.createPokeApi suspend fun simple() { // -8<- [start:simple] @@ -12,7 +11,7 @@ suspend fun simple() { suspend fun custom() { // -8<- [start:custom] - val client = createPokeApi(baseUrl = "https://localhost:8080") + val client = PokeApi.Custom(baseUrl = "https://localhost:8080") val bulbasaur = client.getPokemon(1) println(bulbasaur) // -8<- [end:custom] diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/BulkTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/BulkTest.kt index 6a69580a..e2373f6b 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/BulkTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/BulkTest.kt @@ -2,13 +2,11 @@ package co.pokeapi.pokekotlin.test import co.pokeapi.pokekotlin.model.ResourceSummary import co.pokeapi.pokekotlin.model.ResourceSummaryList -import kotlin.test.Ignore import kotlin.test.Test import kotlin.test.fail import kotlinx.coroutines.test.runTest -// @IgnoreOnJvm // Should work on JVM but it's OOMing. -@Ignore +@IgnoreOnJvm // Should work on JVM but it's OOMing. class BulkTest { private suspend fun testCase(cat: String, id: Int, getObject: suspend (Int) -> Any) { @@ -21,11 +19,11 @@ class BulkTest { } } - private suspend fun testEach( - getList: suspend (Int, Int) -> Result>, - getObject: suspend (Int) -> Result, + private suspend fun testEach( + getList: suspend (Int, Int) -> ResourceSummaryList, + getObject: suspend (Int) -> O, ) { - val list = getList(0, getList(0, 0).getOrThrow().count).getOrThrow().results + val list = getList(0, getList(0, 0).count).results list.forEach { testCase(list[0].category, it.id, getObject) } } diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/FailureTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/FailureTest.kt index ef2ad244..cbc1b9a4 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/FailureTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/FailureTest.kt @@ -1,6 +1,6 @@ package co.pokeapi.pokekotlin.test -import co.pokeapi.pokekotlin.createPokeApi +import co.pokeapi.pokekotlin.PokeApi import io.ktor.client.plugins.* import io.ktor.http.HttpStatusCode.Companion.NotFound import kotlin.test.Test @@ -12,14 +12,15 @@ import kotlinx.coroutines.test.runTest class FailureTest { @Test fun notFoundError() = runTest { - val exception = LocalPokeApi.getMove(-1).exceptionOrNull() + val exception = runCatching { LocalPokeApi.getMove(-1) }.exceptionOrNull() assertIs(exception) assertEquals(NotFound, exception.response.status) } @Test fun badUrlError() = runTest { - val result = createPokeApi("https://localhost:12345/").getBerry(10) - assertNotNull(result.exceptionOrNull()) + val exception = + runCatching { PokeApi.Custom("https://localhost:12345/").getBerry(10) }.exceptionOrNull() + assertNotNull(exception) } } diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/LiveTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/LiveTest.kt index bb7a79a0..6e01ad93 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/LiveTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/LiveTest.kt @@ -1,7 +1,7 @@ package co.pokeapi.pokekotlin.test import co.pokeapi.pokekotlin.PokeApi -import io.ktor.client.plugins.ClientRequestException +import io.ktor.client.plugins.* import io.ktor.http.HttpStatusCode.Companion.NotFound import kotlin.test.Test import kotlin.test.assertEquals @@ -10,19 +10,16 @@ import kotlinx.coroutines.test.runTest class LiveTest { - @Test fun resource() = runTest { assertEquals("sitrus", PokeApi.getBerry(10).getOrThrow().name) } + @Test fun resource() = runTest { assertEquals("sitrus", PokeApi.getBerry(10).name) } @Test fun list() = runTest { - assertEquals( - PokeApi.getMoveList(0, 50).getOrThrow().results[25], - PokeApi.getMoveList(25, 50).getOrThrow().results[0], - ) + assertEquals(PokeApi.getMoveList(0, 50).results[25], PokeApi.getMoveList(25, 50).results[0]) } @Test fun notFound() = runTest { - val exception = PokeApi.getMove(-1).exceptionOrNull() + val exception = runCatching { PokeApi.getMove(-1) }.exceptionOrNull() assertIs(exception) assertEquals(NotFound, exception.response.status) } diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/LocalPokeApi.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/LocalPokeApi.kt index 733fd34e..5a4d4e0a 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/LocalPokeApi.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/LocalPokeApi.kt @@ -1,7 +1,6 @@ package co.pokeapi.pokekotlin.test import co.pokeapi.pokekotlin.PokeApi -import co.pokeapi.pokekotlin.createPokeApi import co.pokeapi.pokekotlin.internal.PokeApiJson import co.pokeapi.pokekotlin.model.ApiResourceList import co.pokeapi.pokekotlin.model.NamedApiResourceList @@ -42,7 +41,5 @@ private val OffsetLimitPlugin = } } -object LocalPokeApi : - PokeApi by (createPokeApi(baseUrl = "http://localhost:8080/api/v2/") { - install(OffsetLimitPlugin) - }) +val LocalPokeApi = + PokeApi.Custom(baseUrl = "http://localhost:8080/api/v2/") { install(OffsetLimitPlugin) } diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/BerryTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/BerryTest.kt index 3bff840f..cfadc0f8 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/BerryTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/BerryTest.kt @@ -14,7 +14,7 @@ class BerryTest { @Test fun getBerry() = runTest { - LocalPokeApi.getBerry(34).getOrThrow().apply { + LocalPokeApi.getBerry(34).apply { assertEquals(34, id) assertEquals("durin", name) assertEquals(15, growthTime) @@ -32,7 +32,7 @@ class BerryTest { @Test fun getBerryFirmness() = runTest { - LocalPokeApi.getBerryFirmness(3).getOrThrow().apply { + LocalPokeApi.getBerryFirmness(3).apply { assertEquals(3, id) assertEquals("hard", name) assertContains(berries, NamedApiResource("rawst", "berry", 4)) @@ -42,7 +42,7 @@ class BerryTest { @Test fun getBerryFlavor() = runTest { - LocalPokeApi.getBerryFlavor(3).getOrThrow().apply { + LocalPokeApi.getBerryFlavor(3).apply { assertEquals(3, id) assertEquals("sweet", name) assertEquals(NamedApiResource("cute", "contest-type", 3), contestType) diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/ContestTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/ContestTest.kt index 21033ca2..2a1db843 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/ContestTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/ContestTest.kt @@ -14,7 +14,7 @@ class ContestTest { @Test fun getContestType() = runTest { - LocalPokeApi.getContestType(4).getOrThrow().apply { + LocalPokeApi.getContestType(4).apply { assertEquals(4, id) assertEquals("smart", name) assertEquals(NamedApiResource("bitter", "berry-flavor", 4), berryFlavor) @@ -31,7 +31,7 @@ class ContestTest { @Test fun getContestEffect() = runTest { - LocalPokeApi.getContestEffect(27).getOrThrow().apply { + LocalPokeApi.getContestEffect(27).apply { assertEquals(27, id) assertEquals(2, appeal) assertEquals(0, jam) @@ -54,7 +54,7 @@ class ContestTest { @Test fun getSuperContestEffect() = runTest { - LocalPokeApi.getSuperContestEffect(14).getOrThrow().apply { + LocalPokeApi.getSuperContestEffect(14).apply { assertEquals(14, id) assertEquals(2, appeal) assertContains( diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/EncounterTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/EncounterTest.kt index f37fb0e4..eca5e773 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/EncounterTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/EncounterTest.kt @@ -12,7 +12,7 @@ class EncounterTest { @Test fun getEncounterMethod() = runTest { - LocalPokeApi.getEncounterMethod(5).getOrThrow().apply { + LocalPokeApi.getEncounterMethod(5).apply { assertEquals(5, id) assertEquals("surf", name) assertEquals(14, order) @@ -25,7 +25,7 @@ class EncounterTest { @Test fun getEncounterCondition() = runTest { - LocalPokeApi.getEncounterCondition(5).getOrThrow().apply { + LocalPokeApi.getEncounterCondition(5).apply { assertEquals(5, id) assertEquals("radio", name) assertContains(values, NamedApiResource("radio-hoenn", "encounter-condition-value", 15)) @@ -35,7 +35,7 @@ class EncounterTest { @Test fun getEncounterConditionValue() = runTest { - LocalPokeApi.getEncounterConditionValue(5).getOrThrow().apply { + LocalPokeApi.getEncounterConditionValue(5).apply { assertEquals(5, id) assertEquals("time-night", name) assertEquals(NamedApiResource("time", "encounter-condition", 2), condition) diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/EvolutionTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/EvolutionTest.kt index 9227f6c5..65b78e82 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/EvolutionTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/EvolutionTest.kt @@ -15,7 +15,7 @@ class EvolutionTest { @Test fun getEvolutionChain1() = runTest { - LocalPokeApi.getEvolutionChain(1).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(1).apply { assertEquals(1, id) assertEquals(null, babyTriggerItem) assertEquals( @@ -60,7 +60,7 @@ class EvolutionTest { @Test fun getEvolutionChain2() = runTest { - LocalPokeApi.getEvolutionChain(109).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(109).apply { assertNotNull( chain.evolvesTo.find { it.evolutionDetails.contains( @@ -77,7 +77,7 @@ class EvolutionTest { @Test fun getEvolutionChain3() = runTest { - LocalPokeApi.getEvolutionChain(67).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(67).apply { assertNotNull( chain.evolvesTo.find { it.evolutionDetails.contains( @@ -93,7 +93,7 @@ class EvolutionTest { @Test fun getEvolutionChain4() = runTest { - LocalPokeApi.getEvolutionChain(67).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(67).apply { assertNotNull( chain.evolvesTo.find { it.evolutionDetails.contains( @@ -109,7 +109,7 @@ class EvolutionTest { @Test fun getEvolutionChain5() = runTest { - LocalPokeApi.getEvolutionChain(67).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(67).apply { assertNotNull( chain.evolvesTo.find { it.evolutionDetails.contains( @@ -126,7 +126,7 @@ class EvolutionTest { @Test fun getEvolutionChain6() = runTest { - LocalPokeApi.getEvolutionChain(67).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(67).apply { assertNotNull( chain.evolvesTo.find { it.evolutionDetails.contains( @@ -143,7 +143,7 @@ class EvolutionTest { @Test fun getEvolutionChain7() = runTest { - LocalPokeApi.getEvolutionChain(112).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(112).apply { assertContains( chain.evolvesTo[0].evolvesTo[0].evolutionDetails, EvolutionDetail( @@ -156,7 +156,7 @@ class EvolutionTest { @Test fun getEvolutionChain8() = runTest { - LocalPokeApi.getEvolutionChain(213).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(213).apply { assertNotNull( chain.evolvesTo.find { it.evolutionDetails.contains( @@ -173,7 +173,7 @@ class EvolutionTest { @Test fun getEvolutionChain9() = runTest { - LocalPokeApi.getEvolutionChain(178).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(178).apply { assertNotNull( chain.evolvesTo.find { it.evolutionDetails.contains( @@ -189,7 +189,7 @@ class EvolutionTest { @Test fun getEvolutionChain10() = runTest { - LocalPokeApi.getEvolutionChain(346).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(346).apply { assertNotNull( chain.evolvesTo.find { it.evolutionDetails.contains( @@ -206,7 +206,7 @@ class EvolutionTest { @Test fun getEvolutionChain11() = runTest { - LocalPokeApi.getEvolutionChain(47).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(47).apply { assertNotNull( chain.evolvesTo.find { it.evolutionDetails.contains( @@ -223,7 +223,7 @@ class EvolutionTest { @Test fun getEvolutionChain12() = runTest { - LocalPokeApi.getEvolutionChain(362).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(362).apply { assertNotNull( chain.evolvesTo[0].evolvesTo.find { it.evolutionDetails.contains( @@ -240,7 +240,7 @@ class EvolutionTest { @Test fun getEvolutionChain13() = runTest { - LocalPokeApi.getEvolutionChain(352).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(352).apply { assertNotNull( chain.evolvesTo.find { it.evolutionDetails.contains( @@ -257,7 +257,7 @@ class EvolutionTest { @Test fun getEvolutionChain14() = runTest { - LocalPokeApi.getEvolutionChain(116).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(116).apply { assertNotNull( chain.evolvesTo.find { it.evolutionDetails.contains( @@ -273,7 +273,7 @@ class EvolutionTest { @Test fun getEvolutionChain15() = runTest { - LocalPokeApi.getEvolutionChain(312).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(312).apply { assertNotNull( chain.evolvesTo.find { it.evolutionDetails.contains( @@ -289,7 +289,7 @@ class EvolutionTest { @Test fun getEvolutionChain16() = runTest { - LocalPokeApi.getEvolutionChain(72).getOrThrow().apply { + LocalPokeApi.getEvolutionChain(72).apply { assertEquals(NamedApiResource("full-incense", "item", 293), babyTriggerItem) assertEquals(true, chain.isBaby) } @@ -297,7 +297,7 @@ class EvolutionTest { @Test fun getEvolutionTrigger() = runTest { - LocalPokeApi.getEvolutionTrigger(1).getOrThrow().apply { + LocalPokeApi.getEvolutionTrigger(1).apply { assertEquals(1, id) assertEquals("level-up", name) assertContains( diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/GameTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/GameTest.kt index 6b4ab839..36e3e452 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/GameTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/GameTest.kt @@ -14,7 +14,7 @@ class GameTest { @Test fun getGeneration() = runTest { - LocalPokeApi.getGeneration(6).getOrThrow().apply { + LocalPokeApi.getGeneration(6).apply { assertEquals(6, id) assertEquals("generation-vi", name) assertContains(abilities, NamedApiResource("primordial-sea", "ability", 189)) @@ -32,7 +32,7 @@ class GameTest { @Test fun getPokedex() = runTest { - LocalPokeApi.getPokedex(12).getOrThrow().apply { + LocalPokeApi.getPokedex(12).apply { assertEquals(12, id) assertEquals("kalos-central", name) assertEquals(true, isMainSeries) @@ -58,7 +58,7 @@ class GameTest { @Test fun getVersion() = runTest { - LocalPokeApi.getVersion(9).getOrThrow().apply { + LocalPokeApi.getVersion(9).apply { assertEquals(9, id) assertEquals("emerald", name) assertContains( @@ -71,7 +71,7 @@ class GameTest { @Test fun getVersionGroup() = runTest { - LocalPokeApi.getVersionGroup(1).getOrThrow().apply { + LocalPokeApi.getVersionGroup(1).apply { assertEquals(1, id) assertEquals("red-blue", name) assertEquals(3, order) diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/ItemTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/ItemTest.kt index 1ea82899..3efa7dbd 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/ItemTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/ItemTest.kt @@ -1,28 +1,15 @@ package co.pokeapi.pokekotlin.test.model -import co.pokeapi.pokekotlin.model.ApiResource -import co.pokeapi.pokekotlin.model.Description -import co.pokeapi.pokekotlin.model.Effect -import co.pokeapi.pokekotlin.model.GenerationGameIndex -import co.pokeapi.pokekotlin.model.ItemHolderPokemonVersionDetail -import co.pokeapi.pokekotlin.model.Name -import co.pokeapi.pokekotlin.model.NamedApiResource -import co.pokeapi.pokekotlin.model.VerboseEffect -import co.pokeapi.pokekotlin.model.VersionGroupFlavorText +import co.pokeapi.pokekotlin.model.* import co.pokeapi.pokekotlin.test.LocalPokeApi -import kotlin.test.Test -import kotlin.test.assertContains -import kotlin.test.assertEquals -import kotlin.test.assertNotEquals -import kotlin.test.assertNotNull -import kotlin.test.assertTrue +import kotlin.test.* import kotlinx.coroutines.test.runTest class ItemTest { @Test fun getItem1() = runTest { - LocalPokeApi.getItem(20).getOrThrow().apply { + LocalPokeApi.getItem(20).apply { assertEquals(20, id) assertEquals("ice-heal", name) assertEquals(200, cost) @@ -66,7 +53,7 @@ class ItemTest { @Test fun getItem2() = runTest { - LocalPokeApi.getItem(33).getOrThrow().apply { + LocalPokeApi.getItem(33).apply { assertNotEquals( null, heldByPokemon.find { @@ -84,14 +71,14 @@ class ItemTest { @Test fun getItem3() = runTest { - LocalPokeApi.getItem(249).getOrThrow().apply { + LocalPokeApi.getItem(249).apply { assertEquals(NamedApiResource("badly-poison", "item-fling-effect", 1), flingEffect) } } @Test fun getItem4() = runTest { - LocalPokeApi.getItem(231).getOrThrow().apply { + LocalPokeApi.getItem(231).apply { assertEquals(ApiResource("evolution-chain", 90), babyTriggerFor) } } @@ -100,7 +87,7 @@ class ItemTest { @Test fun getItem6() = runTest { - LocalPokeApi.getItem(305).getOrThrow().apply { + LocalPokeApi.getItem(305).apply { assertNotNull( machines.find { machineVersionDetail -> machineVersionDetail.machine == ApiResource("machine", 2) && @@ -112,7 +99,7 @@ class ItemTest { @Test fun getItemAttribute() = runTest { - LocalPokeApi.getItemAttribute(3).getOrThrow().apply { + LocalPokeApi.getItemAttribute(3).apply { assertEquals(3, id) assertEquals("usable-overworld", name) assertContains( @@ -132,7 +119,7 @@ class ItemTest { @Test fun getItemCategory() = runTest { - LocalPokeApi.getItemCategory(34).getOrThrow().apply { + LocalPokeApi.getItemCategory(34).apply { assertEquals(34, id) assertEquals("standard-balls", name) assertEquals(NamedApiResource("pokeballs", "item-pocket", 3), pocket) @@ -146,7 +133,7 @@ class ItemTest { @Test fun getItemFlingEffect() = runTest { - LocalPokeApi.getItemFlingEffect(1).getOrThrow().apply { + LocalPokeApi.getItemFlingEffect(1).apply { assertEquals(1, id) assertEquals("badly-poison", name) assertContains( @@ -162,7 +149,7 @@ class ItemTest { @Test fun getItemPocket() = runTest { - LocalPokeApi.getItemPocket(4).getOrThrow().apply { + LocalPokeApi.getItemPocket(4).apply { assertEquals(4, id) assertEquals("machines", name) assertContains(categories, NamedApiResource("all-machines", "item-category", 37)) diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/LocationTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/LocationTest.kt index 53edfab9..1bab9a67 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/LocationTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/LocationTest.kt @@ -1,10 +1,6 @@ package co.pokeapi.pokekotlin.test.model -import co.pokeapi.pokekotlin.model.EncounterMethodRateVersionDetail -import co.pokeapi.pokekotlin.model.GenerationGameIndex -import co.pokeapi.pokekotlin.model.Name -import co.pokeapi.pokekotlin.model.NamedApiResource -import co.pokeapi.pokekotlin.model.PalParkEncounterSpecies +import co.pokeapi.pokekotlin.model.* import co.pokeapi.pokekotlin.test.LocalPokeApi import kotlin.test.Test import kotlin.test.assertContains @@ -16,7 +12,7 @@ class LocationTest { @Test fun getLocation() = runTest { - LocalPokeApi.getLocation(20).getOrThrow().apply { + LocalPokeApi.getLocation(20).apply { assertEquals(20, id) assertEquals("wayward-cave", name) assertEquals(NamedApiResource("sinnoh", "region", 4), region) @@ -37,7 +33,7 @@ class LocationTest { @Test fun getLocationArea() = runTest { - LocalPokeApi.getLocationArea(20).getOrThrow().apply { + LocalPokeApi.getLocationArea(20).apply { assertEquals(20, id) assertEquals("mt-coronet-1f-from-exterior", name) assertEquals(20, gameIndex) @@ -78,7 +74,7 @@ class LocationTest { @Test fun getPalParkArea() = runTest { - LocalPokeApi.getPalParkArea(2).getOrThrow().apply { + LocalPokeApi.getPalParkArea(2).apply { assertEquals(2, id) assertEquals("field", name) assertContains(names, Name(name = "Field", language = NamedApiResource("en", "language", 9))) @@ -95,7 +91,7 @@ class LocationTest { @Test fun getRegion() = runTest { - LocalPokeApi.getRegion(1).getOrThrow().apply { + LocalPokeApi.getRegion(1).apply { assertEquals(1, id) assertEquals("kanto", name) assertEquals(NamedApiResource("generation-i", "generation", 1), mainGeneration) diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/MachineTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/MachineTest.kt index 9b2afb41..36d7d6d1 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/MachineTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/MachineTest.kt @@ -10,7 +10,7 @@ class MachineTest { @Test fun getMachine() = runTest { - LocalPokeApi.getMachine(18).getOrThrow().apply { + LocalPokeApi.getMachine(18).apply { assertEquals(18, id) assertEquals(NamedApiResource("tm01", "item", 305), item) assertEquals(NamedApiResource("work-up", "move", 526), move) diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/MoveTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/MoveTest.kt index cf01fab4..1ef99989 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/MoveTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/MoveTest.kt @@ -12,7 +12,7 @@ class MoveTest { @Test fun getMove1() = runTest { - LocalPokeApi.getMove(34).getOrThrow().apply { + LocalPokeApi.getMove(34).apply { assertEquals(34, id) assertEquals("body-slam", name) assertEquals(100, accuracy) @@ -80,7 +80,7 @@ class MoveTest { @Test fun getMove2() = runTest { - LocalPokeApi.getMove(400).getOrThrow().apply { + LocalPokeApi.getMove(400).apply { assertEquals( ContestComboSets( normalSet = ContestComboDetail(null, null), @@ -93,7 +93,7 @@ class MoveTest { @Test fun getMove3() = runTest { - LocalPokeApi.getMove(16).getOrThrow().apply { + LocalPokeApi.getMove(16).apply { assertNotNull( effectChanges.find { it.versionGroup == NamedApiResource("gold-silver", "version-group", 3) && @@ -108,7 +108,7 @@ class MoveTest { @Test fun getMove4() = runTest { - LocalPokeApi.getMove(14).getOrThrow().apply { + LocalPokeApi.getMove(14).apply { assertContains( statChanges, MoveStatChange(change = 2, stat = NamedApiResource("attack", "stat", 2)), @@ -118,7 +118,7 @@ class MoveTest { @Test fun getMove5() = runTest { - LocalPokeApi.getMove(2).getOrThrow().apply { + LocalPokeApi.getMove(2).apply { assertContains( pastValues, PastMoveStatValues( @@ -136,7 +136,7 @@ class MoveTest { @Test fun getMoveAilment() = runTest { - LocalPokeApi.getMoveAilment(1).getOrThrow().apply { + LocalPokeApi.getMoveAilment(1).apply { assertEquals(1, id) assertEquals("paralysis", name) assertContains( @@ -149,7 +149,7 @@ class MoveTest { @Test fun getMoveBattleStyle() = runTest { - LocalPokeApi.getMoveBattleStyle(1).getOrThrow().apply { + LocalPokeApi.getMoveBattleStyle(1).apply { assertEquals(1, id) assertEquals("attack", name) assertContains(names, Name(name = "Attack", language = NamedApiResource("en", "language", 9))) @@ -158,7 +158,7 @@ class MoveTest { @Test fun getMoveCategory() = runTest { - LocalPokeApi.getMoveCategory(1).getOrThrow().apply { + LocalPokeApi.getMoveCategory(1).apply { assertEquals(1, id) assertEquals("ailment", name) assertContains( @@ -174,7 +174,7 @@ class MoveTest { @Test fun getMoveDamageClass() = runTest { - LocalPokeApi.getMoveDamageClass(1).getOrThrow().apply { + LocalPokeApi.getMoveDamageClass(1).apply { assertEquals(1, id) assertEquals("status", name) assertContains(names, Name(name = "status", language = NamedApiResource("en", "language", 9))) @@ -188,7 +188,7 @@ class MoveTest { @Test fun getMoveLearnMethod() = runTest { - LocalPokeApi.getMoveLearnMethod(10).getOrThrow().apply { + LocalPokeApi.getMoveLearnMethod(10).apply { assertEquals(10, id) assertEquals("form-change", name) assertContains( @@ -211,7 +211,7 @@ class MoveTest { @Test fun getMoveTarget() = runTest { - LocalPokeApi.getMoveTarget(8).getOrThrow().apply { + LocalPokeApi.getMoveTarget(8).apply { assertEquals(8, id) assertEquals("random-opponent", name) assertContains( diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/PokemonTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/PokemonTest.kt index f83328ee..2094d0dc 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/PokemonTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/PokemonTest.kt @@ -1,45 +1,15 @@ package co.pokeapi.pokekotlin.test.model -import co.pokeapi.pokekotlin.model.AbilityPokemon -import co.pokeapi.pokekotlin.model.ApiResource -import co.pokeapi.pokekotlin.model.AwesomeName -import co.pokeapi.pokekotlin.model.Description -import co.pokeapi.pokekotlin.model.Effect -import co.pokeapi.pokekotlin.model.GenerationGameIndex -import co.pokeapi.pokekotlin.model.Genus -import co.pokeapi.pokekotlin.model.GrowthRateExperienceLevel -import co.pokeapi.pokekotlin.model.MoveBattleStylePreference -import co.pokeapi.pokekotlin.model.MoveStatAffect -import co.pokeapi.pokekotlin.model.Name -import co.pokeapi.pokekotlin.model.NamedApiResource -import co.pokeapi.pokekotlin.model.NaturePokeathlonStatAffect -import co.pokeapi.pokekotlin.model.NatureStatChange -import co.pokeapi.pokekotlin.model.PalParkEncounterArea -import co.pokeapi.pokekotlin.model.PokemonAbility -import co.pokeapi.pokekotlin.model.PokemonHeldItemVersion -import co.pokeapi.pokekotlin.model.PokemonMoveVersion -import co.pokeapi.pokekotlin.model.PokemonSpeciesDexEntry -import co.pokeapi.pokekotlin.model.PokemonSpeciesFlavorText -import co.pokeapi.pokekotlin.model.PokemonSpeciesGender -import co.pokeapi.pokekotlin.model.PokemonSpeciesVariety -import co.pokeapi.pokekotlin.model.PokemonStat -import co.pokeapi.pokekotlin.model.PokemonType -import co.pokeapi.pokekotlin.model.TypePokemon -import co.pokeapi.pokekotlin.model.VerboseEffect -import co.pokeapi.pokekotlin.model.VersionGameIndex +import co.pokeapi.pokekotlin.model.* import co.pokeapi.pokekotlin.test.LocalPokeApi -import kotlin.test.Test -import kotlin.test.assertContains -import kotlin.test.assertEquals -import kotlin.test.assertNotNull -import kotlin.test.assertTrue +import kotlin.test.* import kotlinx.coroutines.test.runTest class PokemonTest { @Test fun getAbility() = runTest { - LocalPokeApi.getAbility(1).getOrThrow().apply { + LocalPokeApi.getAbility(1).apply { assertEquals(1, id) assertEquals("stench", name) assertEquals(true, isMainSeries) @@ -82,7 +52,7 @@ class PokemonTest { @Test fun getCharacteristic() = runTest { - LocalPokeApi.getCharacteristic(1).getOrThrow().apply { + LocalPokeApi.getCharacteristic(1).apply { assertEquals(1, id) assertEquals(0, geneModulo) assertEquals((0..6).map { it * 5 }.toList(), possibleValues) @@ -95,7 +65,7 @@ class PokemonTest { @Test fun getEggGroup() = runTest { - LocalPokeApi.getEggGroup(1).getOrThrow().apply { + LocalPokeApi.getEggGroup(1).apply { assertEquals(1, id) assertEquals("monster", name) assertContains( @@ -108,7 +78,7 @@ class PokemonTest { @Test fun getGender() = runTest { - LocalPokeApi.getGender(1).getOrThrow().apply { + LocalPokeApi.getGender(1).apply { assertEquals(1, id) assertEquals("female", name) assertContains( @@ -124,7 +94,7 @@ class PokemonTest { @Test fun getGrowthRate() = runTest { - LocalPokeApi.getGrowthRate(1).getOrThrow().apply { + LocalPokeApi.getGrowthRate(1).apply { assertEquals(1, id) assertEquals("slow", name) assertEquals("\\frac{5x^3}{4}", formula) @@ -139,7 +109,7 @@ class PokemonTest { @Test fun getNature() = runTest { - LocalPokeApi.getNature(10).getOrThrow().apply { + LocalPokeApi.getNature(10).apply { assertEquals(10, id) assertEquals("hasty", name) assertEquals(NamedApiResource("speed", "stat", 6), increasedStat) @@ -167,7 +137,7 @@ class PokemonTest { @Test fun getPokeathlonStat() = runTest { - LocalPokeApi.getPokeathlonStat(1).getOrThrow().apply { + LocalPokeApi.getPokeathlonStat(1).apply { assertEquals(1, id) assertEquals("speed", name) assertContains(names, Name(name = "Speed", language = NamedApiResource("en", "language", 9))) @@ -187,7 +157,7 @@ class PokemonTest { @Test fun getPokemon1() = runTest { - LocalPokeApi.getPokemon(1).getOrThrow().apply { + LocalPokeApi.getPokemon(1).apply { assertEquals(1, id) assertEquals("bulbasaur", name) assertEquals(64, baseExperience) @@ -230,7 +200,7 @@ class PokemonTest { @Test fun getPokemon2() = runTest { - LocalPokeApi.getPokemon(12).getOrThrow().apply { + LocalPokeApi.getPokemon(12).apply { assertNotNull( heldItems.find { it.item == NamedApiResource("silver-powder", "item", 199) && @@ -243,7 +213,7 @@ class PokemonTest { @Test fun getPokemon3() = runTest { - LocalPokeApi.getPokemonEncounterList(12).getOrThrow().apply { + LocalPokeApi.getPokemonEncounterList(12).apply { assertNotNull( find { locAreaEncounter -> locAreaEncounter.locationArea == @@ -267,7 +237,7 @@ class PokemonTest { @Test fun getPokemon4() = runTest { - LocalPokeApi.getPokemon(399).getOrThrow().apply { + LocalPokeApi.getPokemon(399).apply { sprites.apply { assertTrue(backFemale!!.endsWith("/sprites/pokemon/back/female/399.png")) assertTrue(backShinyFemale!!.endsWith("/sprites/pokemon/back/shiny/female/399.png")) @@ -283,7 +253,7 @@ class PokemonTest { @Test fun getPokemonColor() = runTest { - LocalPokeApi.getPokemonColor(1).getOrThrow().apply { + LocalPokeApi.getPokemonColor(1).apply { assertEquals(1, id) assertEquals("black", name) assertContains(names, Name(name = "Black", language = NamedApiResource("en", "language", 9))) @@ -293,7 +263,7 @@ class PokemonTest { @Test fun getPokemonForm() = runTest { - LocalPokeApi.getPokemonForm(1).getOrThrow().apply { + LocalPokeApi.getPokemonForm(1).apply { assertEquals(1, id) assertEquals("bulbasaur", name) assertEquals(1, order) @@ -315,7 +285,7 @@ class PokemonTest { @Test fun getPokemonForm2() = runTest { - LocalPokeApi.getPokemonForm(10266).getOrThrow().apply { + LocalPokeApi.getPokemonForm(10266).apply { assertContains( formNames, Name(name = "Original Color", language = NamedApiResource("en", "language", 9)), @@ -325,7 +295,7 @@ class PokemonTest { @Test fun getPokemonHabitat() = runTest { - LocalPokeApi.getPokemonHabitat(1).getOrThrow().apply { + LocalPokeApi.getPokemonHabitat(1).apply { assertEquals(1, id) assertEquals("cave", name) assertContains(names, Name(name = "cave", language = NamedApiResource("en", "language", 9))) @@ -335,7 +305,7 @@ class PokemonTest { @Test fun getPokemonShape() = runTest { - LocalPokeApi.getPokemonShape(1).getOrThrow().apply { + LocalPokeApi.getPokemonShape(1).apply { assertEquals(1, id) assertEquals("ball", name) assertContains(names, Name(name = "Ball", language = NamedApiResource("en", "language", 9))) @@ -349,7 +319,7 @@ class PokemonTest { @Test fun getPokemonSpecies1() = runTest { - LocalPokeApi.getPokemonSpecies(1).getOrThrow().apply { + LocalPokeApi.getPokemonSpecies(1).apply { assertEquals(1, id) assertEquals("bulbasaur", name) assertEquals(1, order) @@ -417,14 +387,14 @@ class PokemonTest { @Test fun getPokemonSpecies2() = runTest { - LocalPokeApi.getPokemonSpecies(2).getOrThrow().apply { + LocalPokeApi.getPokemonSpecies(2).apply { assertEquals(NamedApiResource("bulbasaur", "pokemon-species", 1), evolvesFromSpecies) } } @Test fun getPokemonSpecies3() = runTest { - LocalPokeApi.getPokemonSpecies(351).getOrThrow().apply { + LocalPokeApi.getPokemonSpecies(351).apply { assertContains( formDescriptions, Description( @@ -440,7 +410,7 @@ class PokemonTest { @Test fun getStat() = runTest { - LocalPokeApi.getStat(2).getOrThrow().apply { + LocalPokeApi.getStat(2).apply { assertEquals(2, id) assertEquals("attack", name) assertEquals(2, gameIndex) @@ -462,7 +432,7 @@ class PokemonTest { @Test fun getType() = runTest { - LocalPokeApi.getType(8).getOrThrow().apply { + LocalPokeApi.getType(8).apply { assertEquals(8, id) assertEquals("ghost", name) damageRelations.apply { diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/ResourceListTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/ResourceListTest.kt index 086fb8a1..af2c6706 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/ResourceListTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/ResourceListTest.kt @@ -5,11 +5,7 @@ import co.pokeapi.pokekotlin.model.ApiResourceList import co.pokeapi.pokekotlin.model.NamedApiResource import co.pokeapi.pokekotlin.model.NamedApiResourceList import co.pokeapi.pokekotlin.test.LocalPokeApi -import kotlin.test.Test -import kotlin.test.assertContains -import kotlin.test.assertEquals -import kotlin.test.assertNotEquals -import kotlin.test.assertTrue +import kotlin.test.* import kotlinx.coroutines.test.runTest class ResourceListTest { @@ -20,9 +16,9 @@ class ResourceListTest { category: String, id: Int, name: String, - call: suspend () -> Result, + call: suspend () -> NamedApiResourceList, ) { - call().getOrThrow().apply { + call().apply { assertTrue(results.size <= pageSize, "Actual count: ${results.size}, pageSize: $pageSize") if (pageSize >= count) { assertEquals(count, results.size) @@ -42,12 +38,8 @@ class ResourceListTest { } } - private suspend fun testCase( - category: String, - id: Int, - call: suspend () -> Result, - ) { - call().getOrThrow().apply { + private suspend fun testCase(category: String, id: Int, call: suspend () -> ApiResourceList) { + call().apply { assertTrue(results.size <= pageSize) if (pageSize >= count) { assertEquals(count, results.size) diff --git a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/UtilityTest.kt b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/UtilityTest.kt index 3a81428e..fa899642 100644 --- a/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/UtilityTest.kt +++ b/src/commonTest/kotlin/co/pokeapi/pokekotlin/test/model/UtilityTest.kt @@ -12,7 +12,7 @@ class UtilityTest { @Test fun getLanguage() = runTest { - LocalPokeApi.getLanguage(9).getOrThrow().apply { + LocalPokeApi.getLanguage(9).apply { assertEquals(9, id) assertEquals("us", iso3166) assertEquals("en", iso639) diff --git a/src/jsMain/kotlin/co/pokeapi/pokekotlin/internal/PromiseConverter.kt b/src/jsMain/kotlin/co/pokeapi/pokekotlin/internal/PromiseConverter.kt deleted file mode 100644 index 77f01643..00000000 --- a/src/jsMain/kotlin/co/pokeapi/pokekotlin/internal/PromiseConverter.kt +++ /dev/null @@ -1,36 +0,0 @@ -package co.pokeapi.pokekotlin.internal - -import de.jensklingenberg.ktorfit.Ktorfit -import de.jensklingenberg.ktorfit.converter.Converter -import de.jensklingenberg.ktorfit.converter.KtorfitResult -import de.jensklingenberg.ktorfit.converter.TypeData -import io.ktor.client.statement.* -import kotlin.js.Promise -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.promise - -internal class PromiseConverter(typeData: TypeData, private val scope: CoroutineScope) : - Converter.ResponseConverter> { - - private val resultConverter = ResultConverter(typeData) - - @OptIn(DelicateCoroutinesApi::class) - override fun convert(getResponse: suspend () -> HttpResponse): Promise<*> { - return scope.promise { - val result = - try { - KtorfitResult.Success(getResponse()) - } catch (throwable: Throwable) { - KtorfitResult.Failure(throwable) - } - resultConverter.convert(result).getOrThrow() - } - } - - internal class Factory(private val scope: CoroutineScope) : Converter.Factory { - override fun responseConverter(typeData: TypeData, ktorfit: Ktorfit): PromiseConverter? { - return PromiseConverter(typeData, scope) - } - } -} diff --git a/src/jsMain/kotlin/co/pokeapi/pokekotlin/js/PokeApi.kt b/src/jsMain/kotlin/co/pokeapi/pokekotlin/js/PokeApi.kt deleted file mode 100644 index 263e52ba..00000000 --- a/src/jsMain/kotlin/co/pokeapi/pokekotlin/js/PokeApi.kt +++ /dev/null @@ -1,528 +0,0 @@ -package co.pokeapi.pokekotlin.js - -import co.pokeapi.pokekotlin.internal.JsOnlyExport -import co.pokeapi.pokekotlin.internal.PromiseConverter -import co.pokeapi.pokekotlin.internal.createPokeApiKtorfitBuilder -import co.pokeapi.pokekotlin.internal.getDefaultEngine -import co.pokeapi.pokekotlin.model.* -import de.jensklingenberg.ktorfit.http.GET -import de.jensklingenberg.ktorfit.http.Path -import de.jensklingenberg.ktorfit.http.Query -import kotlin.js.Promise -import kotlinx.coroutines.GlobalScope - -@JsOnlyExport -@JsName("createPokeApi") -public fun createPokeApiJs(baseUrl: String = "https://pokeapi.co/api/v2/"): PokeApiJs { - - return createPokeApiKtorfitBuilder( - baseUrl = baseUrl, - cacheStorage = null, - engine = getDefaultEngine(), - configure = {}, - ) - .converterFactories(PromiseConverter.Factory(@Suppress("OPT_IN_USAGE") GlobalScope)) - .build() - .createPokeApiJs() -} - -@JsOnlyExport -@JsName("PokeApi") -public interface PokeApiJs { - - // region Resource Lists - - // region Berries - - @GET("berry/") - public fun getBerryList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("berry-firmness/") - public fun getBerryFirmnessList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("berry-flavor/") - public fun getBerryFlavorList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - // endregion Berries - - // region Contests - - @GET("contest-type/") - public fun getContestTypeList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("contest-effect/") - public fun getContestEffectList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("super-contest-effect/") - public fun getSuperContestEffectList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - // endregion Contests - - // region Encounters - - @GET("encounter-method/") - public fun getEncounterMethodList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("encounter-condition/") - public fun getEncounterConditionList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("encounter-condition-value/") - public fun getEncounterConditionValueList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - // endregion - - // region Evolution - - @GET("evolution-chain/") - public fun getEvolutionChainList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("evolution-trigger/") - public fun getEvolutionTriggerList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - // endregion - - // region Games - - @GET("generation/") - public fun getGenerationList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("pokedex/") - public fun getPokedexList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("version/") - public fun getVersionList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("version-group/") - public fun getVersionGroupList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - // endregion - - // region Items - - @GET("item/") - public fun getItemList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("item-attribute/") - public fun getItemAttributeList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("item-category/") - public fun getItemCategoryList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("item-fling-effect/") - public fun getItemFlingEffectList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("item-pocket/") - public fun getItemPocketList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - // endregion - - // region Moves - - @GET("move/") - public fun getMoveList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("move-ailment/") - public fun getMoveAilmentList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("move-battle-style/") - public fun getMoveBattleStyleList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("move-category/") - public fun getMoveCategoryList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("move-damage-class/") - public fun getMoveDamageClassList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("move-learn-method/") - public fun getMoveLearnMethodList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("move-target/") - public fun getMoveTargetList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - // endregion - - // region Locations - - @GET("location/") - public fun getLocationList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("location-area/") - public fun getLocationAreaList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("pal-park-area/") - public fun getPalParkAreaList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("region/") - public fun getRegionList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - // endregion - - // region Machines - - @GET("machine/") - public fun getMachineList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - // endregion - - // region Pokemon - - @GET("ability/") - public fun getAbilityList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("characteristic/") - public fun getCharacteristicList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("egg-group/") - public fun getEggGroupList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("gender/") - public fun getGenderList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("growth-rate/") - public fun getGrowthRateList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("nature/") - public fun getNatureList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("pokeathlon-stat/") - public fun getPokeathlonStatList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("pokemon/") - public fun getPokemonList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("pokemon-color/") - public fun getPokemonColorList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("pokemon-form/") - public fun getPokemonFormList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("pokemon-habitat/") - public fun getPokemonHabitatList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("pokemon-shape/") - public fun getPokemonShapeList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("pokemon-species/") - public fun getPokemonSpeciesList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("stat/") - public fun getStatList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - @GET("type/") - public fun getTypeList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - // endregion - - // region Utility - - @GET("language/") - public fun getLanguageList( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): Promise - - // endregion - - // endregion - - // region Berries - - @GET("berry/{id}/") public fun getBerry(@Path("id") id: Int): Promise - - @GET("berry-firmness/{id}/") - public fun getBerryFirmness(@Path("id") id: Int): Promise - - @GET("berry-flavor/{id}/") public fun getBerryFlavor(@Path("id") id: Int): Promise - - // endregion Berries - - // region Contests - - @GET("contest-type/{id}/") public fun getContestType(@Path("id") id: Int): Promise - - @GET("contest-effect/{id}/") - public fun getContestEffect(@Path("id") id: Int): Promise - - @GET("super-contest-effect/{id}/") - public fun getSuperContestEffect(@Path("id") id: Int): Promise - - // endregion Contests - - // region Encounters - - @GET("encounter-method/{id}/") - public fun getEncounterMethod(@Path("id") id: Int): Promise - - @GET("encounter-condition/{id}/") - public fun getEncounterCondition(@Path("id") id: Int): Promise - - @GET("encounter-condition-value/{id}/") - public fun getEncounterConditionValue(@Path("id") id: Int): Promise - - // endregion Contests - - // region Evolution - - @GET("evolution-chain/{id}/") - public fun getEvolutionChain(@Path("id") id: Int): Promise - - @GET("evolution-trigger/{id}/") - public fun getEvolutionTrigger(@Path("id") id: Int): Promise - - // endregion Evolution - - // region Games - - @GET("generation/{id}/") public fun getGeneration(@Path("id") id: Int): Promise - - @GET("pokedex/{id}/") public fun getPokedex(@Path("id") id: Int): Promise - - @GET("version/{id}/") public fun getVersion(@Path("id") id: Int): Promise - - @GET("version-group/{id}/") public fun getVersionGroup(@Path("id") id: Int): Promise - - // endregion Games - - // region Items - - @GET("item/{id}/") public fun getItem(@Path("id") id: Int): Promise - - @GET("item-attribute/{id}/") - public fun getItemAttribute(@Path("id") id: Int): Promise - - @GET("item-category/{id}/") public fun getItemCategory(@Path("id") id: Int): Promise - - @GET("item-fling-effect/{id}/") - public fun getItemFlingEffect(@Path("id") id: Int): Promise - - @GET("item-pocket/{id}/") public fun getItemPocket(@Path("id") id: Int): Promise - - // endregion Items - - // region Moves - - @GET("move/{id}/") public fun getMove(@Path("id") id: Int): Promise - - @GET("move-ailment/{id}/") public fun getMoveAilment(@Path("id") id: Int): Promise - - @GET("move-battle-style/{id}/") - public fun getMoveBattleStyle(@Path("id") id: Int): Promise - - @GET("move-category/{id}/") public fun getMoveCategory(@Path("id") id: Int): Promise - - @GET("move-damage-class/{id}/") - public fun getMoveDamageClass(@Path("id") id: Int): Promise - - @GET("move-learn-method/{id}/") - public fun getMoveLearnMethod(@Path("id") id: Int): Promise - - @GET("move-target/{id}/") public fun getMoveTarget(@Path("id") id: Int): Promise - - // endregion Moves - - // region Locations - - @GET("location/{id}/") public fun getLocation(@Path("id") id: Int): Promise - - @GET("location-area/{id}/") public fun getLocationArea(@Path("id") id: Int): Promise - - @GET("pal-park-area/{id}/") public fun getPalParkArea(@Path("id") id: Int): Promise - - @GET("region/{id}/") public fun getRegion(@Path("id") id: Int): Promise - - // endregion Locations - - // region Machines - - @GET("machine/{id}/") public fun getMachine(@Path("id") id: Int): Promise - - // endregion - - // region Pokemon - - @GET("ability/{id}/") public fun getAbility(@Path("id") id: Int): Promise - - @GET("characteristic/{id}/") - public fun getCharacteristic(@Path("id") id: Int): Promise - - @GET("egg-group/{id}/") public fun getEggGroup(@Path("id") id: Int): Promise - - @GET("gender/{id}/") public fun getGender(@Path("id") id: Int): Promise - - @GET("growth-rate/{id}/") public fun getGrowthRate(@Path("id") id: Int): Promise - - @GET("nature/{id}/") public fun getNature(@Path("id") id: Int): Promise - - @GET("pokeathlon-stat/{id}/") - public fun getPokeathlonStat(@Path("id") id: Int): Promise - - @GET("pokemon/{id}/") public fun getPokemon(@Path("id") id: Int): Promise - - @GET("pokemon/{id}/encounters/") - public fun getPokemonEncounterList(@Path("id") id: Int): Promise> - - @GET("pokemon-color/{id}/") public fun getPokemonColor(@Path("id") id: Int): Promise - - @GET("pokemon-form/{id}/") public fun getPokemonForm(@Path("id") id: Int): Promise - - @GET("pokemon-habitat/{id}/") - public fun getPokemonHabitat(@Path("id") id: Int): Promise - - @GET("pokemon-shape/{id}/") public fun getPokemonShape(@Path("id") id: Int): Promise - - @GET("pokemon-species/{id}/") - public fun getPokemonSpecies(@Path("id") id: Int): Promise - - @GET("stat/{id}/") public fun getStat(@Path("id") id: Int): Promise - - @GET("type/{id}/") public fun getType(@Path("id") id: Int): Promise - - // endregion Pokemon - - // region Utility - - @GET("language/{id}/") public fun getLanguage(@Path("id") id: Int): Promise - - // endregion Utility -} diff --git a/src/jsTest/kotlin/co/pokeapi/pokekotlin/test/LiveJsTest.kt b/src/jsTest/kotlin/co/pokeapi/pokekotlin/test/LiveJsTest.kt index f393bd51..5eab4e3d 100644 --- a/src/jsTest/kotlin/co/pokeapi/pokekotlin/test/LiveJsTest.kt +++ b/src/jsTest/kotlin/co/pokeapi/pokekotlin/test/LiveJsTest.kt @@ -1,6 +1,6 @@ package co.pokeapi.pokekotlin.test -import co.pokeapi.pokekotlin.js.PokeApiJs +import co.pokeapi.pokekotlin.PokeApi import io.ktor.client.plugins.* import io.ktor.http.* import kotlin.js.Promise @@ -12,15 +12,15 @@ import kotlinx.coroutines.test.runTest @Suppress("UnusedVariable", "unused") class LiveJsTest { - private val client: PokeApiJs = - js("require('./pokekotlin.js').co.pokeapi.pokekotlin.js.createPokeApi()") + private val client: PokeApi = + js("require('./pokekotlin.js').co.pokeapi.pokekotlin.PokeApi.Default") @Test fun resource() = runTest { val client = client assertEquals( "sitrus", - js("client.getBerry(10).then(function (it) { return it.name; })") + js("client.getBerryAsync(10).then(function (it) { return it.name; })") .unsafeCast>() .await(), ) @@ -30,17 +30,17 @@ class LiveJsTest { fun list() = runTest { val client = client - val move1 = - js("client.getMoveList(0, 50).then(function (it) { return it.results[25]; })") + val berry1 = + js("client.getBerryListAsync(0, 10).then(function (it) { return it.results[5]; })") .unsafeCast>() .await() - val move2 = - js("client.getMoveList(25, 50).then(function (it) { return it.results[0]; })") + val berry2 = + js("client.getBerryListAsync(5, 15).then(function (it) { return it.results[0]; })") .unsafeCast>() .await() - assertEquals(move1, move2) + assertEquals(berry1, berry2) } @Test @@ -49,7 +49,7 @@ class LiveJsTest { val exception = assertFailsWith { - js("client.getMove(-1)").unsafeCast>().await() + js("client.getMoveAsync(-1)").unsafeCast>().await() } assertEquals(HttpStatusCode.NotFound, exception.response.status) diff --git a/src/jvmMain/kotlin/co/pokeapi/pokekotlin/internal/CompletableFutureConverter.kt b/src/jvmMain/kotlin/co/pokeapi/pokekotlin/internal/CompletableFutureConverter.kt deleted file mode 100644 index 123f084e..00000000 --- a/src/jvmMain/kotlin/co/pokeapi/pokekotlin/internal/CompletableFutureConverter.kt +++ /dev/null @@ -1,40 +0,0 @@ -package co.pokeapi.pokekotlin.internal - -import de.jensklingenberg.ktorfit.Ktorfit -import de.jensklingenberg.ktorfit.converter.Converter -import de.jensklingenberg.ktorfit.converter.KtorfitResult -import de.jensklingenberg.ktorfit.converter.TypeData -import io.ktor.client.statement.* -import java.util.concurrent.CompletableFuture -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.future.future - -internal class CompletableFutureConverter(typeData: TypeData, private val scope: CoroutineScope) : - Converter.ResponseConverter> { - - private val resultConverter = ResultConverter(typeData) - - @OptIn(DelicateCoroutinesApi::class) - override fun convert(getResponse: suspend () -> HttpResponse): CompletableFuture<*> { - return scope.future { - val result = - try { - KtorfitResult.Success(getResponse()) - } catch (throwable: Throwable) { - KtorfitResult.Failure(throwable) - } - resultConverter.convert(result).getOrThrow() - } - } - - internal class Factory(private val scope: CoroutineScope) : Converter.Factory { - override fun responseConverter( - typeData: TypeData, - ktorfit: Ktorfit, - ): CompletableFutureConverter? { - return if (typeData.typeInfo.type != CompletableFuture::class) null - else CompletableFutureConverter(typeData, scope) - } - } -} diff --git a/src/jvmMain/kotlin/co/pokeapi/pokekotlin/java/PokeApi.kt b/src/jvmMain/kotlin/co/pokeapi/pokekotlin/java/PokeApi.kt deleted file mode 100644 index 0ea95c4a..00000000 --- a/src/jvmMain/kotlin/co/pokeapi/pokekotlin/java/PokeApi.kt +++ /dev/null @@ -1,562 +0,0 @@ -package co.pokeapi.pokekotlin.java - -import co.pokeapi.pokekotlin.internal.CompletableFutureConverter -import co.pokeapi.pokekotlin.internal.createPokeApiKtorfitBuilder -import co.pokeapi.pokekotlin.internal.getDefaultEngine -import co.pokeapi.pokekotlin.model.* -import de.jensklingenberg.ktorfit.http.GET -import de.jensklingenberg.ktorfit.http.Path -import de.jensklingenberg.ktorfit.http.Query -import io.ktor.client.* -import io.ktor.client.engine.* -import io.ktor.client.plugins.cache.storage.* -import java.util.concurrent.CompletableFuture -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.GlobalScope - -public interface PokeApi { - - private companion object { - @JvmStatic val default: PokeApi by lazy { create() } - - @OptIn(DelicateCoroutinesApi::class) - @JvmStatic - @JvmOverloads - fun create( - baseUrl: String = "https://pokeapi.co/api/v2/", - cacheStorage: CacheStorage? = null, - engine: HttpClientEngine = getDefaultEngine(), - coroutineScope: CoroutineScope = GlobalScope, - configure: HttpClientConfig<*>.() -> Unit = {}, - ): PokeApi { - return createPokeApiKtorfitBuilder( - baseUrl = baseUrl, - cacheStorage = cacheStorage, - engine = engine, - configure = configure, - ) - .converterFactories(CompletableFutureConverter.Factory(coroutineScope)) - .build() - .createPokeApi() - } - } - - // region Resource Lists - - // region Berries - - @GET("berry/") - public fun getBerryListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("berry-firmness/") - public fun getBerryFirmnessListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("berry-flavor/") - public fun getBerryFlavorListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - // endregion Berries - - // region Contests - - @GET("contest-type/") - public fun getContestTypeListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("contest-effect/") - public fun getContestEffectListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("super-contest-effect/") - public fun getSuperContestEffectListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - // endregion Contests - - // region Encounters - - @GET("encounter-method/") - public fun getEncounterMethodListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("encounter-condition/") - public fun getEncounterConditionListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("encounter-condition-value/") - public fun getEncounterConditionValueListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - // endregion - - // region Evolution - - @GET("evolution-chain/") - public fun getEvolutionChainListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("evolution-trigger/") - public fun getEvolutionTriggerListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - // endregion - - // region Games - - @GET("generation/") - public fun getGenerationListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("pokedex/") - public fun getPokedexListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("version/") - public fun getVersionListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("version-group/") - public fun getVersionGroupListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - // endregion - - // region Items - - @GET("item/") - public fun getItemListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("item-attribute/") - public fun getItemAttributeListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("item-category/") - public fun getItemCategoryListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("item-fling-effect/") - public fun getItemFlingEffectListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("item-pocket/") - public fun getItemPocketListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - // endregion - - // region Moves - - @GET("move/") - public fun getMoveListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("move-ailment/") - public fun getMoveAilmentListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("move-battle-style/") - public fun getMoveBattleStyleListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("move-category/") - public fun getMoveCategoryListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("move-damage-class/") - public fun getMoveDamageClassListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("move-learn-method/") - public fun getMoveLearnMethodListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("move-target/") - public fun getMoveTargetListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - // endregion - - // region Locations - - @GET("location/") - public fun getLocationListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("location-area/") - public fun getLocationAreaListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("pal-park-area/") - public fun getPalParkAreaListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("region/") - public fun getRegionListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - // endregion - - // region Machines - - @GET("machine/") - public fun getMachineListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - // endregion - - // region Pokemon - - @GET("ability/") - public fun getAbilityListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("characteristic/") - public fun getCharacteristicListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("egg-group/") - public fun getEggGroupListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("gender/") - public fun getGenderListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("growth-rate/") - public fun getGrowthRateListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("nature/") - public fun getNatureListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("pokeathlon-stat/") - public fun getPokeathlonStatListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("pokemon/") - public fun getPokemonListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("pokemon-color/") - public fun getPokemonColorListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("pokemon-form/") - public fun getPokemonFormListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("pokemon-habitat/") - public fun getPokemonHabitatListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("pokemon-shape/") - public fun getPokemonShapeListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("pokemon-species/") - public fun getPokemonSpeciesListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("stat/") - public fun getStatListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - @GET("type/") - public fun getTypeListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - // endregion - - // region Utility - - @GET("language/") - public fun getLanguageListAsync( - @Query("offset") offset: Int, - @Query("limit") limit: Int, - ): CompletableFuture - - // endregion - - // endregion - - // region Berries - - @GET("berry/{id}/") public fun getBerryAsync(@Path("id") id: Int): CompletableFuture - - @GET("berry-firmness/{id}/") - public fun getBerryFirmnessAsync(@Path("id") id: Int): CompletableFuture - - @GET("berry-flavor/{id}/") - public fun getBerryFlavorAsync(@Path("id") id: Int): CompletableFuture - - // endregion Berries - - // region Contests - - @GET("contest-type/{id}/") - public fun getContestTypeAsync(@Path("id") id: Int): CompletableFuture - - @GET("contest-effect/{id}/") - public fun getContestEffectAsync(@Path("id") id: Int): CompletableFuture - - @GET("super-contest-effect/{id}/") - public fun getSuperContestEffectAsync(@Path("id") id: Int): CompletableFuture - - // endregion Contests - - // region Encounters - - @GET("encounter-method/{id}/") - public fun getEncounterMethodAsync(@Path("id") id: Int): CompletableFuture - - @GET("encounter-condition/{id}/") - public fun getEncounterConditionAsync(@Path("id") id: Int): CompletableFuture - - @GET("encounter-condition-value/{id}/") - public fun getEncounterConditionValueAsync( - @Path("id") id: Int - ): CompletableFuture - - // endregion Contests - - // region Evolution - - @GET("evolution-chain/{id}/") - public fun getEvolutionChainAsync(@Path("id") id: Int): CompletableFuture - - @GET("evolution-trigger/{id}/") - public fun getEvolutionTriggerAsync(@Path("id") id: Int): CompletableFuture - - // endregion Evolution - - // region Games - - @GET("generation/{id}/") - public fun getGenerationAsync(@Path("id") id: Int): CompletableFuture - - @GET("pokedex/{id}/") public fun getPokedexAsync(@Path("id") id: Int): CompletableFuture - - @GET("version/{id}/") public fun getVersionAsync(@Path("id") id: Int): CompletableFuture - - @GET("version-group/{id}/") - public fun getVersionGroupAsync(@Path("id") id: Int): CompletableFuture - - // endregion Games - - // region Items - - @GET("item/{id}/") public fun getItemAsync(@Path("id") id: Int): CompletableFuture - - @GET("item-attribute/{id}/") - public fun getItemAttributeAsync(@Path("id") id: Int): CompletableFuture - - @GET("item-category/{id}/") - public fun getItemCategoryAsync(@Path("id") id: Int): CompletableFuture - - @GET("item-fling-effect/{id}/") - public fun getItemFlingEffectAsync(@Path("id") id: Int): CompletableFuture - - @GET("item-pocket/{id}/") - public fun getItemPocketAsync(@Path("id") id: Int): CompletableFuture - - // endregion Items - - // region Moves - - @GET("move/{id}/") public fun getMoveAsync(@Path("id") id: Int): CompletableFuture - - @GET("move-ailment/{id}/") - public fun getMoveAilmentAsync(@Path("id") id: Int): CompletableFuture - - @GET("move-battle-style/{id}/") - public fun getMoveBattleStyleAsync(@Path("id") id: Int): CompletableFuture - - @GET("move-category/{id}/") - public fun getMoveCategoryAsync(@Path("id") id: Int): CompletableFuture - - @GET("move-damage-class/{id}/") - public fun getMoveDamageClassAsync(@Path("id") id: Int): CompletableFuture - - @GET("move-learn-method/{id}/") - public fun getMoveLearnMethodAsync(@Path("id") id: Int): CompletableFuture - - @GET("move-target/{id}/") - public fun getMoveTargetAsync(@Path("id") id: Int): CompletableFuture - - // endregion Moves - - // region Locations - - @GET("location/{id}/") - public fun getLocationAsync(@Path("id") id: Int): CompletableFuture - - @GET("location-area/{id}/") - public fun getLocationAreaAsync(@Path("id") id: Int): CompletableFuture - - @GET("pal-park-area/{id}/") - public fun getPalParkAreaAsync(@Path("id") id: Int): CompletableFuture - - @GET("region/{id}/") public fun getRegionAsync(@Path("id") id: Int): CompletableFuture - - // endregion Locations - - // region Machines - - @GET("machine/{id}/") public fun getMachineAsync(@Path("id") id: Int): CompletableFuture - - // endregion - - // region Pokemon - - @GET("ability/{id}/") public fun getAbilityAsync(@Path("id") id: Int): CompletableFuture - - @GET("characteristic/{id}/") - public fun getCharacteristicAsync(@Path("id") id: Int): CompletableFuture - - @GET("egg-group/{id}/") - public fun getEggGroupAsync(@Path("id") id: Int): CompletableFuture - - @GET("gender/{id}/") public fun getGenderAsync(@Path("id") id: Int): CompletableFuture - - @GET("growth-rate/{id}/") - public fun getGrowthRateAsync(@Path("id") id: Int): CompletableFuture - - @GET("nature/{id}/") public fun getNatureAsync(@Path("id") id: Int): CompletableFuture - - @GET("pokeathlon-stat/{id}/") - public fun getPokeathlonStatAsync(@Path("id") id: Int): CompletableFuture - - @GET("pokemon/{id}/") public fun getPokemonAsync(@Path("id") id: Int): CompletableFuture - - @GET("pokemon/{id}/encounters/") - public fun getPokemonEncounterListAsync( - @Path("id") id: Int - ): CompletableFuture> - - @GET("pokemon-color/{id}/") - public fun getPokemonColorAsync(@Path("id") id: Int): CompletableFuture - - @GET("pokemon-form/{id}/") - public fun getPokemonFormAsync(@Path("id") id: Int): CompletableFuture - - @GET("pokemon-habitat/{id}/") - public fun getPokemonHabitatAsync(@Path("id") id: Int): CompletableFuture - - @GET("pokemon-shape/{id}/") - public fun getPokemonShapeAsync(@Path("id") id: Int): CompletableFuture - - @GET("pokemon-species/{id}/") - public fun getPokemonSpeciesAsync(@Path("id") id: Int): CompletableFuture - - @GET("stat/{id}/") public fun getStatAsync(@Path("id") id: Int): CompletableFuture - - @GET("type/{id}/") public fun getTypeAsync(@Path("id") id: Int): CompletableFuture - - // endregion Pokemon - - // region Utility - - @GET("language/{id}/") - public fun getLanguageAsync(@Path("id") id: Int): CompletableFuture - - // endregion Utility -} diff --git a/src/jvmTest/java/co/pokeapi/pokekotlin/test/LiveJavaTest.java b/src/jvmTest/java/co/pokeapi/pokekotlin/test/LiveJavaTest.java index add8b1f2..014d19f9 100644 --- a/src/jvmTest/java/co/pokeapi/pokekotlin/test/LiveJavaTest.java +++ b/src/jvmTest/java/co/pokeapi/pokekotlin/test/LiveJavaTest.java @@ -1,36 +1,34 @@ package co.pokeapi.pokekotlin.test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; - -import co.pokeapi.pokekotlin.java.PokeApi; +import co.pokeapi.pokekotlin.PokeApi; import io.ktor.client.plugins.ClientRequestException; import io.ktor.http.HttpStatusCode; import java.util.concurrent.ExecutionException; +import org.junit.Assert; import org.junit.Test; public class LiveJavaTest { @Test public void resource() throws ExecutionException, InterruptedException { - assertEquals("sitrus", PokeApi.getDefault().getBerryAsync(10).get().getName()); + Assert.assertEquals("sitrus", PokeApi.Default.getBerryAsync(10).get().getName()); } @Test public void list() throws ExecutionException, InterruptedException { - assertEquals( - PokeApi.getDefault().getMoveListAsync(0, 50).get().getResults().get(25), - PokeApi.getDefault().getMoveListAsync(25, 50).get().getResults().get(0)); + Assert.assertEquals( + PokeApi.Default.getBerryListAsync(0, 10).get().getResults().get(5), + PokeApi.Default.getBerryListAsync(5, 15).get().getResults().get(0)); } @Test public void notFound() { var exception = - assertThrows(ExecutionException.class, () -> PokeApi.getDefault().getMoveAsync(-1).get()); + Assert.assertThrows(ExecutionException.class, () -> PokeApi.Default.getMoveAsync(-1).get()); - assertEquals(ClientRequestException.class, exception.getCause().getClass()); + Assert.assertEquals(ClientRequestException.class, exception.getCause().getClass()); var cause = (ClientRequestException) exception.getCause(); - assertEquals(HttpStatusCode.Companion.getNotFound(), cause.getResponse().getStatus()); + Assert.assertEquals(HttpStatusCode.Companion.getNotFound(), cause.getResponse().getStatus()); } } diff --git a/src/jvmTest/kotlin/co/pokeapi/pokekotlin/test/EndpointTest.kt b/src/jvmTest/kotlin/co/pokeapi/pokekotlin/test/EndpointTest.kt index 2fa94e59..27b4ef51 100644 --- a/src/jvmTest/kotlin/co/pokeapi/pokekotlin/test/EndpointTest.kt +++ b/src/jvmTest/kotlin/co/pokeapi/pokekotlin/test/EndpointTest.kt @@ -2,9 +2,9 @@ package co.pokeapi.pokekotlin.test import co.pokeapi.pokekotlin.PokeApi import co.pokeapi.pokekotlin.internal.PokeApiJson -import io.ktor.client.HttpClient -import io.ktor.client.request.get -import io.ktor.client.statement.bodyAsText +import io.ktor.client.* +import io.ktor.client.request.* +import io.ktor.client.statement.* import kotlin.reflect.full.declaredMemberFunctions import kotlin.test.Test import kotlin.test.assertEquals @@ -37,6 +37,7 @@ class EndpointTest { val actualResources = PokeApi::class .declaredMemberFunctions + .filter { it.isSuspend } .map { it.name.removePrefix("get") } .groupBy { it.endsWith("List") }