Skip to content

Commit 05a9855

Browse files
Merge pull request #1016 from ItzNotABug/add-ping-to-mobile-sdks
Add `ping` to Mobile and other SDKs
2 parents 27d8ecd + b30d003 commit 05a9855

31 files changed

+277
-15
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()

templates/apple/Sources/Client.swift.twig

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,26 @@ open class Client {
214214
) ?? ""
215215
}
216216

217+
///
218+
/// Sends a "ping" request to Appwrite to verify connectivity.
219+
///
220+
/// @return String
221+
/// @throws Exception
222+
///
223+
open func ping() async throws -> String {
224+
let apiPath: String = "/ping"
225+
226+
let apiHeaders: [String: String] = [
227+
"content-type": "application/json"
228+
]
229+
230+
return try await call(
231+
method: "GET",
232+
path: apiPath,
233+
headers: apiHeaders
234+
)
235+
}
236+
217237
///
218238
/// Make an API call
219239
///
@@ -284,6 +304,8 @@ open class Client {
284304
}
285305
}
286306

307+
var data = try await response.body.collect(upTo: Int.max)
308+
287309
switch response.status.code {
288310
case 0..<400:
289311
if response.headers["Set-Cookie"].count > 0 {
@@ -296,10 +318,11 @@ open class Client {
296318
switch T.self {
297319
case is Bool.Type:
298320
return true as! T
321+
case is String.Type:
322+
return (data.readString(length: data.readableBytes) ?? "") as! T
299323
case is ByteBuffer.Type:
300-
return try await response.body.collect(upTo: Int.max) as! T
324+
return data as! T
301325
default:
302-
let data = try await response.body.collect(upTo: Int.max)
303326
if data.readableBytes == 0 {
304327
return true as! T
305328
}
@@ -309,7 +332,6 @@ open class Client {
309332
}
310333
default:
311334
var message = ""
312-
var data = try await response.body.collect(upTo: Int.max)
313335
var type = ""
314336

315337
do {

templates/dart/lib/src/client.dart.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ abstract class Client {
4848
/// Add headers that should be sent with all API calls.
4949
Client addHeader(String key, String value);
5050

51+
/// Sends a "ping" request to Appwrite to verify connectivity.
52+
Future<String> ping();
53+
5154
/// Upload a file in chunks.
5255
Future<Response> chunkedUpload({
5356
required String path,

templates/dart/lib/src/client_base.dart.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ abstract class ClientBase implements Client {
2020
@override
2121
ClientBase addHeader(String key, String value);
2222

23+
@override
24+
Future<String> ping();
25+
2326
@override
2427
Future<Response> call(
2528
HttpMethod method, {

templates/dart/lib/src/client_browser.dart.twig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ class ClientBrowser extends ClientBase with ClientMixin {
7777
return this;
7878
}
7979

80+
@override
81+
Future<String> ping() async {
82+
final String apiPath = '/ping';
83+
final response = await call(HttpMethod.get, path: apiPath, responseType: ResponseType.plain);
84+
85+
return response.data;
86+
}
87+
8088
@override
8189
Future<String?> webAuth(Uri url) async {
8290
final request = http.Request('GET', url);

templates/dart/lib/src/client_io.dart.twig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ class ClientIO extends ClientBase with ClientMixin {
8989
return this;
9090
}
9191

92+
@override
93+
Future<String> ping() async {
94+
final String apiPath = '/ping';
95+
final response = await call(HttpMethod.get, path: apiPath, responseType: ResponseType.plain);
96+
97+
return response.data;
98+
}
99+
92100
@override
93101
Future<Response> chunkedUpload({
94102
required String path,

templates/flutter/lib/src/client.dart.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ abstract class Client {
6565
/// Add headers that should be sent with all API calls.
6666
Client addHeader(String key, String value);
6767

68+
/// Sends a "ping" request to Appwrite to verify connectivity.
69+
Future<String> ping();
70+
6871
/// Send the API request.
6972
Future<Response> call(HttpMethod method, {
7073
String path = '',

templates/flutter/lib/src/client_base.dart.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ abstract class ClientBase implements Client {
2323
@override
2424
ClientBase addHeader(String key, String value);
2525

26+
@override
27+
Future<String> ping();
28+
2629
@override
2730
Future<Response> call(
2831
HttpMethod method, {

templates/flutter/lib/src/client_browser.dart.twig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ class ClientBrowser extends ClientBase with ClientMixin {
9898
return this;
9999
}
100100

101+
@override
102+
Future<String> ping() async {
103+
final String apiPath = '/ping';
104+
final response = await call(HttpMethod.get, path: apiPath, responseType: ResponseType.plain);
105+
106+
return response.data;
107+
}
108+
101109
Future init() async {
102110
final cookieFallback = web.window.localStorage['cookieFallback'];
103111
if (cookieFallback != null) {

templates/flutter/lib/src/client_io.dart.twig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ class ClientIO extends ClientBase with ClientMixin {
130130
return this;
131131
}
132132

133+
@override
134+
Future<String> ping() async {
135+
final String apiPath = '/ping';
136+
final response = await call(HttpMethod.get, path: apiPath, responseType: ResponseType.plain);
137+
138+
return response.data;
139+
}
140+
133141
Future init() async {
134142
if(_initProgress) return;
135143
_initProgress = true;

0 commit comments

Comments
 (0)