Skip to content

Commit

Permalink
fix cors error on wasm app
Browse files Browse the repository at this point in the history
  • Loading branch information
Fethi Tewelde committed Oct 5, 2024
1 parent 8dd9c50 commit c2695a7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions core/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ buildConfig {
"RIJKSMUSEUM_API_KEY",
"\"${secretKeyProperties["rijksmuseum.api.key"]}\""
)
buildConfigField(
"String",
"APP_NAME",
"\"${rootProject.name}\""
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ import io.ktor.http.URLProtocol
import io.ktor.http.path
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json
import org.koin.core.module.dsl.bind
import org.koin.core.module.dsl.singleOf
import org.koin.core.qualifier.named
import org.koin.dsl.module

const val KEY = "key"

val networkModule = module {
singleOf(::KtorRijksMuseumNetwork) { bind<RijksMuseumNetworkDataSource>() }
single<RijksMuseumNetworkDataSource> {
KtorRijksMuseumNetwork(
rijksmuseumClient = get(named(BuildConfig.APP_NAME)),
client = get()
)
}
single {
Json {
ignoreUnknownKeys = true
Expand All @@ -35,7 +39,7 @@ val networkModule = module {
prettyPrint = true
}
}
single {
single(named(BuildConfig.APP_NAME)) {
HttpClient {
install(ContentNegotiation) {
json(get())
Expand All @@ -56,4 +60,8 @@ val networkModule = module {
}
}
}

single {
HttpClient()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@ const val PAGE = "p"
const val HAS_IMAGE = "hasImage"

class KtorRijksMuseumNetwork(
private val rijksmuseumClient: HttpClient,
private val client: HttpClient,
) : RijksMuseumNetworkDataSource {
override suspend fun getCollection(page: Int): List<NetworkArt> =
client.get("$COLLECTION?&$PAGE=$page&$PS=$PAGING_PAGE_SIZE")
rijksmuseumClient.get("$COLLECTION?&$PAGE=$page&$PS=$PAGING_PAGE_SIZE")
.body<CollectionNetworkResponse>().networkArtObjects


override suspend fun getDetail(objectId: String): NetworkArtObject =
client.get("$COLLECTION/$objectId")
rijksmuseumClient.get("$COLLECTION/$objectId")
.body<DetailNetworkResponse>().networkArtObject

override suspend fun downloadImage(url: String, onDownload: (Long, Long?) -> Unit): ByteReadChannel {
return client.get("https://corsproxy.io/?$url") {
override suspend fun downloadImage(
url: String,
onDownload: (Long, Long?) -> Unit
): ByteReadChannel = client.get(url) {
onDownload { bytesSentTotal, contentLength ->
onDownload(bytesSentTotal, contentLength)
}
}.bodyAsChannel()
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ androidx-material = "1.12.0"
androidx-test-junit = "1.2.1"
coil = "3.0.0-alpha09"
compose-plugin = "1.6.11"
filekit = "0.8.3"
filekit = "0.8.4"
junit = "4.13.2"
kotlin = "2.0.20"
koin = "4.0.0"
Expand Down

0 comments on commit c2695a7

Please sign in to comment.