Skip to content

Commit f30f801

Browse files
committed
WIP raise
1 parent 9d9fe8b commit f30f801

File tree

7 files changed

+57
-7
lines changed

7 files changed

+57
-7
lines changed

app/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
@file:OptIn(ExperimentalWasmDsl::class)
2+
13
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
4+
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
25
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig
36

47
plugins {

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ plugins {
44
alias(libs.plugins.sqlDelight) apply false
55
alias(libs.plugins.compose.compiler) apply false
66
alias(libs.plugins.compose.runtime) apply false
7+
alias(libs.plugins.ksp) apply false
78
}
89

910
group = "org.example"

discord-bot/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
kotlin("jvm")
3+
alias(libs.plugins.ksp)
34
}
45

56
group = "org.example"
@@ -10,6 +11,8 @@ dependencies {
1011
implementation(project(":carddb"))
1112
implementation(project(":utils"))
1213
implementation(libs.bundles.base)
14+
implementation(libs.arrow.optics)
15+
ksp(libs.arrow.optics.ksp)
1316
}
1417
kotlin {
1518
compilerOptions {
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
import arrow.core.raise.either
2+
import client.ScryfallApiImpl
13
import com.mfriend.db.databaseModule
2-
import kotlinx.coroutines.coroutineScope
34
import org.koin.core.KoinApplication
45
import org.koin.core.context.GlobalContext.startKoin
56

6-
suspend fun main() = coroutineScope {
7-
7+
suspend fun main() {
8+
ScryfallApiImpl().use { scryfallApi ->
9+
val x = either {
10+
scryfallApi.searchCardRaise("")
11+
}
12+
println(x)
13+
}
814
}
915

1016
fun initKoin(): KoinApplication = startKoin {
1117
modules(databaseModule, scryfallModule)
12-
}
18+
}

gradle/libs.versions.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[versions]
22
kotlin = "2.2.0-Beta2"
33
kermit = "2.0.5"
4-
arrow = "2.0.1"
4+
arrow = "2.1.1"
55
koin = "4.0.3"
66
ktor = "3.1.2"
77
sqlDelight = "2.0.0"
@@ -12,6 +12,8 @@ lifecycleViewmodelAndroid = "2.8.7"
1212
kermit = { module = "co.touchlab:kermit", version.ref = "kermit" }
1313
coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version = "1.10.2" }
1414
arrow = { module = "io.arrow-kt:arrow-core", version.ref = "arrow" }
15+
arrow-optics = { module = "io.arrow-kt:arrow-optics", version.ref = "arrow" }
16+
arrow-optics-ksp = { module = "io.arrow-kt:arrow-optics-ksp-plugin", version = "2.1.1" }
1517

1618
koin = { module = "io.insert-koin:koin-core", version.ref = "koin" }
1719
koin-compose = { module = "io.insert-koin:koin-compose", version.ref = "koin" }
@@ -39,6 +41,7 @@ sqlDelight = { id = "app.cash.sqldelight", version.ref = "sqlDelight" }
3941
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
4042
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
4143
compose-runtime = { id = "org.jetbrains.compose", version.ref = "jetbrains-compose" }
44+
ksp = { id = "com.google.devtools.ksp", version = "2.1.20-2.0.0" }
4245

4346
[bundles]
4447
base = [

scryfall/src/commonMain/kotlin/client/ScryfallApi.kt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@ package client
22

33
import arrow.core.Either
44
import arrow.core.left
5+
import arrow.core.raise.Raise
56
import arrow.core.right
7+
import bind
68
import co.touchlab.kermit.Logger
9+
import ensure
710
import io.ktor.client.call.body
811
import io.ktor.client.request.HttpRequestBuilder
912
import io.ktor.client.request.get
1013
import io.ktor.client.request.parameter
1114
import io.ktor.client.statement.HttpResponse
1215
import io.ktor.http.encodedPath
16+
import io.ktor.http.isSuccess
1317
import io.ktor.http.takeFrom
1418
import models.CardDto
1519
import models.ListResp
1620
import models.SetDto
1721

18-
interface ScryfallApi {
22+
interface ScryfallApi : AutoCloseable {
1923
suspend fun cardNamed(name: String): Either<String, CardDto>
2024
suspend fun searchCard(searchParam: String): Either<String, List<CardDto>>
2125
suspend fun sets(): Either<String, List<SetDto>>
@@ -26,7 +30,12 @@ suspend inline fun <reified T> HttpResponse.toEither(): Either<String, T> = when
2630
else -> this.body<String>().left()
2731
}
2832

29-
class ScryfallApiImpl : ScryfallApi {
33+
context(_: Raise<Throwable>)
34+
inline fun <T> catch(block: () -> T): T {
35+
return Either.catch(block).bind()
36+
}
37+
38+
class ScryfallApiImpl : ScryfallApi, AutoCloseable {
3039
private val client = newKtorClient()
3140
override suspend fun cardNamed(name: String): Either<String, CardDto> {
3241
val response = client.get {
@@ -37,6 +46,25 @@ class ScryfallApiImpl : ScryfallApi {
3746
return response.toEither()
3847
}
3948

49+
// This NEEDS to be called or it will hold up the couroutine scope of suspend fun main
50+
override fun close() {
51+
client.close()
52+
}
53+
54+
context(_: Raise<Throwable>)
55+
suspend fun searchCardRaise(searchParam: String): List<CardDto> {
56+
return catch {
57+
val response = client.get {
58+
scryfall("$CardApiBase$Search")
59+
parameter("q", searchParam)
60+
}
61+
ensure(response.status.isSuccess()) {
62+
IllegalStateException(response.body<String>())
63+
}
64+
response.body<ListResp<CardDto>>().data
65+
}
66+
}
67+
4068
override suspend fun searchCard(searchParam: String): Either<String, List<CardDto>> {
4169
val resp: HttpResponse = client.get {
4270
scryfall("$CardApiBase$Search")

utils/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
@file:OptIn(ExperimentalWasmDsl::class)
2+
3+
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
4+
15
plugins {
26
alias(libs.plugins.kotlin.multiplatform)
37
}
@@ -14,6 +18,8 @@ kotlin {
1418
iosX64()
1519
iosArm64()
1620
iosSimulatorArm64()
21+
wasmJs()
22+
js()
1723
applyDefaultHierarchyTemplate()
1824
sourceSets {
1925
commonMain.dependencies {

0 commit comments

Comments
 (0)