Skip to content

Commit 715e01d

Browse files
committed
add: ping to android kotlin.
1 parent 1d644da commit 715e01d

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed

templates/android/library/src/main/java/io/package/Client.kt.twig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,25 @@ class Client @JvmOverloads constructor(
211211
return this
212212
}
213213

214+
/**
215+
* Sends a "ping" request to Appwrite to verify connectivity.
216+
*
217+
* @return String
218+
*/
219+
suspend fun ping(): String {
220+
val apiPath = "/ping"
221+
val apiParams = mutableMapOf<String, Any?>()
222+
val apiHeaders = mutableMapOf("content-type" to "application/json")
223+
224+
return call(
225+
"GET",
226+
apiPath,
227+
apiHeaders,
228+
apiParams,
229+
responseType = String::class.java
230+
)
231+
}
232+
214233
/**
215234
* Send the HTTP request
216235
*
@@ -489,6 +508,14 @@ class Client @JvmOverloads constructor(
489508
it.resume(true as T)
490509
return
491510
}
511+
responseType == String::class.java -> {
512+
val body = response.body!!
513+
.charStream()
514+
.buffered()
515+
.use(BufferedReader::readText)
516+
it.resume(body as T)
517+
return
518+
}
492519
responseType == ByteArray::class.java -> {
493520
it.resume(response.body!!
494521
.byteStream()

tests/Android14Java11Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Android14Java11Test extends Base
2020
'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/android alvrme/alpine-android:android-34-jdk11 sh -c "./gradlew :library:testReleaseUnitTest --stacktrace -q && cat library/result.txt"';
2121

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

tests/Android14Java17Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Android14Java17Test extends Base
2020
'docker run --rm --network="mockapi" -v $(pwd):/app -w /app/tests/sdks/android alvrme/alpine-android:android-34-jdk17 sh -c "./gradlew :library:testReleaseUnitTest --stacktrace -q && cat library/result.txt"';
2121

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

tests/Android14Java8Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Android14Java8Test extends Base
2020
'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/android alvrme/alpine-android:android-34-jdk8 sh -c "./gradlew :library:testReleaseUnitTest --stacktrace -q && cat library/result.txt"';
2121

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

tests/Android5Java17Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Android5Java17Test extends Base
2020
'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/android alvrme/alpine-android:android-21-jdk17 sh -c "./gradlew :library:testReleaseUnitTest --stacktrace -q && cat library/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/android/Tests.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.junit.After
2929
import org.junit.Before
3030
import org.junit.Test
3131
import org.junit.runner.RunWith
32+
import org.json.JSONObject
3233
import org.robolectric.annotation.Config
3334
import java.io.File
3435
import java.io.IOException
@@ -61,10 +62,20 @@ class ServiceTest {
6162
@Throws(IOException::class)
6263
fun test() {
6364
val client = Client(ApplicationProvider.getApplicationContext())
64-
.setEndpointRealtime("wss://cloud.appwrite.io/v1")
65-
.setProject("console")
65+
.setProject("123456")
6666
.addHeader("Origin", "http://localhost")
6767
.setSelfSigned(true)
68+
69+
runBlocking {
70+
val ping = client.ping()
71+
val pingResponse = parse(ping)
72+
writeToFile(pingResponse)
73+
}
74+
75+
// reset configs
76+
client.setProject("console")
77+
.setEndpointRealtime("wss://cloud.appwrite.io/v1")
78+
6879
val foo = Foo(client)
6980
val bar = Bar(client)
7081
val general = General(client)
@@ -219,4 +230,11 @@ class ServiceTest {
219230
File("result.txt").appendText(text)
220231
}
221232

233+
private fun parse(json: String): String? {
234+
return try {
235+
JSONObject(json).getString("result")
236+
} catch (exception: Exception) {
237+
null
238+
}
239+
}
222240
}

0 commit comments

Comments
 (0)