Skip to content

Commit a17341f

Browse files
authored
Merge pull request #116 from fmasa/desktop-functions
Implement cloud functions support for Desktop
2 parents 2ec8779 + 5700e7a commit a17341f

File tree

6 files changed

+55
-12
lines changed

6 files changed

+55
-12
lines changed

buildSrc/src/main/java/Versions.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ object Versions {
66
val agp = loadPropertyFromResources(versionsFile, "androidGradlePluginVersion")
77
val compose = loadPropertyFromResources(versionsFile, "composeVersion")
88
val kotlin = loadPropertyFromResources(versionsFile, "kotlinVersion")
9+
val ktor = "2.0.0"
910

1011
val napier = loadPropertyFromResources(versionsFile, "napierVersion")
1112

common/build.gradle.kts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ kotlin {
2929
}
3030
}
3131

32-
val ktorVersion = "2.0.0"
3332
val kodeinVersion = "7.11.0"
3433

3534
val commonMain by getting {
@@ -46,8 +45,6 @@ kotlin {
4645

4746
api(project(":common:firebase"))
4847

49-
api("io.ktor:ktor-client-core:$ktorVersion")
50-
5148
// Basic Kotlin stuff
5249
api("org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}")
5350

@@ -70,9 +67,10 @@ kotlin {
7067
api("io.github.aakira:napier:${Versions.napier}")
7168

7269
// HTTP client
73-
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
74-
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
75-
api("io.ktor:ktor-client-cio:$ktorVersion")
70+
implementation("io.ktor:ktor-client-content-negotiation:${Versions.ktor}")
71+
implementation("io.ktor:ktor-serialization-kotlinx-json:${Versions.ktor}")
72+
api("io.ktor:ktor-client-cio:${Versions.ktor}")
73+
api("io.ktor:ktor-client-core:${Versions.ktor}")
7674

7775
val richtextVersion = "0.13.0"
7876
implementation("com.halilibo.compose-richtext:richtext-commonmark:$richtextVersion")

common/firebase/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ kotlin {
2323
dependencies {
2424
api("com.google.cloud:google-cloud-firestore:3.0.18")
2525
api("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.6.4")
26+
implementation("io.ktor:ktor-client-content-negotiation:${Versions.ktor}")
27+
implementation("io.ktor:ktor-serialization-kotlinx-json:${Versions.ktor}")
28+
api("io.ktor:ktor-client-cio:${Versions.ktor}")
29+
api("io.ktor:ktor-client-core:${Versions.ktor}")
2630
}
2731
}
2832
named("androidMain") {
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
package cz.frantisekmasa.wfrp_master.common.firebase.functions
22

3-
actual class CloudFunctions(token: String, projectId: String) {
3+
import io.ktor.client.HttpClient
4+
5+
actual class CloudFunctions(
6+
private val token: String,
7+
private val projectId: String,
8+
private val region: String,
9+
private val http: HttpClient,
10+
) {
411
actual fun getHttpsCallable(name: String): HttpsCallableReference {
5-
TODO("Implement code that calls HTTP request")
6-
// see spec: https://firebase.google.com/docs/functions/callable-reference
12+
return HttpsCallableReference(
13+
url = "https://$region-$projectId.cloudfunctions.net/$name",
14+
token = token,
15+
http = http,
16+
)
717
}
818
}
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
package cz.frantisekmasa.wfrp_master.common.firebase.functions
22

3-
actual class HttpsCallableReference {
3+
import io.ktor.client.HttpClient
4+
import io.ktor.client.request.bearerAuth
5+
import io.ktor.client.request.post
6+
import io.ktor.client.request.setBody
7+
import io.ktor.client.statement.bodyAsText
8+
import io.ktor.http.ContentType
9+
import io.ktor.http.contentType
10+
11+
actual class HttpsCallableReference(
12+
private val url: String,
13+
private val token: String,
14+
private val http: HttpClient,
15+
) {
416
actual suspend fun call(data: Any): HttpsCallableResult {
5-
TODO("Implement using Ktor")
17+
val response = http.post(url) {
18+
contentType(ContentType.Application.Json)
19+
bearerAuth(token)
20+
setBody(mapOf("data" to data))
21+
}
22+
23+
if (response.status.value >= 300) {
24+
throw Exception("Cloud function returned ${response.status} (${response.bodyAsText()}")
25+
}
26+
27+
return HttpsCallableResult
628
}
729
}

common/src/jvmMain/kotlin/cz/frantisekmasa/wfrp_master/common/platformModule.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@ internal actual val platformModule = DI.Module("jvm") {
3636
}
3737

3838
val firebaseProjectId = "dnd-master-58fca"
39+
val region = "us-central1"
3940

4041
bindSingleton { SettingsStorage() }
4142
bindSingleton { AuthenticationManager(instance(), instance(), instance()) }
4243

4344
bindSingleton { FirebaseTokenHolder() }
44-
bindSingleton { CloudFunctions(instance<FirebaseTokenHolder>().getToken(), firebaseProjectId) }
45+
bindSingleton {
46+
CloudFunctions(
47+
instance<FirebaseTokenHolder>().getToken(),
48+
firebaseProjectId,
49+
region,
50+
instance(),
51+
)
52+
}
4553
bindSingleton {
4654
val tokenHolder: FirebaseTokenHolder = instance()
4755
val firestore = FirestoreOptions.newBuilder()

0 commit comments

Comments
 (0)