Skip to content

Commit 1b97935

Browse files
committed
feat: add ping to kotlin.
1 parent 09f1197 commit 1b97935

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,25 @@ class Client @JvmOverloads constructor(
170170
return this
171171
}
172172

173+
/**
174+
* Sends a "ping" request to Appwrite to verify connectivity.
175+
*
176+
* @return String
177+
*/
178+
suspend fun ping(): String {
179+
val apiPath = "/ping"
180+
val apiParams = mutableMapOf<String, Any?>()
181+
val apiHeaders = mutableMapOf("content-type" to "application/json")
182+
183+
return call(
184+
"GET",
185+
apiPath,
186+
apiHeaders,
187+
apiParams,
188+
responseType = String::class.java
189+
)
190+
}
191+
173192
/**
174193
* Prepare the HTTP request
175194
*
@@ -536,6 +555,14 @@ class Client @JvmOverloads constructor(
536555
it.resume(true as T)
537556
return
538557
}
558+
responseType == String::class.java -> {
559+
val body = response.body!!
560+
.charStream()
561+
.buffered()
562+
.use(BufferedReader::readText)
563+
it.resume(body as T)
564+
return
565+
}
539566
responseType == ByteArray::class.java -> {
540567
it.resume(response.body!!
541568
.byteStream()

tests/KotlinJava11Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class KotlinJava11Test extends Base
2020
'docker run --network="mockapi" -v $(pwd):/app -w /app/tests/sdks/kotlin openjdk:11-jdk-slim sh -c "./gradlew test -q && cat result.txt"';
2121

2222
protected array $expectedOutput = [
23+
...Base::PING_RESPONSE,
2324
...Base::FOO_RESPONSES,
2425
...Base::BAR_RESPONSES,
2526
...Base::GENERAL_RESPONSES,

tests/KotlinJava17Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class KotlinJava17Test extends Base
2020
'docker run --network="mockapi" -v $(pwd):/app -w /app/tests/sdks/kotlin openjdk:17-jdk-slim sh -c "./gradlew test -q && cat result.txt"';
2121

2222
protected array $expectedOutput = [
23+
...Base::PING_RESPONSE,
2324
...Base::FOO_RESPONSES,
2425
...Base::BAR_RESPONSES,
2526
...Base::GENERAL_RESPONSES,

tests/KotlinJava8Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class KotlinJava8Test extends Base
2020
'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/kotlin openjdk:8-jdk-slim sh -c "./gradlew test -q && cat result.txt"';
2121

2222
protected array $expectedOutput = [
23+
...Base::PING_RESPONSE,
2324
...Base::FOO_RESPONSES,
2425
...Base::BAR_RESPONSES,
2526
...Base::GENERAL_RESPONSES,

tests/languages/kotlin/Tests.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import kotlinx.coroutines.runBlocking
1919
import okhttp3.Response
2020
import org.junit.Before
2121
import org.junit.Test
22+
import org.json.JSONObject
2223
import java.io.File
2324
import java.io.IOException
2425
import java.nio.file.Files
@@ -38,8 +39,19 @@ class ServiceTest {
3839
@Throws(IOException::class)
3940
fun test() {
4041
val client = Client()
42+
.setProject("123456")
4143
.addHeader("Origin", "http://localhost")
4244
.setSelfSigned(true)
45+
46+
runBlocking {
47+
val ping = client.ping()
48+
val pingResponse = parse(ping)
49+
writeToFile(pingResponse)
50+
}
51+
52+
// reset project
53+
client.setProject("123456")
54+
4355
val foo = Foo(client)
4456
val bar = Bar(client)
4557
val general = General(client)
@@ -185,4 +197,11 @@ class ServiceTest {
185197
File("result.txt").appendText(text)
186198
}
187199

200+
private fun parse(json: String): String? {
201+
return try {
202+
JSONObject(json).getString("result")
203+
} catch (exception: Exception) {
204+
null
205+
}
206+
}
188207
}

0 commit comments

Comments
 (0)