-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Davide Farella
committed
Oct 24, 2020
1 parent
d13741c
commit 8c7e850
Showing
3 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
network/src/commonMain/kotlin/network/ktorEitherSupport.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package network | ||
|
||
import entities.Either | ||
import entities.Left | ||
import entities.NetworkError | ||
import entities.Right | ||
import io.ktor.client.HttpClientConfig | ||
import io.ktor.client.features.HttpResponseValidator | ||
|
||
/** | ||
* Catch [KtorEitherException] for create an [Either] | ||
* @return [Either] of [NetworkError] and [B] | ||
*/ | ||
inline fun <B> Either.Companion.Try(block: () -> B): Either<NetworkError, B> = | ||
try { | ||
Right(block()) | ||
} catch (e: KtorEitherException) { | ||
Left(e.reason) | ||
} | ||
|
||
internal fun HttpClientConfig<*>.withEitherValidator() = | ||
HttpResponseValidator { | ||
validateResponse { response -> | ||
val error = when (response.status.value) { | ||
404 -> NetworkError.NotFound | ||
else -> null | ||
} | ||
error?.let { throw KtorEitherException(it) } | ||
} | ||
} | ||
|
||
@PublishedApi | ||
internal class KtorEitherException(val reason: NetworkError): Throwable(reason.toString()) |
57 changes: 57 additions & 0 deletions
57
network/src/jvmTest/kotlin/network/KtorEitherSupportTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package network | ||
|
||
import assert4k.* | ||
import entities.Either | ||
import entities.NetworkError | ||
import entities.left | ||
import entities.right | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.engine.mock.MockEngine | ||
import io.ktor.client.engine.mock.respond | ||
import io.ktor.client.request.get | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.http.fullPath | ||
import util.test.CoroutinesTest | ||
import kotlin.test.* | ||
|
||
internal class KtorEitherSupportTest: CoroutinesTest { | ||
|
||
class TestService(private val client: HttpClient) { | ||
|
||
suspend fun success() = Either.Try { | ||
client.get<String>("success") | ||
} | ||
|
||
suspend fun notFound() = Either.Try { | ||
client.get<String>("not_found") | ||
} | ||
} | ||
|
||
private val client = HttpClient(MockEngine) { | ||
engine { | ||
addHandler { request -> | ||
|
||
val url = request.url.fullPath.substringAfter('/') | ||
fun error(code: Int) = respond("url", HttpStatusCode(code, url)) | ||
|
||
when (url) { | ||
"success" -> respond("hello") | ||
"not_found" -> error(404) | ||
else -> error("Unhandled $url") | ||
} | ||
} | ||
} | ||
withEitherValidator() | ||
} | ||
private val service = TestService(client) | ||
|
||
@Test | ||
fun `correctly create an Either of String`() = coroutinesTest { | ||
assert that service.success() equals "hello".right() | ||
} | ||
|
||
@Test | ||
fun `correctly create an Either of NotFound error`() = coroutinesTest { | ||
assert that service.notFound() equals NetworkError.NotFound.left() | ||
} | ||
} |