Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ set windows-shell := ["C:\\Program Files\\Git\\bin\\sh.exe", "-c"]
_default:
just --list

test-server-cmd := "npx -y http-server src/commonTest/resources/data -e json -p 8080 --cors"

# Serve PokeAPI data from static files
test-server:
npx -y http-server src/commonTest/resources/data -e json -p 8080 --cors
{{ test-server-cmd }}

# Spawn a background job serving PokeAPI data from static files
test-server-background:
npx -y http-server src/commonTest/resources/data -e json -p 8080 --cors &
{{ test-server-cmd }} &
5 changes: 4 additions & 1 deletion src/commonMain/kotlin/dev/sargunv/pokekotlin/PokeApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import de.jensklingenberg.ktorfit.Ktorfit.Builder
import de.jensklingenberg.ktorfit.http.GET
import de.jensklingenberg.ktorfit.http.Path
import de.jensklingenberg.ktorfit.http.Query
import dev.sargunv.pokekotlin.internal.PokeApiConverter
import dev.sargunv.pokekotlin.internal.PokeApiJson
import dev.sargunv.pokekotlin.internal.getDefaultEngine
import dev.sargunv.pokekotlin.model.Ability
Expand Down Expand Up @@ -65,6 +66,7 @@ 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
import kotlin.jvm.JvmName

Expand Down Expand Up @@ -574,8 +576,9 @@ fun PokeApi(
HttpClient(engine) {
configure()
install(HttpCache) { cacheStorage?.let { privateStorage(it) } }
install(ContentNegotiation) { json(PokeApiJson) }
install(ContentNegotiation) { json(PokeApiJson, ContentType.Any) }
}
)
.converterFactories(PokeApiConverter.Factory)
.build()
.createPokeApi()
3 changes: 0 additions & 3 deletions src/commonMain/kotlin/dev/sargunv/pokekotlin/PokeApiError.kt

This file was deleted.

13 changes: 13 additions & 0 deletions src/commonMain/kotlin/dev/sargunv/pokekotlin/PokeApiException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.sargunv.pokekotlin

import io.ktor.http.HttpStatusCode

sealed class PokeApiException : Throwable {
constructor(message: String) : super(message)

constructor(cause: Throwable) : super(cause)

class HttpStatus(val status: HttpStatusCode) : PokeApiException(status.toString())

class UnknownException(cause: Throwable) : PokeApiException(cause)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.sargunv.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 dev.sargunv.pokekotlin.PokeApiException
import io.ktor.client.call.body
import io.ktor.client.statement.HttpResponse
import io.ktor.http.isSuccess

internal class PokeApiConverter(private val typeData: TypeData) :
Converter.SuspendResponseConverter<HttpResponse, Any> {
override suspend fun convert(result: KtorfitResult): Any {
return when (result) {
is KtorfitResult.Failure -> throw PokeApiException.UnknownException(result.throwable)
is KtorfitResult.Success -> {
val status = result.response.status
when {
status.isSuccess() -> result.response.body<Any>(typeData.typeInfo)
else -> throw PokeApiException.HttpStatus(status)
}
}
}
}

internal object Factory : Converter.Factory {
override fun suspendResponseConverter(
typeData: TypeData,
ktorfit: Ktorfit,
): Converter.SuspendResponseConverter<HttpResponse, Any> = PokeApiConverter(typeData)
}
}
24 changes: 0 additions & 24 deletions src/commonTest/kotlin/dev/sargunv/pokekotlin/test/ErrorTest.kt

This file was deleted.

13 changes: 11 additions & 2 deletions src/commonTest/kotlin/dev/sargunv/pokekotlin/test/LiveTest.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package dev.sargunv.pokekotlin.test

import dev.sargunv.pokekotlin.PokeApi
import dev.sargunv.pokekotlin.PokeApiException
import io.ktor.http.HttpStatusCode.Companion.NotFound
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlinx.coroutines.test.runTest

@Ignore
class LiveTest {

@Test fun liveObject() = runTest { assertEquals("sitrus", PokeApi.getBerry(10).name) }
@Test fun resource() = runTest { assertEquals("sitrus", PokeApi.getBerry(10).name) }

@Test
fun liveList() = runTest {
fun list() = runTest {
assertEquals(PokeApi.getMoveList(0, 50).results[25], PokeApi.getMoveList(25, 50).results[0])
}

@Test
fun notFound() = runTest {
val e = assertFailsWith(PokeApiException.HttpStatus::class) { PokeApi.getMove(-1) }
assertEquals(NotFound, e.status)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.sargunv.pokekotlin.test

import dev.sargunv.pokekotlin.PokeApi
import dev.sargunv.pokekotlin.PokeApiException
import io.ktor.http.HttpStatusCode.Companion.NotFound
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlinx.coroutines.test.runTest

class PokeApiExceptionTest {
@Test
fun notFoundError() = runTest {
val e = assertFailsWith(PokeApiException.HttpStatus::class) { LocalPokeApi.getMove(-1) }
assertEquals(NotFound, e.status)
}

@Test
fun badUrlError() = runTest {
assertFailsWith(PokeApiException.UnknownException::class) {
PokeApi("https://localhost:12345/").getBerry(10)
}
}
}