Skip to content

Commit

Permalink
Merge pull request #1016 from ItzNotABug/add-ping-to-mobile-sdks
Browse files Browse the repository at this point in the history
Add `ping` to Mobile and other SDKs
  • Loading branch information
christyjacob4 authored Dec 18, 2024
2 parents 27d8ecd + b30d003 commit 05a9855
Show file tree
Hide file tree
Showing 31 changed files with 277 additions and 15 deletions.
27 changes: 27 additions & 0 deletions templates/android/library/src/main/java/io/package/Client.kt.twig
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,25 @@ class Client @JvmOverloads constructor(
return this
}

/**
* Sends a "ping" request to Appwrite to verify connectivity.
*
* @return String
*/
suspend fun ping(): String {
val apiPath = "/ping"
val apiParams = mutableMapOf<String, Any?>()
val apiHeaders = mutableMapOf("content-type" to "application/json")

return call(
"GET",
apiPath,
apiHeaders,
apiParams,
responseType = String::class.java
)
}

/**
* Send the HTTP request
*
Expand Down Expand Up @@ -489,6 +508,14 @@ class Client @JvmOverloads constructor(
it.resume(true as T)
return
}
responseType == String::class.java -> {
val body = response.body!!
.charStream()
.buffered()
.use(BufferedReader::readText)
it.resume(body as T)
return
}
responseType == ByteArray::class.java -> {
it.resume(response.body!!
.byteStream()
Expand Down
28 changes: 25 additions & 3 deletions templates/apple/Sources/Client.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,26 @@ open class Client {
) ?? ""
}

///
/// Sends a "ping" request to Appwrite to verify connectivity.
///
/// @return String
/// @throws Exception
///
open func ping() async throws -> String {
let apiPath: String = "/ping"

let apiHeaders: [String: String] = [
"content-type": "application/json"
]

return try await call(
method: "GET",
path: apiPath,
headers: apiHeaders
)
}

///
/// Make an API call
///
Expand Down Expand Up @@ -284,6 +304,8 @@ open class Client {
}
}

var data = try await response.body.collect(upTo: Int.max)

switch response.status.code {
case 0..<400:
if response.headers["Set-Cookie"].count > 0 {
Expand All @@ -296,10 +318,11 @@ open class Client {
switch T.self {
case is Bool.Type:
return true as! T
case is String.Type:
return (data.readString(length: data.readableBytes) ?? "") as! T
case is ByteBuffer.Type:
return try await response.body.collect(upTo: Int.max) as! T
return data as! T
default:
let data = try await response.body.collect(upTo: Int.max)
if data.readableBytes == 0 {
return true as! T
}
Expand All @@ -309,7 +332,6 @@ open class Client {
}
default:
var message = ""
var data = try await response.body.collect(upTo: Int.max)
var type = ""

do {
Expand Down
3 changes: 3 additions & 0 deletions templates/dart/lib/src/client.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ abstract class Client {
/// Add headers that should be sent with all API calls.
Client addHeader(String key, String value);

/// Sends a "ping" request to Appwrite to verify connectivity.
Future<String> ping();

/// Upload a file in chunks.
Future<Response> chunkedUpload({
required String path,
Expand Down
3 changes: 3 additions & 0 deletions templates/dart/lib/src/client_base.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ abstract class ClientBase implements Client {
@override
ClientBase addHeader(String key, String value);

@override
Future<String> ping();

@override
Future<Response> call(
HttpMethod method, {
Expand Down
8 changes: 8 additions & 0 deletions templates/dart/lib/src/client_browser.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ class ClientBrowser extends ClientBase with ClientMixin {
return this;
}

@override
Future<String> ping() async {
final String apiPath = '/ping';
final response = await call(HttpMethod.get, path: apiPath, responseType: ResponseType.plain);

return response.data;
}

@override
Future<String?> webAuth(Uri url) async {
final request = http.Request('GET', url);
Expand Down
8 changes: 8 additions & 0 deletions templates/dart/lib/src/client_io.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ class ClientIO extends ClientBase with ClientMixin {
return this;
}

@override
Future<String> ping() async {
final String apiPath = '/ping';
final response = await call(HttpMethod.get, path: apiPath, responseType: ResponseType.plain);

return response.data;
}

@override
Future<Response> chunkedUpload({
required String path,
Expand Down
3 changes: 3 additions & 0 deletions templates/flutter/lib/src/client.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ abstract class Client {
/// Add headers that should be sent with all API calls.
Client addHeader(String key, String value);

/// Sends a "ping" request to Appwrite to verify connectivity.
Future<String> ping();

/// Send the API request.
Future<Response> call(HttpMethod method, {
String path = '',
Expand Down
3 changes: 3 additions & 0 deletions templates/flutter/lib/src/client_base.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ abstract class ClientBase implements Client {
@override
ClientBase addHeader(String key, String value);

@override
Future<String> ping();

@override
Future<Response> call(
HttpMethod method, {
Expand Down
8 changes: 8 additions & 0 deletions templates/flutter/lib/src/client_browser.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ class ClientBrowser extends ClientBase with ClientMixin {
return this;
}

@override
Future<String> ping() async {
final String apiPath = '/ping';
final response = await call(HttpMethod.get, path: apiPath, responseType: ResponseType.plain);

return response.data;
}

Future init() async {
final cookieFallback = web.window.localStorage['cookieFallback'];
if (cookieFallback != null) {
Expand Down
8 changes: 8 additions & 0 deletions templates/flutter/lib/src/client_io.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ class ClientIO extends ClientBase with ClientMixin {
return this;
}

@override
Future<String> ping() async {
final String apiPath = '/ping';
final response = await call(HttpMethod.get, path: apiPath, responseType: ResponseType.plain);

return response.data;
}

Future init() async {
if(_initProgress) return;
_initProgress = true;
Expand Down
27 changes: 27 additions & 0 deletions templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,25 @@ class Client @JvmOverloads constructor(
return this
}

/**
* Sends a "ping" request to Appwrite to verify connectivity.
*
* @return String
*/
suspend fun ping(): String {
val apiPath = "/ping"
val apiParams = mutableMapOf<String, Any?>()
val apiHeaders = mutableMapOf("content-type" to "application/json")

return call(
"GET",
apiPath,
apiHeaders,
apiParams,
responseType = String::class.java
)
}

/**
* Prepare the HTTP request
*
Expand Down Expand Up @@ -536,6 +555,14 @@ class Client @JvmOverloads constructor(
it.resume(true as T)
return
}
responseType == String::class.java -> {
val body = response.body!!
.charStream()
.buffered()
.use(BufferedReader::readText)
it.resume(body as T)
return
}
responseType == ByteArray::class.java -> {
it.resume(response.body!!
.byteStream()
Expand Down
28 changes: 25 additions & 3 deletions templates/swift/Sources/Client.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,26 @@ open class Client {
) ?? ""
}

///
/// Sends a "ping" request to Appwrite to verify connectivity.
///
/// @return String
/// @throws Exception
///
open func ping() async throws -> String {
let apiPath: String = "/ping"

let apiHeaders: [String: String] = [
"content-type": "application/json"
]

return try await call(
method: "GET",
path: apiPath,
headers: apiHeaders
)
}

///
/// Make an API call
///
Expand Down Expand Up @@ -327,15 +347,18 @@ open class Client {
}
}

var data = try await response.body.collect(upTo: Int.max)

switch response.status.code {
case 0..<400:
switch T.self {
case is Bool.Type:
return true as! T
case is String.Type:
return (data.readString(length: data.readableBytes) ?? "") as! T
case is ByteBuffer.Type:
return try await response.body.collect(upTo: Int.max) as! T
return data as! T
default:
let data = try await response.body.collect(upTo: Int.max)
if data.readableBytes == 0 {
return true as! T
}
Expand All @@ -345,7 +368,6 @@ open class Client {
}
default:
var message = ""
var data = try await response.body.collect(upTo: Int.max)
var type = ""

do {
Expand Down
1 change: 1 addition & 0 deletions tests/Android14Java11Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Android14Java11Test extends Base
'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"';

protected array $expectedOutput = [
...Base::PING_RESPONSE,
...Base::FOO_RESPONSES,
...Base::BAR_RESPONSES,
...Base::GENERAL_RESPONSES,
Expand Down
1 change: 1 addition & 0 deletions tests/Android14Java17Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Android14Java17Test extends Base
'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"';

protected array $expectedOutput = [
...Base::PING_RESPONSE,
...Base::FOO_RESPONSES,
...Base::BAR_RESPONSES,
...Base::GENERAL_RESPONSES,
Expand Down
1 change: 1 addition & 0 deletions tests/Android14Java8Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Android14Java8Test extends Base
'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"';

protected array $expectedOutput = [
...Base::PING_RESPONSE,
...Base::FOO_RESPONSES,
...Base::BAR_RESPONSES,
...Base::GENERAL_RESPONSES,
Expand Down
1 change: 1 addition & 0 deletions tests/Android5Java17Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Android5Java17Test extends Base
'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"';

protected array $expectedOutput = [
...Base::PING_RESPONSE,
...Base::FOO_RESPONSES,
...Base::BAR_RESPONSES,
...Base::GENERAL_RESPONSES,
Expand Down
1 change: 1 addition & 0 deletions tests/AppleSwift56Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AppleSwift56Test extends Base
'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/apple swift:5.6-focal swift test';

protected array $expectedOutput = [
...Base::PING_RESPONSE,
...Base::FOO_RESPONSES,
...Base::BAR_RESPONSES,
...Base::GENERAL_RESPONSES,
Expand Down
1 change: 1 addition & 0 deletions tests/DartBetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class DartBetaTest extends Base
'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/dart dart:beta sh -c "dart pub get && dart pub run tests/tests.dart"';

protected array $expectedOutput = [
...Base::PING_RESPONSE,
...Base::FOO_RESPONSES,
...Base::BAR_RESPONSES,
...Base::GENERAL_RESPONSES,
Expand Down
1 change: 1 addition & 0 deletions tests/DartStableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class DartStableTest extends Base
'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/dart dart:stable sh -c "dart pub get && dart pub run tests/tests.dart"';

protected array $expectedOutput = [
...Base::PING_RESPONSE,
...Base::FOO_RESPONSES,
...Base::BAR_RESPONSES,
...Base::GENERAL_RESPONSES,
Expand Down
1 change: 1 addition & 0 deletions tests/FlutterBetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class FlutterBetaTest extends Base
'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/flutter fischerscode/flutter-sudo:beta sh -c "sudo chown -R flutter:flutter . && flutter pub get && flutter test test/appwrite_test.dart"';

protected array $expectedOutput = [
...Base::PING_RESPONSE,
...Base::FOO_RESPONSES,
...Base::BAR_RESPONSES,
...Base::GENERAL_RESPONSES,
Expand Down
1 change: 1 addition & 0 deletions tests/FlutterStableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class FlutterStableTest extends Base
'docker run --network="mockapi" --rm -v $(pwd):/app:rw -w /app/tests/sdks/flutter fischerscode/flutter-sudo:stable sh -c "sudo chown -R flutter:flutter . && flutter pub get && flutter test test/appwrite_test.dart"';

protected array $expectedOutput = [
...Base::PING_RESPONSE,
...Base::FOO_RESPONSES,
...Base::BAR_RESPONSES,
...Base::GENERAL_RESPONSES,
Expand Down
1 change: 1 addition & 0 deletions tests/KotlinJava11Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class KotlinJava11Test extends Base
'docker run --network="mockapi" -v $(pwd):/app -w /app/tests/sdks/kotlin openjdk:11-jdk-slim sh -c "./gradlew test -q && cat result.txt"';

protected array $expectedOutput = [
...Base::PING_RESPONSE,
...Base::FOO_RESPONSES,
...Base::BAR_RESPONSES,
...Base::GENERAL_RESPONSES,
Expand Down
1 change: 1 addition & 0 deletions tests/KotlinJava17Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class KotlinJava17Test extends Base
'docker run --network="mockapi" -v $(pwd):/app -w /app/tests/sdks/kotlin openjdk:17-jdk-slim sh -c "./gradlew test -q && cat result.txt"';

protected array $expectedOutput = [
...Base::PING_RESPONSE,
...Base::FOO_RESPONSES,
...Base::BAR_RESPONSES,
...Base::GENERAL_RESPONSES,
Expand Down
1 change: 1 addition & 0 deletions tests/KotlinJava8Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class KotlinJava8Test extends Base
'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"';

protected array $expectedOutput = [
...Base::PING_RESPONSE,
...Base::FOO_RESPONSES,
...Base::BAR_RESPONSES,
...Base::GENERAL_RESPONSES,
Expand Down
1 change: 1 addition & 0 deletions tests/Swift56Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Swift56Test extends Base
'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/swift swift:5.6-focal swift test';

protected array $expectedOutput = [
...Base::PING_RESPONSE,
...Base::FOO_RESPONSES,
...Base::BAR_RESPONSES,
...Base::GENERAL_RESPONSES,
Expand Down
Loading

0 comments on commit 05a9855

Please sign in to comment.