@@ -3,10 +3,11 @@ package client
33import arrow.core.Either
44import arrow.core.left
55import arrow.core.raise.Raise
6+ import arrow.core.raise.either
7+ import arrow.core.raise.withError
68import arrow.core.right
79import bind
810import co.touchlab.kermit.Logger
9- import ensure
1011import io.ktor.client.call.body
1112import io.ktor.client.request.HttpRequestBuilder
1213import io.ktor.client.request.get
@@ -17,11 +18,27 @@ import io.ktor.http.isSuccess
1718import io.ktor.http.takeFrom
1819import models.CardDto
1920import models.ListResp
21+ import models.ScryfallError
2022import models.SetDto
23+ import raise
2124
2225interface ScryfallApi : AutoCloseable {
26+ context(_: Raise <ScryfallError >)
27+ suspend fun cardNamedRaise (name : String ): CardDto
28+
29+ context(_: Raise <ScryfallError >)
30+ suspend fun searchCardRaise (searchParam : String ): List <CardDto >
31+
32+ context(_: Raise <ScryfallError >)
33+ suspend fun setsRaise (): List <SetDto >
34+
35+ @Deprecated(" Use raise variant" , replaceWith = ReplaceWith (" cardNamedRaise(name)" ))
2336 suspend fun cardNamed (name : String ): Either <String , CardDto >
37+
38+ @Deprecated(" Use raise variant" , replaceWith = ReplaceWith (" searchCardRaise(searchParam)" ))
2439 suspend fun searchCard (searchParam : String ): Either <String , List <CardDto >>
40+
41+ @Deprecated(" Use raise variant" , replaceWith = ReplaceWith (" setsRaise()" ))
2542 suspend fun sets (): Either <String , List <SetDto >>
2643}
2744
@@ -37,34 +54,46 @@ inline fun <T> catch(block: () -> T): T {
3754
3855class ScryfallApiImpl : ScryfallApi , AutoCloseable {
3956 private val client = newKtorClient()
40- override suspend fun cardNamed (name : String ): Either <String , CardDto > {
41- val response = client.get {
42- scryfall(" $CardApiBase$FindNamed " )
43- parameter(" fuzzy" , name)
57+
58+ // TODO handle exceptions/responses that dont fit ScryfallError
59+ context(_: Raise <ScryfallError >)
60+ private suspend inline fun <reified T > HttpResponse.bodyOrError (): T {
61+ return if (status.isSuccess()) {
62+ body<T >()
63+ } else {
64+ raise(body<ScryfallError >())
4465 }
66+ }
4567
46- return response.toEither()
68+ /* *
69+ * Gets a single card object from the search or raises a [ScryfallError]
70+ * If the error response doesnt fit [ScryfallError] throws an exception
71+ */
72+ context(_: Raise <ScryfallError >)
73+ override suspend fun cardNamedRaise (name : String ): CardDto {
74+ return client.get {
75+ scryfall(" $CardApiBase$FindNamed " )
76+ parameter(" fuzzy" , name)
77+ }.bodyOrError()
4778 }
4879
80+
4981 // This NEEDS to be called or it will hold up the couroutine scope of suspend fun main
5082 override fun close () {
5183 client.close()
5284 }
5385
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- }
86+ context(_: Raise <ScryfallError >)
87+ override suspend fun searchCardRaise (searchParam : String ): List <CardDto > = client.get {
88+ scryfall(" $CardApiBase$Search " )
89+ parameter(" q" , searchParam)
90+ }.bodyOrError<ListResp <CardDto >>().data
91+
92+ context(_: Raise <ScryfallError >)
93+ override suspend fun setsRaise (): List <SetDto > =
94+ client.get { scryfall(" sets" ) }.bodyOrError<ListResp <SetDto >>().data
6795
96+ @Deprecated(" Use raise variant" , replaceWith = ReplaceWith (" searchCardRaise(searchParam)" ))
6897 override suspend fun searchCard (searchParam : String ): Either <String , List <CardDto >> {
6998 val resp: HttpResponse = client.get {
7099 scryfall(" $CardApiBase$Search " )
@@ -77,13 +106,12 @@ class ScryfallApiImpl : ScryfallApi, AutoCloseable {
77106 .map { it.data }
78107 }
79108
80- override suspend fun sets (): Either <String , List <SetDto >> {
81- val resp: HttpResponse = client.get {
82- scryfall(" sets" )
83- }
84- return resp
85- .toEither<ListResp <SetDto >>()
86- .map { it.data }
109+ @Deprecated(" Use raise variant" , replaceWith = ReplaceWith (" setsRaise()" ))
110+ override suspend fun sets (): Either <String , List <SetDto >> = either { withError({ it.details }) { setsRaise() } }
111+
112+ @Deprecated(" Use raise variant" , replaceWith = ReplaceWith (" cardNamedRaise(name)" ))
113+ override suspend fun cardNamed (name : String ): Either <String , CardDto > = either {
114+ withError({ it.details }) { cardNamedRaise(name) }
87115 }
88116
89117 companion object {
0 commit comments